Bhanukiran Bhanukiran - 1 month ago 7
Python Question

How to fetch data from link tag in django?

In my models I have a class called "Add_prod",I have created few columns like book,author,price and so on.In the templates I have created two hyperlinks for sorting data in ascending/descending order of price."name" attribute is not supporting in anchor tag.I also tried with id attribute instead of name,but still no use.So how to fetch data from anchor tag so that I arrange products in sorted order.

Views.py,

def welcome_user(request):
if 'low_price' in request.GET:
my_products = Add_prod.objects.all().order_by('price')
elif 'high_price' in request.GET:
my_products = Add_prod.objects.all().order_by('-price')
else:
my_products = Add_prod.objects.all()
context = { "Products":my_products}

#rest of code for other functionalities
return render(request,"welcome-user.html",context)


My template file,

<form>
<div style="text-align: right">
<a name="low_price" href="{% url 'welcome_user' %}">Low Price</a>
<a name="high_price" href="{% url 'welcome_user' %}">High Price</a>
</div>
</form>

AKS AKS
Answer

You need to append the URLs with the query parameters you are expecting in the view:

<a name="low_price" href="{% url 'welcome_user' %}?low_price">Low Price</a>
<a name="high_price" href="{% url 'welcome_user' %}?high_price">High Price</a>
Comments