I am using this method inside Product
Class get_add_to_basket_url
to add item to basket but link not working, hope anyone can help with this.
this is the Link address from Add To Cart Button
http://localhost:8000/products/product/%7B(%20product.get_add_to_basket_url%20)%7D
Error
Page Not Found 404 for this Url above
html
<div class="extra content">
<span class="right floated">
<i class="money bill alternate icon"></i>
Final Price: {{ product.price }}
</span>
{% if product.before_price %}
<span>
<i class="money bill alternate outline icon"></i>
Old Price: <del class="DublStrk">{{ product.before_price }}</del>
</span>
{% endif %}
</div>
<div class="extra content">
<a class="ui fluid button" href="{( product.get_add_to_basket_url )}">Add To Basket</a>
</div>
Method in Product Model
from django.urls import reverse_lazy
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(f'{self.name}-{self.sku}')
super(Product, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse_lazy('Products:ProductPageUrl', kwargs={'slug': self.slug})
def get_add_to_basket_url(self):
return reverse_lazy('Baskets:ItemAddToBasketUrl', kwargs={'slug': self.slug})
def __str__(self):
return f'{self.name} - {self.sku}'
Baskets Urls.py
app_name = 'Baskets'
urlpatterns = [
path('add-to-basket/<slug:slug>', views.ItemAddToBasketView, name='ItemAddToBasketUrl'),
]
Baskets Views.py
def ItemAddToBasketView(request, slug):
item = Product.objects.filter(slug=slug)
order_item, created = OrderItem.objects.get_or_create(item=item, account=request.user, ordered=False)
order_qs = Order.objects.filter(account=request.user, ordered=False)
if order_qs.exists():
order = order_qs[0]
if order.items.filter(item__slug=item.slug).exists():
order_item.quantity += 1
order_item.save()
message.success(request, f'{item} added Quantity is Updated in Basket.')
return redirect('Products:ProductPageUrl')
else:
order.items.add(order_item)
order.save()
message.success(request, f'{item} added to Basket.')
return redirect('Products:ProductPageUrl')
else:
order = Order.objects.create(account=request.user, ordered=False, ordered_at=ordered_at)
order.items.add(order_item)
order.save()
return redirect('Products:ProductPageUrl')