Привет, я пытаюсь создать сайт покупок в Интернете, но когда я добавляю товары (дома) в корзину, я получаю следующую ошибку
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]+)/$']
У меня есть приложение для магазина и корзины.соответствующие представления /cart/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')),
]
Шаблон, в котором возникает ошибка /templates/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, любая помощь будет признательна