Why My Url Template tag not work

Reverse for ‘AddCart’ not found. ‘AddCart’ is not a valid view function or pattern name.

My Main urls.py

from django.contrib import admin
from django.urls import path, include
from .import views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index),
    path('onlinePizza/', include('onlinePizza.urls')),
    path('cart/', include(('cart.urls'), namespace='cart')),
    path('accounts/', include(('accounts.urls'), namespace='accounts')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

my cart app urls.py

from django.urls import path
from .import views
from cart.views import Cart

app_name = 'cart'

urlpatterns = [
    path('add_cart/', views.AddCart, name='Cart'),
]

template

<form action="{% url 'cart:AddCart' %}" method="POST">{% csrf_token %}
       <input hidden type="text" name="product" value="{{i.id}}">
       <input hidden type="text" name="remove" value="True">
       <button  class="main-btn cart cart-btn" style="padding: 3px 17px;"><i class="fa-solid fa-minus"></i></button>
</form>

Not only this URL tag does not work but my Project’s every URL tag is not working.

It’s because you have set the URL’s name to “Cart” but you are calling “AddCart”.

Either change the URL name in app urls.py to “AddCart”, or update the URL in your template to “cart:Cart”

1 Like

This question also related to this project

I have one more question please solve this query.

from django.shortcuts import render, redirect
from django.views import View
from cart.models import *
from onlinePizza.models import Product

# Create your views here.

def AddCart(request):
	product = request.POST.get('product')
	remove = request.POST.get('remove')
	cart = request.session.get('cart')
	
	if not cart:
		request.session['cart'] = {}

	if cart:
		quantity=cart.get(product)
		if quantity:
			if remove:
				if quantity<=1:
					cart.pop(product)
				else:
					cart[product]=quantity-1
			else:	
				cart[product]=quantity+1
		else:
			cart[product]=1
	else:
		cart={}
		cart[product]=1

	request.session['cart']=cart

	return redirect ('Menu')

def Cart(request):
	ids = list(request.session.get('cart').keys())
	products = Product.get_products_by_id(ids)
	context={'products':products}
	print(products)
	return render(request , 'cart/pc_cart.html', context)

This is my models.py

class Product(models.Model):
    product_id = models.AutoField
    product= models.CharField(max_length=50)
    category = models.ForeignKey(Category, default='', null=True, on_delete=models.CASCADE)
    desc = models.TextField()
    price = models.FloatField(max_length=10, default=0)
    nonveg = models.BooleanField()
    # ratings = GenericRelation(Rating)
    slug = models.SlugField(max_length=100)
    image = models.ImageField(upload_to='onlinePizza/image', default="")

    @staticmethod
    def get_products_by_id(ids):
        return Product.objects.filter(id__in=ids)

    def __str__(self):
        return self.product

Why Cart function doesn’t work?

I want my output here.

<hr>
    {% for i in products %}
        {{i.product}}
    {% endfor %}
<hr>

Here product is in my model