I have listed all the vendors (users which are staff) in my django tempelate using the following code. In tempelates:
{% for stf in staff %}
<li><a href="{% url 'vendor_products' %}">{{stf.username}}</a></li>
{% endfor %}
In views:
def vendor_products(request):
vend_prod = Product.objects.filter(user_id=request.user.id)
context={
'vend_prod': vend_prod
}
return render(request, "vendor_products.html", context)
The usernames of the suppliers(vendors) is already listed. What I want is the products of the respective vendor will be dispalyed in a separate html page when the vendor username is clicked.
Now the above code is displaying just the products of the user who is logged in.
Here is the products model incase it is needed.
class Product(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
title = models.CharField(max_length=150)
keywords = models.CharField(max_length=255)
description = models.TextField(max_length=255)
image = models.ImageField(null=False, upload_to='images/')
price = models.DecimalField(max_digits=12, decimal_places=2,default=0)
amount = models.IntegerField(default=0)
detail = RichTextUploadingField()
Thank you in advance!
CodePudding user response:
You define a function that takes the primary key (or another unique field like the username) as parameter:
def vendor_products(request, pk):
vend_prod = Product.objects.filter(user_id=pk)
context={
'vend_prod': vend_prod
}
return render(request, 'vendor_products.html', context)
In the urls.py
you add the parameter to the url:
urlpatterns = [
path('my/path/<int:pk>/', vendor_products, name='vendor_products')
]
and then you pass the value for the primary key when determining the URL:
{% for stf in staff %} <li><a href="{% url 'vendor_products' stf.pk %}">{{ stf.username }}</a></li> {% endfor %}