找不到 'x' 的反向。 'x' 不是有效的视图函数或模式名称
Reverse for 'x' not found. 'x' is not a valid view function or pattern name
我有三个页面 - 主页,http://127.0.0.1:8000/, displaying one paragraph sentence and two links in the header. and list of pizzas, http://127.0.0.1:8000/pizzas . Now i was trying to add links for each pizza on http://127.0.0.1:8000/pizzas 页面,这样人们可以点击它们并查看有哪些浇头可用。我可能被卡住了,因为我决定使用路径而不是 url() 来映射 urls,这是我正在阅读的书所使用的。
错误:/pizzas 处没有反向匹配。
找不到 'pizza_w_toppings' 的反向。 'pizza_w_toppings' 不是有效的视图函数或模式名称。
pizzas.html -
{% extends "pizzeria_app/base.html" %}
{% block content %}
<h1> Available Pizzas : </h1>
<ul>
{% for pizza in pizzas %}
<li> <a href = {% url 'pizza_w_toppings' %}> {{pizza}}</a><li>
{% empty %}
<p> We're outta Pizzas. next time bro! <p>
{% endfor %}
</ul>
{% endblock content %}
app/urls.py :
urlpatterns = [
#homepage
path('', views.index),
#show available pizzas
path('pizzas', views.pizzas),
path('pizzas/<int:pizza_id>', views.pizza_w_toppings, name="pizza_w_toppings")
观看次数:
我是 Whosebug 的新手,不知道如何添加我的 views.py。我附上了一张照片,抱歉
views.py screenshot
您的 url 标签应该是 {% url 'pizza_w_toppings' pizza.id %}
。如果您检查 documentation,您将看到 url 标签的所有可能变体。
For example, suppose you have a view, app_views.client, whose URLconf
takes a client ID (here, client() is a method inside the views file
app_views.py). The URLconf line might look like this:
path('client/<int:id>/', app_views.client, name='app-views-client')
If this app’s URLconf is included into the project’s URLconf under a
path such as this:
path('clients/', include('project_name.app_name.urls'))
…then, in a template, you can create a link to this view like this:
{% url 'app-views-client' client.id %}
The template tag will output the string /clients/client/123/.
如果您使用命名空间,请确保在您的 url 标签中包含命名空间,如下所示:
{% url 'your-namespace:app-views-client' client.id %}
我有三个页面 - 主页,http://127.0.0.1:8000/, displaying one paragraph sentence and two links in the header. and list of pizzas, http://127.0.0.1:8000/pizzas . Now i was trying to add links for each pizza on http://127.0.0.1:8000/pizzas 页面,这样人们可以点击它们并查看有哪些浇头可用。我可能被卡住了,因为我决定使用路径而不是 url() 来映射 urls,这是我正在阅读的书所使用的。
错误:/pizzas 处没有反向匹配。
找不到 'pizza_w_toppings' 的反向。 'pizza_w_toppings' 不是有效的视图函数或模式名称。
pizzas.html -
{% extends "pizzeria_app/base.html" %}
{% block content %}
<h1> Available Pizzas : </h1>
<ul>
{% for pizza in pizzas %}
<li> <a href = {% url 'pizza_w_toppings' %}> {{pizza}}</a><li>
{% empty %}
<p> We're outta Pizzas. next time bro! <p>
{% endfor %}
</ul>
{% endblock content %}
app/urls.py :
urlpatterns = [
#homepage
path('', views.index),
#show available pizzas
path('pizzas', views.pizzas),
path('pizzas/<int:pizza_id>', views.pizza_w_toppings, name="pizza_w_toppings")
观看次数: 我是 Whosebug 的新手,不知道如何添加我的 views.py。我附上了一张照片,抱歉 views.py screenshot
您的 url 标签应该是 {% url 'pizza_w_toppings' pizza.id %}
。如果您检查 documentation,您将看到 url 标签的所有可能变体。
For example, suppose you have a view, app_views.client, whose URLconf takes a client ID (here, client() is a method inside the views file app_views.py). The URLconf line might look like this:
path('client/<int:id>/', app_views.client, name='app-views-client')
If this app’s URLconf is included into the project’s URLconf under a path such as this:
path('clients/', include('project_name.app_name.urls'))
…then, in a template, you can create a link to this view like this:
{% url 'app-views-client' client.id %}
The template tag will output the string /clients/client/123/.
如果您使用命名空间,请确保在您的 url 标签中包含命名空间,如下所示:
{% url 'your-namespace:app-views-client' client.id %}