使用参数 '('l', 'o', 'n', 'd', 'o', 'n')' 和关键字反转 'places'未找到参数“{}”
Reverse for 'places' with arguments '('l', 'o', 'n', 'd', 'o', 'n')' and keyword arguments '{}' not found
Django 1.9.6
你能帮我理解我做错了什么吗?
该程序已尝试:['places/$']。好吧,这有点令人困惑。
/places/ + 参数 'london' 有效。我的意思是 http://localhost:8000/places/london/ 工作正常。
我很乐意在这里踢球。提前谢谢你。
/photoarchive/place/views.py
class PlaceUpdateView(UpdateView):
model = Place
fields = ['modern_name', 'since', 'postal_code']
template_name_suffix = '_update_form'
/photoarchive/photoarchive/urls.py
urlpatterns = [
url(r'^places/create$', login_required(PlaceCreateView.as_view()), name="place_create"),
url(r'^places/(?P<slug>[\w-]+)/edit$', login_required(PlaceUpdateView.as_view()), name="place_edit"),
url(r'^places/(?P<slug>[\w-]+)/$', login_required(PlaceDetailView.as_view()), name="place"),
url(r'^places/$', login_required(PlaceListView.as_view()), name="places"),
]
/photoarchive/place/templates/place/place_update_form.html
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save" />
</form>
回溯
Exception Type: NoReverseMatch at /places/london/edit
Exception Value: Reverse for 'places' with arguments '('l', 'o', 'n', 'd', 'o', 'n')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['places/$']
稍后添加(完整追溯)
Environment:
Request Method: POST
Request URL: http://localhost:8000/places/london/edit
Django Version: 1.9.6
Python Version: 3.5.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'registration',
'homepage',
'person',
'general',
'picture',
'place']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in post
279. return super(BaseUpdateView, self).post(request, *args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in post
222. return self.form_valid(form)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in form_valid
202. return super(ModelFormMixin, self).form_valid(form)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in form_valid
108. return HttpResponseRedirect(self.get_success_url())
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in get_success_url
190. url = self.object.get_absolute_url()
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/db/models/__init__.py" in inner
43. return reverse(bits[0], None, *bits[1:3])
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/core/urlresolvers.py" in reverse
600. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
508. (lookup_view_s, args, kwargs, len(patterns), patterns))
Exception Type: NoReverseMatch at /places/london/edit
Exception Value: Reverse for 'places' with arguments '('l', 'o', 'n', 'd', 'o', 'n')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['places/$']
稍后添加
urlpatterns = [
url(r'^$', login_required(HomePage.as_view()), name='home'),
url(r'^admin/', admin.site.urls),
url(r'^accounts/logout/$', auth_views.logout, {'next_page': 'auth_login'}, name='auth_logout'),
url(r'^accounts/', include('registration.backends.hmac.urls')), # django-registration
url(r'^people/create/$', login_required(PersonCreateView.as_view()), name="person_create"),
url(r'^people/(?P<slug>[\w-]+)/edit$', login_required(PersonUpdateView.as_view()), name="person_edit"),
url(r'^people/(?P<slug>[\w-]+)/$', login_required(PersonDetailView.as_view()), name="person"),
url(r'^people/$', login_required(PersonListView.as_view()), name="people"),
url(r'^search/$', login_required(SearchEngineView.as_view()), name="search"),
url(r'^places/create$', login_required(PlaceCreateView.as_view()), name="place_create"),
url(r'^places/(?P<slug>[\w-]+)/edit$', login_required(PlaceUpdateView.as_view()), name="place_edit"),
url(r'^places/(?P<slug>[\w-]+)/$', login_required(PlaceDetailView.as_view()), name="place"),
url(r'^places/$', login_required(PlaceListView.as_view()), name="places"),
]
GET_ABSOLUTE_URL
@models.permalink
def get_absolute_url(self):
return ('places', self.slug)
新例外
Environment:
Request Method: POST
Request URL: http://localhost:8000/places/london/edit
Django Version: 1.9.6
Python Version: 3.5.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'registration',
'homepage',
'person',
'general',
'picture',
'place']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in post
279. return super(BaseUpdateView, self).post(request, *args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in post
222. return self.form_valid(form)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in form_valid
202. return super(ModelFormMixin, self).form_valid(form)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in form_valid
108. return HttpResponseRedirect(self.get_success_url())
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in get_success_url
190. url = self.object.get_absolute_url()
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/db/models/__init__.py" in inner
43. return reverse(bits[0], None, *bits[1:3])
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/core/urlresolvers.py" in reverse
600. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
508. (lookup_view_s, args, kwargs, len(patterns), patterns))
Exception Type: NoReverseMatch at /places/london/edit
Exception Value: Reverse for 'place' with arguments '('l', 'o', 'n', 'd', 'o', 'n')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['places/(?P<slug>[\w-]+)/$']
我认为您的模型 get_absolute_url
有两个问题:
@models.permalink
def get_absolute_url(self):
return ('places', self.slug)
应该是
@models.permalink
def get_absolute_url(self):
return ('place', (self.slug,))
编辑 我相信元组的第二个成员应该是一个可枚举的参数。
EDIT2 仅供参考,我相信这优于使用 models.permalink
:
from django.core.urlresolvers import reverse
...
def get_absolute_url(self):
return reverse('place', args=(self.slug,))
Django 1.9.6
你能帮我理解我做错了什么吗? 该程序已尝试:['places/$']。好吧,这有点令人困惑。
/places/ + 参数 'london' 有效。我的意思是 http://localhost:8000/places/london/ 工作正常。
我很乐意在这里踢球。提前谢谢你。
/photoarchive/place/views.py
class PlaceUpdateView(UpdateView):
model = Place
fields = ['modern_name', 'since', 'postal_code']
template_name_suffix = '_update_form'
/photoarchive/photoarchive/urls.py
urlpatterns = [
url(r'^places/create$', login_required(PlaceCreateView.as_view()), name="place_create"),
url(r'^places/(?P<slug>[\w-]+)/edit$', login_required(PlaceUpdateView.as_view()), name="place_edit"),
url(r'^places/(?P<slug>[\w-]+)/$', login_required(PlaceDetailView.as_view()), name="place"),
url(r'^places/$', login_required(PlaceListView.as_view()), name="places"),
]
/photoarchive/place/templates/place/place_update_form.html
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save" />
</form>
回溯
Exception Type: NoReverseMatch at /places/london/edit
Exception Value: Reverse for 'places' with arguments '('l', 'o', 'n', 'd', 'o', 'n')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['places/$']
稍后添加(完整追溯)
Environment:
Request Method: POST
Request URL: http://localhost:8000/places/london/edit
Django Version: 1.9.6
Python Version: 3.5.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'registration',
'homepage',
'person',
'general',
'picture',
'place']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in post
279. return super(BaseUpdateView, self).post(request, *args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in post
222. return self.form_valid(form)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in form_valid
202. return super(ModelFormMixin, self).form_valid(form)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in form_valid
108. return HttpResponseRedirect(self.get_success_url())
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in get_success_url
190. url = self.object.get_absolute_url()
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/db/models/__init__.py" in inner
43. return reverse(bits[0], None, *bits[1:3])
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/core/urlresolvers.py" in reverse
600. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
508. (lookup_view_s, args, kwargs, len(patterns), patterns))
Exception Type: NoReverseMatch at /places/london/edit
Exception Value: Reverse for 'places' with arguments '('l', 'o', 'n', 'd', 'o', 'n')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['places/$']
稍后添加
urlpatterns = [
url(r'^$', login_required(HomePage.as_view()), name='home'),
url(r'^admin/', admin.site.urls),
url(r'^accounts/logout/$', auth_views.logout, {'next_page': 'auth_login'}, name='auth_logout'),
url(r'^accounts/', include('registration.backends.hmac.urls')), # django-registration
url(r'^people/create/$', login_required(PersonCreateView.as_view()), name="person_create"),
url(r'^people/(?P<slug>[\w-]+)/edit$', login_required(PersonUpdateView.as_view()), name="person_edit"),
url(r'^people/(?P<slug>[\w-]+)/$', login_required(PersonDetailView.as_view()), name="person"),
url(r'^people/$', login_required(PersonListView.as_view()), name="people"),
url(r'^search/$', login_required(SearchEngineView.as_view()), name="search"),
url(r'^places/create$', login_required(PlaceCreateView.as_view()), name="place_create"),
url(r'^places/(?P<slug>[\w-]+)/edit$', login_required(PlaceUpdateView.as_view()), name="place_edit"),
url(r'^places/(?P<slug>[\w-]+)/$', login_required(PlaceDetailView.as_view()), name="place"),
url(r'^places/$', login_required(PlaceListView.as_view()), name="places"),
]
GET_ABSOLUTE_URL
@models.permalink
def get_absolute_url(self):
return ('places', self.slug)
新例外
Environment:
Request Method: POST
Request URL: http://localhost:8000/places/london/edit
Django Version: 1.9.6
Python Version: 3.5.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'registration',
'homepage',
'person',
'general',
'picture',
'place']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in post
279. return super(BaseUpdateView, self).post(request, *args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in post
222. return self.form_valid(form)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in form_valid
202. return super(ModelFormMixin, self).form_valid(form)
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in form_valid
108. return HttpResponseRedirect(self.get_success_url())
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/edit.py" in get_success_url
190. url = self.object.get_absolute_url()
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/db/models/__init__.py" in inner
43. return reverse(bits[0], None, *bits[1:3])
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/core/urlresolvers.py" in reverse
600. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
508. (lookup_view_s, args, kwargs, len(patterns), patterns))
Exception Type: NoReverseMatch at /places/london/edit
Exception Value: Reverse for 'place' with arguments '('l', 'o', 'n', 'd', 'o', 'n')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['places/(?P<slug>[\w-]+)/$']
我认为您的模型 get_absolute_url
有两个问题:
@models.permalink
def get_absolute_url(self):
return ('places', self.slug)
应该是
@models.permalink
def get_absolute_url(self):
return ('place', (self.slug,))
编辑 我相信元组的第二个成员应该是一个可枚举的参数。
EDIT2 仅供参考,我相信这优于使用 models.permalink
:
from django.core.urlresolvers import reverse
...
def get_absolute_url(self):
return reverse('place', args=(self.slug,))