如何在Django中修复NoReverseMatch

时间:2018-12-30 07:30:04

标签: python django

我正在开发一个电子商务网站,只是为了一种实践,所有代码都工作正常,但是突然之间,我在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>

0 个答案:

没有答案