Reputation: 83
I have a view which the user can create/update their posts. However, when the user has finished their editing, I want them to return back to the post (summary). However, there's an error somewhere which I can't find. Any ideas on what the issue may be?
Error: 1) Reverse for 'aircraftdetail' with arguments '()' and keyword arguments '{'id': 2}' not found. 0 pattern(s) tried: []
2) return HttpResponseRedirect(instance.get_absolute_url())
3) return reverse('aircraftdetail', kwargs={"id": self.id})
views.py
def AircraftUpdate(request, id=None):
instance = get_object_or_404(Aircraft, id=id)
form = AircraftForm(request.POST or None, instance=instance)
if form.is_valid():
instance = form.save(commit=False)
instance.save()
return HttpResponseRedirect(instance.get_absolute_url())
context={"aircraft":instance, "form":form}
return render(request,'aircraft_form.html',context)
Models.py
class Aircraft(models.Model):
title = models.CharField(max_length=50, default="")
image = models.ImageField(upload_to = 'static/image_upload', blank=True)
cost = models.DecimalField(max_digits=8, decimal_places=3)
def get_absolute_url(self):
return reverse('aircraftdetail', kwargs={"id": self.id})
Urls.py - Aircraft
urlpatterns = [
url(r'^detail/(?P<id>\d+)/$', views.AircraftDetail, name='AircraftDetail'),
url(r'^detail/(?P<id>\d+)/edit/$', views.AircraftUpdate, name='AircraftEdit'),
url(r'^$', views.AircrafList, name='aircraft'),]
Urls.py AviationSite (Main)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'aircraft/', include('aircraft.urls')),
url(r'^login/', login_view, name="login"),
url(r'^logout/', logout_view, name="logout"),
url(r'^register/', register_view, name="register"),
url(r'^aircraft/create/$', AircraftCreate),]
Upvotes: 2
Views: 719
Reputation: 308939
Your URL pattern has `name='AircraftDetail' (capitalised 'A' and 'D'):
url(r'^detail/(?P<id>\d+)/$', views.AircraftDetail, name='AircraftDetail'),
That does not match the aircraftdetail
(all lowercase) where you call reverse
:
return reverse('aircraftdetail', kwargs={"id": self.id})
Change one of them to make them match (all lowercase is more common in Django).
Upvotes: 3