我有一個視圖,用戶可以創建/更新他們的帖子。但是,當用戶完成編輯時,我希望他們返回到帖子(摘要)。但是,有一個我找不到的錯誤。任何想法可能是什麼問題?NoReverseMatch錯誤Django - UpdateView
錯誤:1)未找到參數爲'()'和關鍵字參數'{'id':2}'的'aircraftdetail'的反向。 0圖案(多個)嘗試:[]
2)返回HttpResponseRedirect(instance.get_absolute_url())
3)返回的反向( 'aircraftdetail',kwargs = { 「ID」:自我。 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 - 飛機
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(主)
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),]
在Python中,推薦的做法是調用你的視圖函數'aircraft_update'。使用'AircraftUpdate'使其看起來像其他Django用戶的基於類的視圖。 – Alasdair