反向{%url%}:反向为“”未找到。 ''不是有效的视图函数或模式名称 [英] Reverse {% url %}: Reverse for '"' not found. '' is not a valid view function or pattern name
问题描述
我已经坚持了一段时间,似乎无法解决该错误。我不在视图的任何地方使用 reverse()
。
I've been stuck on this for a while, can't seem to fix the error. I do not use reverse()
anywhere in the view.
urls.py
urlpatterns = [
url(r"^book/", include("bookings.urls")),
]
bookings.urls.py
from django.conf.urls import url
from . import views
app_name = 'bookings'
urlpatterns = [
url(r'^charge/$', views.charge, name='charge'),
url(r'^booking/$', views.booking, name='booking'),
]
views.py
def booking(request):
# some code
render(request, 'bookings/template.html', {'listing': listing,})
def charge(request):
# some code
template.html
<form action="{% url 'bookings:charge' %}" method="post">
我尝试了所有不同的更改和名称空间,例如尝试仅使用 charge
,urls.py中的其他命名空间等。
I tried all different alterations and namespaces, e.g. trying to use just charge
, different namespaces in urls.py etc.
当我渲染 book / booking /
,出现以下错误:
When I render the book/booking/
, I get the following error:
未找到费用的相反内容。 'charge'不是有效的视图函数
或模式名称。
Reverse for 'charge' not found. 'charge' is not a valid view function or pattern name.
推荐答案
如果要以这种方式使用URL标记,则需要在urls.py中添加命名空间
。
If you want to use URL tag on that way, you need a namespace
in your urls.py.
url(r"^book/", include("bookings.urls", namespace="bookings")),
然后,您的模板中可以包含 {%url'bookings:charge'%}
。
Then you can have {% url 'bookings:charge' %}
in your template.
这篇关于反向{%url%}:反向为“”未找到。 ''不是有效的视图函数或模式名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!