【发布时间】:2023-03-08 05:29:02
【问题描述】:
我收到一条错误消息:
NoReverseMatch at /books/
Reverse for 'urlvar' not found. 'urlvar' is not a valid
view function or pattern name.
我猜{% with %} 标签在books/index.html 中效果不佳
但我不知道如何解决。
这是我的代码:
书籍/urls.py
from django.conf.urls import url
from books import views
urlpatterns = [
url(r'^$', views.BooksModelView.as_view(), name='index'),
url(r'^book/$', views.BookList.as_view(), name='book_list'),
url(r'^author/$', views.AuthorList.as_view(), name='author_list'),
url(r'^publisher/$', views.PublisherList.as_view(), name='publisher_list'),
url(r'^book/(?P<pk>\d+)/$', views.BookDetail.as_view(), name='book_detail'),
url(r'^author/(?P<pk>\d+)/$', views.AuthorDetail.as_view(), name='author_detail'),
url(r'^publisher/(?P<pk>\d+)/$', views.PublisherDetail.as_view(), name='publisher_detail'),
]
模板/书籍/index.html
{% extends 'base_books.html' %}
{% block content %}
<h2>Books Management Systemt</h2>
<ul>
{% for modelname in object_list %}
{% with 'books:'|add:modelname|lower|add:'_list' as urlvar %}
<li><a href="{% url 'urlvar' %}">{{ modelname }}</a></li>
{% endwith %}
{% endfor %}
</ul>
{% endblock %}
【问题讨论】:
-
url 中的 name='urlvar' 在哪里?
-
你使用的是哪个版本的 Django?
-
@Alasdair 我正在使用 2.0.7
-
@HemanthSP 我必须把 name=urlvar 放在哪里?
-
Charlotte Lee 请按照以下 Alasdair 的回答,