来自 sites / models.py
def get_absolute_url(self):
return reverse('site-detail', kwargs={'pk':self.pk})
站点/ urls.py
from django.conf.urls import url
from sites.views import SiteCreate, SiteUpdate, SiteDelete, IndexView, TestedView, DetailView, ApproveSite
app_name = 'sites'
urlpatterns = [
url(r'^$', IndexView.as_view(), name='site-list'),
url(r'add/$', SiteCreate.as_view(), name='site-add'),
url(r'(?P<pk>[0-9]+)/update/$', SiteUpdate.as_view(), name='site-update'),
url(r'delete/$', SiteDelete.as_view(), name='delete'),
url(r'tested/$', TestedView.as_view(), name='site-tested'),
url(r'(?P<pk>[0-9]+)/$', DetailView.as_view(), name='site-detail'),
url(r'approve/$', ApproveSite.as_view(), name='approve')
]
来自 sites / views.py
class DetailView(generic.DetailView):
model = Site
template_name = "sites/detail.html"
context_object_name = "site"
站点/ forms.py
from django import forms
from sites.models import Site, Category
class SiteForm(forms.ModelForm):
class Meta:
model = Site
fields = '__all__'
exclude = ('Status',)
当我使用我的网站更新视图中的表单更新网站时,我希望它将我带到网站详细信息视图,但它会抛出NoReverseMatch错误。具体做法是:
NoReverseMatch at /sites/1/update/
Reverse for 'site-detail' with arguments '()' and keyword arguments '{'pk': 1}' not found. 0 pattern(s) tried: []
为什么会发生此错误,如何摆脱它以便我的应用在提交表单后显示我的网站记录的详细信息视图?
答案 0 :(得分:1)
您的应用app_name = 'sites'
中有urls.py
,因此在撤消网址时应包含sites
命名空间:
def get_absolute_url(self):
return reverse('sites:site-detail', kwargs={'pk':self.pk})