Django Url直接错误

时间:2017-07-05 00:47:36

标签: django django-urls

我的Django urlpatterns:

urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^login', views.login_page, name='login'),
url(r'^logout', views.logout_page, name='logout'),
url(r'^register', views.register_page, name='register'),
url(r'^create_book', views.create_book, name='create_book'),

url(r'^^(?P<book_id>[0-9]+)/$', views.book_details, name='book_details'),

url(r'^^(?P<book_id>[0-9]+)/create_entry$', views.create_entry, name='create_entry'),   ]

我正在尝试这样做:

{% url 'book:book_details' book.pk %}

它给出了这个错误:

NoReverseMatch at / 

Reverse for 'book_details' with keyword arguments '{'book_id': 1}' not found. 1 pattern(s) tried: ['^(?P<book_id>[0-9]+)/$']

但是直接在浏览器中访问localhost:8000/ <some id>

如何编写{%url%}部分以使其进入/(某些ID)

1 个答案:

答案 0 :(得分:1)

在模板中更改此行

{% url 'book:book_details' book.pk %}

{% url 'book_details' book.pk %}

并从网址

中删除额外的caret sign ^
url(r'^(?P<book_id>[0-9]+)/$', views.book_details, name='book_details'),

url(r'^(?P<book_id>[0-9]+)/create_entry$', views.create_entry, name='create_entry'),