【发布时间】:2019-04-21 11:26:57
【问题描述】:
我正在使用django 2.1 和python 3.6,但我遇到了一个似乎other people 在过去几年中遇到过的问题。
这是我views.py的一部分:
def blog_list_by_cat(request, cat_id, cat_name):
...
def blog_list_by_genre(request, genre_id, genre_name):
...
这是我的urls.py:
urlpatterns = [
path('', views.index, name='index'),
re_path(r'^blog/(?P<blog_id>\d+)/(?P<slug>[^/]+)/?$', views.single_blog, name='single_blog'),
re_path(r'^blog-list/(?P<cat_id>\d+)/(?P<cat_name>[^/]+)/?$', views.blog_list_by_cat, name='blog_list_by_cat'),
path('blog-list/latest/', views.blog_latest, name='blog_latest'),
re_path(r'^blog-list/genre/(?P<genre_id>\d+)/(?P<genre_name>[^/]+)/?$', views.blog_list_by_genre, name='blog_list_by_genre'),
]
当我调用这些链接时,在我的模板中,这个可以正常工作:
{% for cat in cat_list %}
<a class="dropdown-item" href="{% url 'blog_list_by_cat' cat_id=cat.id cat_name=cat.cat %}">{{ cat.cat }}</a>
{% endfor %}
虽然这个在同一个模板中抛出错误:
{% for genre in genre_list %}
<li><a class="dropdown-item" href="{% url 'blog_list_by_genre' genre_id=genre.id genre_name=genre.title %}">{{ genre.title }}</a></li>
{% endfor %}
这是完整的错误:
异常类型:NoReverseMatch
异常值:“blog_list_by_genre”与关键字参数“{'genre_id”相反: 5、'genre_name': 'Action'}' 未找到。尝试了 1 种模式: ['博客列表/流派/(?P\d+)/(?P[^/]+)/?$']
如您所见,我使用的是named urls,并且我在 url 名称周围使用引号,而不是在 url 中添加应用程序名称。两个代码是一样的。但是为什么第二个不起作用?
【问题讨论】:
标签: django python-3.x url