简体   繁体   English

如何避免 django 中没有反向匹配

[英]how can i avoid no reverse match in django

I don't know why this error occurred no revers match while I'm practicing Django.我不知道为什么在练习 Django 时出现此错误occurred no revers match

I modified the shops.html code to link pizza but after this modifying, this error occurred我修改了shops.html代码来链接披萨,但是修改后,出现了这个错误

Reverse for 'pizza' with arguments '('',)' not found.未找到 arguments '('',)' 的 'pizza' 反向。 1 pattern(s) tried: ['shops/(?P<pizza_id>[0-9]+)$']尝试了 1 种模式:['shops/(?P<pizza_id>[0-9]+)$']

first code:第一个代码:

<ul>
    {%for shop in shops%}

         <li>
            {{shop}}
         </li>

second code:第二个代码:

<ul>
    {%for shop in shops%}

     <li>
        <a href="{% url 'pizzas:pizza' pizza.id %}">{{shop}}</a></li>

I have Posted all codes in Pastebin if needed.如果需要,我已经在Pastebin中发布了所有代码。

your problem is that instead of shop.pk you used pizza.id and that is not actually defined.您的问题是您使用的是pizza.id shop.pk实际上并未定义。
change this改变这个

{%for shop in shops%}
 
 <li>
    <a href="{% url 'pizzas:pizza' pizza.id %}">{{shop}}</a></li>
 
{%empty%}
 
 <li>currently there is no pizza available</li>
{%endfor%}

to

{%for shop in shops%}
 
 <li>
    <a href="{% url 'pizzas:pizza' shop.pk %}">{{shop}}</a></li>
 
{%empty%}
 
 <li>currently there is no pizza available</li>
{%endfor%}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM