I am implementing an app in which a User can create a Shopping List and can search for a Product and add the Product to the Shopping List.
I am stuck at the 'how to add to the list' part. I have created the Model as follows:
class Product(models.Model):
pid = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100, db_index=True)
description = models.TextField(blank=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_detail', args=[self.pid, self.slug])
class ShoppingList(models.Model):
user = models.ForeignKey(User, related_name='shoplist', on_delete=models.CASCADE)
list_name = models.CharField(max_length=20)
items = models.ManyToManyField(Product)
slug = models.SlugField(max_length=150, db_index=True)
def __str__(self):
return self.list_name
def get_absolute_url(self):
return reverse('shop:item_list', args=[self.slug])
In the Template to view each Product, I have the following
<h3>{{ product.name }}</h3>
<a href="(What is to be written here)">ADD</a>
I need the Particular product which I am displaying through the get_absolute_url, to be Added to the Shopping List, when the ADD button is cliked.
I am lost as to what Steps to take next. Please help.