首页 > 解决方案 > 如何在 django 中修复 NoReverseMatch

问题描述

我正在开发一个电子商务网站只是为了练习,所有代码都运行良好,但突然间我在 django 模板上遇到错误

我已经尝试遵循大多数答案,但似乎都没有奏效

这是我的 urls.py

urlpatterns = [
    url(r'^$', views.cart_detail, name='cart_detail'),
    url(r'^add/(?P<product_id>\d+)/$', views.cart_add, name='cart_add'),
    url(r'^remove/(?P<product_id>\d+)/$', views.cart_remove, name='cart_remove'),
]

这是我项目的主要 urls.py

urlpatterns = [
    # Examples:
    # url(r'^$', 'myshop.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^cart/', include('cart.urls', namespace='cart')),
    url(r'^orders/', include('orders.urls', namespace='orders')),
    url(r'^paypal/', include('paypal.standard.ipn.urls')),
    url(r'^payment/', include('payment.urls', namespace='payment')),
    url(r'^coupons/', include('coupons.urls', namespace='coupons')),
    url(r'^', include('shop.urls', namespace='shop')),

]

这是我收到错误的模板

{% block content %}
<h1>Your shopping cart</h1>
<table class="cart table-striped ">
    <thead>
    <tr>
        <th>Image</th>
        <th>Product</th>
        <th>Quantity</th>
        <th>Remove</th>
        <th>Unit Price</th>
        <th>Price</th>
    </tr>
    </thead>
    <tbody>
    {% for item in cart %}
    {% with product=item.product %}
    <tr>
        <td>
            <a href="{{ product.get_absolute_url }}">
                <img src="{% if product.image %}
                {{ product.image.url }}
                {% else %}{% endif %}">
            </a>
        </td>
        <td>{{product.name}}</td>
        <td>
            <form action="{% url 'cart:cart_add' product.id %}" method="post">
                {{item.update_quantity_form.quantity}}
                {{item.update_quantity_form.update}}
                <input type="submit" class=" btn btn-default outline" value="update">
                {% csrf_token %}
            </form>
        </td>
        <td>
            <a href="{% url 'cart:cart_remove' product.id %}">Remove</a>
        </td>
        <td class="num">${{item.price}}</td>
        <td class="num">${{item.total_price}}</td>
    </tr>
    {% endwith %}
    {% endfor %}
    {% if cart.coupon %}}
    <tr class="subtotal">
<td class="{% trans 'subtotal' %}"></td>
        <td colspan="4"></td>
        <td class="num">${{ cart.get_total_price}}</td>
    </tr>
    <tr>
{% blocktrans with code=cart.coupon.code discount=cart.coupon.discount %}
        <td>"{{ code}}" coupon ({{ discount}}% off)</td>
        {% endblocktrans %}
        <td colspan="4"></td>
        <td class="num neg">- ${{cart.get_discount|floatformat:"2"}}</td>
    </tr>
    {% endif %}
    <tr class="total"></tr>
    <td>{% trans "total" %}</td>
    <td colspan="4"></td>
    <td class="num">${{ cart.get_total_price_after_discount|floatformat:"2"}}</td>
    </tbody>
</table>
<p>
    {% trans "Apply for a coupon" %}
</p>
<form action="{% url 'coupons:apply' %}" method="post">
    {{ coupon_apply_form}}
    <input type="submit" value="{% trans 'Apply' %}">
    {% csrf_token %}
</form>
<p class="text-right">
    <a href="{% url 'shop:product_list' %}" class="button-light">Continue shopping</a>
    <a href="{% url 'orders:order_create' %}" class="button">Checkout</a>
</p>
{% endblock %}

这是我在模板渲染期间遇到的错误 在模板/Users/nirvana/PycharmProjects/Ecommerce/myshop/templates/cart/detail.html 中,第 35 行出现错误

Reverse for 'cart_add' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['cart/add/(?P<product_id>\\d+)/$']
25  

        <tr>
    26          <td>
    27              <a href="{{ product.get_absolute_url }}">
    28                  <img src="{% if product.image %}
    29                  {{ product.image.url }}
    30                  {% else %}{% endif %}">
    31              </a>
    32          </td>
    33          <td>{{product.name}}</td>
    34          <td>
    35  
                      <form action="
          {% url 'cart:cart_add' product.id %}
          " method="post">


    36                  {{item.update_quantity_form.quantity}}
    37                  {{item.update_quantity_form.update}}
    38                  <input type="submit" class=" btn btn-default outline" value="update">
    39                  {% csrf_token %}
    40              </form>
    41          </td>
    42   





<td>
43   <a href="{% url 'cart:cart_remove' product.id %}">Remove</a>
44          </td>
45          <td class="num">${{item.price}}</td>

标签: pythondjango

解决方案


推荐阅读