简体   繁体   English

在Django 1.9模板中将相对URL链接到视图时出现NoRverseMatch错误

[英]NoRverseMatch Error when linking relative urls to views in django 1.9 templates

I'm using the view-callables in my app.urls because I keep getting warnings that app.views.view_name is being deprecated 我在app.urls中使用view-callables,因为我不断收到关于app.views.view_name已过时的警告

app.urls
from .views import (
post_list,
post_detail,
post_create,
post_edit,
post_delete,
recent_posts,
)

urlpatterns = [
    url(r'^$', recent_posts, name='recent_posts'),
    url(r'^post/$', post_list, name="list"),
    url(r'^post/create/$', post_create, name="create"),
    url(r'^post/(?P<slug>[\w-]+)/$', post_detail, name="detail"),
    url(r'^post/(?P<slug>[\w-]+)/edit/$', post_edit, name="edit"),
    url(r'^post/delete/$', post_delete, name="delete"),

    ]

Here is my project urls.py 这是我的项目urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include ("blog.urls", namespace="post")),
]

here's the template: 这是模板:

<div class="menu-wrap">
   <nav class="menu">
       <ul>
           <li><a href="/">Home</a></li>
           <li><a href="{% url 'app.views.view_name' %}">All Posts</a></li>
           {% if user.is_authenticated %}
            <li><a href="{% url 'app.views.another_view_name' %}"> Create Post </a></li>
           {% endif %}
       </ul>
   </nav>
</div>

This is what I have had to use but it's also bad, probably worse 这是我不得不使用的,但它也很糟糕,可能更糟

<div class="menu-wrap">
   <nav class="menu">
       <ul>
           <li><a href="/">Home</a></li>
           <li><a href="{% url 'app.views.view_name' %}">All Posts</a></li>
           {% if user.is_authenticated %}
            <li><a href="{% url 'app.views.another_view_name' %}"> Create Post </a></li>
           {% endif %}
       </ul>
   </nav>
</div>

Please any ideas on how to represent urls to my views in the templates? 请问关于如何在模板中表示我的观点的网址的任何想法? Thank you. 谢谢。

You should use the namespace name,in your case 'post' followed by colon (:) then view_name eg to list all posts: 您应该使用名称空间名称,在您的情况下使用“ post”,后跟冒号(:),然后使用view_name例如列出所有帖子:

<li><a href="{% url 'post:list' %}">All posts</a><li>

You can read more on how to handle urls in django documentation https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#url 您可以在Django文档https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#url中阅读有关如何处理url的更多信息。

You want to link to the relevant URL using the url name that you declare in your urls.py file. 您想要使用在urls.py文件中声明的url name链接到相关的URL。 So, your template should probably look like: 因此,您的模板应该看起来像:

<div class="menu-wrap">
   <nav class="menu">
       <ul>
           <li><a href="/">Home</a></li>
           <li><a href="{% url 'list' %}">All Posts</a></li>
           {% if user.is_authenticated %}
            <li><a href="{% url 'create' %}"> Create Post </a></li>
           {% endif %}
       </ul>
   </nav>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM