django urls在浏览器中抛出错误

时间:2017-10-14 17:43:13

标签: python django

我从常规网址格式切换到为每个应用创建urls.py文件,因此在移动网址后我收到错误Reverse for 'trending' not found. 'trending' is not a valid view function or pattern name

urls.py

from django.conf.urls import url
from . import views
from django.contrib import admin

app_name = 'posts'
urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^trending/$', views.trending, name='trending'),
    url(r'^ranking/$', views.post_ranking, name='rank'),
    url(r'^create/$', views.create, name='create'),
]

general urls.py url(r'^', include('posts.urls' , namespace="posts")),

模板

<li><a href="{% url 'trending' %}"><i class="fa fa-hashtag"><small class="notification-badge">5</small></i> Trending</a></li>
            <li><a href="{% url 'index' %}"><i class="fa fa-level-down"></i> Recent</a></li>

将根据要求添加其他代码。

1 个答案:

答案 0 :(得分:1)

它应该与模板中的此链接一起使用:

equals

以及以下链接的相同内容:href="{% url 'posts:trending' %}"

Reverse Namespace documentation