При попытке просмотреть страницу корзины на моем сайте электронной коммерции я получаю вышеуказанное сообщение. Я вижу, что это довольно распространенный вопрос, но после обхода множества других потоков здесь ни одно из решений не работает.
В текущей итерации моего кода я добавил имя приложения в корзину, а затем сослался на это в моем html, но без радости.
Traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/cart/
Django Version: 2.2.5
Python Version: 3.8.1
Installed Applications:
['pages.apps.PagesConfig',
'accounts.apps.AccountsConfig',
'products.apps.ProductsConfig',
'cart.apps.CartConfig',
'search.apps.SearchConfig',
'crispy_forms',
'storages',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
('whitenoise.middleware.WhiteNoiseMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Template error:
In template C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\pages\templates\base.html, error at line 0
Reverse for '' not found. '' is not a valid view function or pattern name.
1 : {% load static %}
2 :
3 : <!DOCTYPE html>
4 : <html lang="en">
5 :
6 : <head>
7 : <meta charset="utf-8" />
8 : <!-- Set the viewport to allow responsiveness -->
9 : <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
10 : <!-- Bootstrap CSS -->
Traceback:
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\cart\views.py" in view_cart
7. return render(request, 'cart.html')
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\shortcuts.py" in render
36. content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\template\loader.py" in render_to_string
62. return template.render(context, request)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\template\backends\django.py" in render
61. return self.template.render(context)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\template\base.py" in render
171. return self._render(context)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\template\base.py" in _render
163. return self.nodelist.render(context)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\template\loader_tags.py" in render
150. return compiled_parent._render(context)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\template\base.py" in _render
163. return self.nodelist.render(context)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\template\loader_tags.py" in render
62. result = block.nodelist.render(context)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\template\defaulttags.py" in render
443. url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\urls\base.py" in reverse
90. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:\Users\Vegeta\OneDrive\Visual Studio Code\milestone-project-four\env\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
673. raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /cart/
Exception Value: Reverse for '' not found. '' is not a valid view function or pattern name.
проект urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('accounts.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('', include('pages.urls')),
path('products/', include('products.urls')),
path('cart/', include('cart.urls')),
path('search/', include('search.urls')),
]
приложение корзины URL urls.py
from django.urls import path
from .views import view_cart, add_to_cart
app_name = 'cart'
urlpatterns = [
path('', view_cart, name='view_cart'),
path('add/<slug:pk>', add_to_cart, name='add_to_cart'),
]
cart app views.py
from django.shortcuts import render, redirect, reverse
from products.models import Product
def view_cart(request):
"""A View that renders the cart contents page"""
return render(request, 'cart.html')
def add_to_cart(request, item_id):
"""
Add an item to the cart
"""
cart = request.session.get('cart', {})
if item_id in cart:
cart[item_id] = int(cart[item_id])
else:
cart[item_id] = cart.get(item_id)
request.session['cart'] = cart
return redirect(reverse('home'))
base. html навигационная ссылка для просмотра корзины
<li class="nav-item" id="nav-item-cart">
<a class="nav-link" href="{% url 'cart:view_cart' %}"><i class="fa fa-shopping-cart fa-1.5x">/i>Cart
{% if product_count > 0 %}
<label class="badge badge-warning">{{ product_count }}</label>
{% endif %}
</a>
</li>
артефакт. html Форма для добавления товара в корзину
<form method="POST" action="{% url 'cart:add_to_cart' artifact.id %}">
{% csrf_token %}
<label for="buy" id="buy-header"><strong>Buy lot</strong></label>
<input name="disabled" type="text" class="input-field" value="£100,000.00" size="8" disabled>
<a class="btn btn-sm artifact-btn" type="submit">Add to cart</a>
</form>
корзина. html
{% extends 'base.html' %}
{% block content %}
<section class="cart-section">
<div class="container">
<h1 id="cart-header">Your Cart</h1>
{% for item in cart_items %}
<div class="row lot-wrapper">
<div class="col-5 col-md-6 image-wrapper">
<img src="{{ MEDIA_URL }}{{ item.product.image }}" class="lot-image" alt="Picture of {{ product.name }}">
</div>
<div class="col-7 col-md-6 cart-content-wrapper">
<h2 class="cart-lot-header">{{ item.name }}</h2>
<div class="cart-lot-text">
<p><strong>Price</strong> <span class="bid-value"> {{ item.buyout }}</span></p>
</div>
<a href="#"><i class="fa fa-trash" aria-hidden="true"></i></a>
</div>
</div>
<div class="row">
<p>Grand Total: £{{ total }}</p>
</div>
{% endfor %}
</div>
<div class="container">
<div class="row cart-options">
<div class="col">
<a href="{% url for 'products' %}" class="btn btn-sm" id="continue-btn">Continue shopping</a>
</div>
<div class="col">
<a href="checkout.html" class="btn btn-sm" id="checkout-btn">Checkout</a>
</div>
</div>
</div>
</section>
{% endblock content %}
Любая помощь будет принята с благодарностью.