首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >找不到参数为‘(’,)‘’的'cart_add‘在/cart/ NoReverseMatch处的反向。尝试了1个模式:['cart/add/(?P<house_id>[0-9]+)/$']

找不到参数为‘(’,)‘’的'cart_add‘在/cart/ NoReverseMatch处的反向。尝试了1个模式:['cart/add/(?P<house_id>[0-9]+)/$']
EN

Stack Overflow用户
提问于 2019-03-20 01:08:48
回答 1查看 615关注 0票数 -1

您好,我试图创建一个在线房屋购物网站,但当我要添加项目(房屋)到购物车时,我得到以下错误

    Internal Server Error: /cart/
Traceback (most recent call last):
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\user\myprojects\byarent\buyandrent\cart\views.py", line 31, in cart_detail
    return render(request, 'cart/detail.html', {'cart': cart})
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\shortcuts.py", line 36, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\defaulttags.py", line 512, in render
    return self.nodelist.render(context)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\template\defaulttags.py", line 442, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\urls\base.py", line 90, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "C:\Users\user\myprojects\byarent\env\lib\site-packages\django\urls\resolvers.py", line 622, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'cart_add' with arguments '('',)' not found. 1 pattern(s) tried: ['cart/add/(?P<house_id>[0-9]+)/$']

我有一个商店和购物车应用程序,这里是相关的视图/购物车/view.py

@require_POST
def cart_add(request, house_id):
    cart = Cart(request)
    house = get_object_or_404(House, id=house_id)
    form = CartAddHouseForm(request.POST)
    if form.is_valid():
        cd = form.cleaned_data
        cart.add(house=house,
                 quantity=cd['quantity'],
                 update_quantity=cd['update'])
    return redirect('cart:cart_detail')

def cart_remove(request, house_id):
    cart = Cart(request)
    house = get_object_or_404(House, id=house_id)
    cart.remove(house)
    return redirect('cart:cart_detail')

def cart_detail(request):
    cart = Cart(request)
    for item in cart:
        item['update_quantity_form'] = CartAddHouseForm(
                          initial={'quantity': item['quantity'],
                          'update': True})
    return render(request, 'cart/detail.html', {'cart': cart})

/shop/views.py

def house_list(request, category_slug=None):
    category = None
    categories = Category.objects.all()
    houses = House.objects.filter(available=True)
    if category_slug:
        category = get_object_or_404(Category, slug=category_slug)
        houses = houses.filter(category=category)
    return render(request,
                  'shop/house/list.html',
                  {'category': category,
                   'categories': categories,
                   'houses': houses})

def house_detail(request, id, slug):
    house = get_object_or_404(House,
                            id=id,
                            slug=slug,
                            available=True)
    cart_house_form = CartAddHouseForm()
    return render(request,
                'shop/house/detail.html',
                {'house': house,
                'cart_house_form':cart_house_form})

下面是购物车应用程序的urls.py

urlpatterns = [
    path('', views.cart_detail, name='cart_detail'),
    path('add/<int:house_id>/',
         views.cart_add,
         name='cart_add'),
    path('remove/<int:house_id>/',
         views.cart_remove,
         name='cart_remove'),
]

我的项目的urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('cart/', include('cart.urls', namespace='cart')),
    # path('orders/', include('orders.urls', namespace='orders')),
    path('', include('shop.urls', namespace='shop')),
]

引发错误的模板/ template /shop/base.html

{% load static %}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>{% block title %}My shop{% endblock %}</title>
  <link href="{% static "css/base.css" %}" rel="stylesheet">
</head>
<body>
  <div id="header">
    <a href="/" class="logo">My shop</a>
  </div>
  <div id="subheader">
    <div class="cart">
      {% with total_items=cart|length %}
          {% if cart|length > 0 %}
            Your cart: 
            <a href="{% url "cart:cart_detail" %}">
              {{ total_items }} item{{ total_items|pluralize }},
              ${{ cart.get_total_price }}
            </a>
          {% else %}
            Your cart is empty.
          {% endif %}
        {% endwith %}
    </div>
  </div>
  <div id="content">
    {% block content %}
    {% endblock %}
  </div>
</body>
</html>

/templates/cart/detail.html

{% extends "shop/base.html" %}
{% load static %}

{% block title %}
  Your shopping cart
{% endblock %}

{% block content %}
  <h1>Your shopping cart</h1>
  <table class="cart">
    <thead>
      <tr>
        <th>Image</th>
        <th>House</th>
        <th>Quantity</th>
        <th>Remove</th>
        <th>Unit price</th>                
        <th>Price</th>
      </tr>
    </thead>
    <tbody>
      {% for item in cart %}
        {% with house=item.house %}
          <tr>
            <td>
              <a href="{{ house.get_absolute_url }}">
                <img src="{% if house.image %}{{ house.image.url }}{% else %}{% static "img/no_image.png" %}{% endif %}">                    
              </a>
            </td>
            <td>{{ house.name }}</td>
            <td>
                <form action="{% url "cart:cart_add" house.id %}" method="post">
                    {{ item.update_quantity_form.quantity }}
                    {{ item.update_quantity_form.update }}
                    <input type="submit" value="Update">
                    {% csrf_token %}
                </form>
            </td>
            <td><a href="{% url "cart:cart_remove" house.id %}">Remove</a></td>
            <td class="num">${{ item.price }}</td>
            <td class="num">${{ item.total_price }}</td>
          </tr>
        {% endwith %}
      {% endfor %}
      <tr class="total">
        <td>Total</td>
        <td colspan="4"></td>
        <td class="num">${{ cart.get_total_price }}</td>
      </tr>
    </tbody>
  </table>
  <p class="text-right">
    <a href="{% url "shop:house_list" %}" class="button light">Continue shopping</a>
    <a href="#" class="button">Checkout</a>
  </p>
{% endblock %}

我还在掌握Django的技巧,任何帮助都将不胜感激

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-20 02:28:20

尝试将您的url更改为:

re_path(add/(?P<house_id>[0-9]+)/$',view.cart_add)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55246509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档