我正在按照"Django by Example“这本书中的代码做网络商店。我尝试了谷歌和搜索Stackoverflow,但我没有找到这个问题的答案。
产品列表页面和产品详细信息页面工作正常,但当我尝试向购物车添加项目时,浏览器显示: TemplateSyntaxError at / cart /,Invalid block tag:'"cart:cart_remove"',预期为'endwith‘。
以下是错误页面的一部分屏幕截图:error page code lines, maybe the picture is more clear。在这里您还可以看到错误页面的代码:
23 {% with product=item.product %}
24 <tr>
25 <td>
26 <a href="{{ product.get_absolute_url }}">
27 <img src="{% if product.image %}{{ product.image.url
28 }}{% else %}{% static "img/no_image.png" %}{% endif %}">
29 </a>
30 </td>
31 <td>{{ product.name }}</td>
32 <td>{{ item.quantity }}</td>
33
<td><a href="
{% "cart:cart_remove" product.id %}
">Remove</a></td>
34 <td class="num">${{ item.price }}</td>
35 <td class="num">${{ item.total_price }}</td>
36 </tr>
37 {% endwith %}我检查了代码。正如你在下面看到的,我有endwith标签,所以我不明白为什么它会给我这样的错误。Pycharm抱怨说:“所以我把'no_image.png‘和'cart:cart_remove’改成了‘cart_remove’。然而,我试着在用‘替换’后运行网站,但它给了我同样的错误。”
也许这个问题是相关的。Pycharm抱怨cart\view.py的店铺模式,并用红色下划线:
from shop.models import ProductPycharm表示未解析的引用为“shop”。
shop\view.py也有类似的问题:
from cart.forms import CartAddProductForm我之前也有一些类似的未解决的引用问题,但它似乎以某种方式自行消失了。
发布于 2016-08-26 16:34:24
问题不在于endwith,而在于突出显示的标签。"cart:cart_remove"不是一个标记;我想您在这里是要使用{% url "cart:cart_remove" ... %}的。
https://stackoverflow.com/questions/39161736
复制相似问题