我有一个用户可以创建/更新帖子的视图。但是,当用户完成编辑后,我希望他们返回帖子(摘要)。但是,我无法找到某个错误。关于问题可能是什么想法?
错误: 1)反向' aircraftdetail'参数'()'和关键字参数' {' id':2}'未找到。尝试了0种模式:[]
2)返回HttpResponseRedirect(instance.get_absolute_url())
3)返回反向(' 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 - 飞机
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),]
答案 0 :(得分:3)
您的网址格式有`name =&#39; AircraftDetail&#39; (大写&#39; A&#39;和&#39; D):
url(r'^detail/(?P<id>\d+)/$', views.AircraftDetail, name='AircraftDetail'),
这与您拨打aircraftdetail
的{{1}}(全部小写)不符:
reverse
更改其中一个以使它们匹配(所有小写在Django中更常见)。