PacktPublishing / Django-3-by-Example

Django 3 by Example (3rd Edition) published by Packt
https://djangobyexample.com/
MIT License
716 stars 687 forks source link

Chapter 7: for ' cart_detail' not found. ' cart_detail' is not a valid view function or pattern name. #69

Closed ziom1555 closed 3 years ago

ziom1555 commented 3 years ago

error

my base.html: `{% load static %} <!DOCTYPE html>

{% block title %}Mój sklep{% endblock %}
{% with total_items=cart|length %} {% if cart|length > 0 %} Koszyk: {{ total_items }} produkt{{ total_items|pluralize}}, {{ cart.get_total_price }} zł {% else %} Koszyk jest pusty. {% endif %} {% endwith %}
{% block content %} {% endblock %}
` cart/urls: `from django.urls import path from . import views app_name = 'cart' urlpatterns = [ path('', views.cart_detail, name='cart_detail'), path('add//', views.cart_add, name='cart_add'), path('remove//', views.cart_remove, name='cart_remove'), ]` cart/detail: `{% extends "shop/base.html" %} {% load static %} {% block title %} Koszyk na zakupy {% endblock %} {% block content %}

Koszyk na zakupy

{% for item in cart %} {% with product=item.product %} {% endwith %} {% endfor %}
Obraz Produkt Ilość Usuń Cena jednostkowa Kwota
{{ product.name }}
{{ item.update_quantity_form.quantity }} {{ item.update_quantity_form.update }} {% csrf_token %}
Usuń {{ item.price }} zł {{ item.total_price }} zł
Wartość całkowita {{ cart.get_total_price }} zł

Kontynuuj zakupy Do kasy

{% endblock %}` cart/views.py: `from django.shortcuts import render, redirect, get_object_or_404 from django.views.decorators.http import require_POST from shop.models import Product from .cart import Cart from .forms import CartAddProductForm @require_POST def cart_add(request, product_id): cart = Cart(request) product = get_object_or_404(Product, id=product_id) form = CartAddProductForm(request.POST) if form.is_valid(): cd = form.cleaned_data cart.add(product=product, quantity=cd['quantity'], override_quantity=cd['override']) return redirect('cart:cart_detail') @require_POST def cart_remove(request, product_id): cart = Cart(request) product = get_object_or_404(Product, id=product_id) cart.remove(product) return redirect('cart:cart_detail') def cart_detail(request): cart = Cart(request) for item in cart: item['update_quantity_form'] = CartAddProductForm(initial={'quantity': item['quantity'], 'override': True}) return render(request, 'cart/detail.html', {'cart': cart})` Does anyone know how to fix this error?
ziom1555 commented 3 years ago

I solved the problem. In the base.html file, I had spaces in the line:

<a href=" replies "cart: cart_detail"% inc">
after the word "cart:"

Correct spelling without spaces. There must be no spaces after the word "cart:". Correct ruler:

<a href=" replies "cart: cart_detail"% inc">

zenx commented 3 years ago

Exactly, there should be no spaces in cart:cart_detail. The code should look like this:

<a href="{% url "cart:cart_detail" %}">
    {{ total_items }} item{{ total_items|pluralize }},
    ${{ cart.get_total_price }}
</a>