我已经坚持了一段时间,似乎无法修复错误。我没有在 View 中的任何地方使用 reverse()
。
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">
我尝试了所有不同的更改和 namespace ,例如尝试仅使用 charge
,urls.py 中的不同命名空间等。
当我呈现 book/booking/
时,出现以下错误:
Reverse for 'charge' not found. 'charge' is not a valid view function or pattern name.
最佳答案
如果你想以那种方式使用 URL 标签,你需要在你的 urls.py 中有一个 namespace
。
url(r"^book/", include("bookings.urls", namespace="bookings")),
然后您可以在模板中包含 {% url 'bookings:charge' %}
。
关于python - Reverse {% url %} : Reverse for '"' not found. '' 不是有效的 View 函数或模式名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49709479/