动态URL Django模板的NoReverseMatch

时间:2016-07-04 16:01:43

标签: python django django-templates

在我的urls.py中,我已将用于编辑帖子的网址设置为slug / edit

url(r'^(?P<slug>[\w-]+)/edit/$', post_update, name='update'),

从模板调用href="{% url 'posts:update' %}时,出现以下错误:

NoReverseMatch at /posts/this-is-my-title-to-my-awesome-post/

Reverse for 'update' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['posts/(?P<slug>[\\w-]+)/edit/$']

似乎无法从帖子实例继承slug。 (要清楚posts:update是从帖子本身调用的。我试图输入正确的完整网址,但是反向匹配则无法完全找到模式。

Environment:


Request Method: GET
Request URL: http://192.168.1.58:8800/posts/this-is-my-title-to-my-awesome-post/

Django Version: 1.9.7
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',
 'crispy_forms',
 'trumbowyg',
 'flatpages',
 'home',
 'posts']
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']


Template error:
In template C:\Users\Kasper\OneDrive\Projects\PersonalWebsite\_Home\trydjango19env\src\templates\post_detail.html, error at line 10
   Reverse for 'update' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['posts/(?P<slug>[\\w-]+)/edit/$']   1 : {% extends "base.html" %}
   2 : 
   3 : {% block head_title %}{{ instance.title }} | {{ block.super }} {% endblock %}
   4 : 
   5 : 
   6 : 
   7 : 
   8 : {% block admin %}
   9 :   <li><a href="#"></a></li>
   10 :   <li><a href=" {% url 'posts:update' %} "></a></li>
   11 :   <li><a href="{% url 'admin:index' %}">A</a></li>
   12 : {% endblock admin %}
   13 : 
   14 : 
   15 : 
   16 : {% block content %}
   17 : 
   18 : <!--Featured Image-->
   19 : <div class="flex-center" style="height: auto;">
   20 :   <div class="view overlay hm-white-slight">


Traceback:

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\core\urlresolvers.py" in reverse
  586.                 extra, resolver = resolver.namespace_dict[ns]

During handling of the above exception ('trydjango19.posts'), another exception occurred:

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\defaulttags.py" in render
  507.                               current_app=current_app)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\core\urlresolvers.py" in reverse
  596.                                          key)

During handling of the above exception ('trydjango19.posts' is not a registered namespace), another exception occurred:

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\core\handlers\base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\core\handlers\base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\Kasper\OneDrive\Projects\PersonalWebsite\_Home\trydjango19env\src\posts\views.py" in post_detail
  44.   return render(request, "post_detail.html", context)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\shortcuts.py" in render
  67.             template_name, context, request=request, using=using)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\loader.py" in render_to_string
  97.         return template.render(context, request)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\backends\django.py" in render
  95.             return self.template.render(context)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\base.py" in render
  206.                     return self._render(context)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\base.py" in _render
  197.         return self.nodelist.render(context)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\loader_tags.py" in render
  173.         return compiled_parent._render(context)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\base.py" in _render
  197.         return self.nodelist.render(context)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\defaulttags.py" in render
  326.                 return nodelist.render(context)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\loader_tags.py" in render
  69.                 result = block.nodelist.render(context)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\defaulttags.py" in render
  513.                         six.reraise(*exc_info)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\utils\six.py" in reraise
  686.         raise value

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\template\defaulttags.py" in render
  499.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\core\urlresolvers.py" in reverse
  600.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File "C:\Users\Kasper\OneDrive\Projects\PERSON~1\_Home\TRYDJA~1\lib\site-packages\django\core\urlresolvers.py" in _reverse_with_prefix
  508.                              (lookup_view_s, args, kwargs, len(patterns), patterns))

Exception Type: NoReverseMatch at /posts/this-is-my-title-to-my-awesome-post/
Exception Value: Reverse for 'update' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['posts/(?P<slug>[\\w-]+)/edit/$']

1 个答案:

答案 0 :(得分:1)

你错过了对象的slu ::

href="{% url 'posts:update' 'object-slug-here' %}

您可以使用Django's url template tag看到一些打印网址的选项。