I'm starting to learn Django and I ran into this problem: I can't add parameters to the link
Example:
Link before the change:
http://127.0.0.1:8000/?brand=&search=
Link after the change:
http://127.0.0.1:8000/?brand=&search=&sort=
What I get:
http://127.0.0.1:8000/?sort=
How to implement it?
views.py
def filters(request):
#search
search_post = request.GET.get('search', '')
if search_post:
all = Product.objects.filter(Q(title__icontains=search_post) & Q(content__icontains=search_post)).order_by()
else:
all = Product.objects.all()
#sort by price
sort_by = request.GET.get("sort", '')
if sort_by == "l2h":
all = Product.objects.all()
all = all.extra(order_by = ['-price'])
elif sort_by == "h2l":
all = Product.objects.all().order_by('price')
filters = IndexFilter(request.GET, queryset=all)
context = {
'filters': filters
}
return render(request, 'index.html', context)
urls.py
from django.urls import path
from .views import *
urlpatterns = [
path('', filters, name='filters')
]
index.html
<form method="get" action="{% url 'filters' %}">
{{ filters.form }}
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" name="search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
<a class="filter_by" href="?sort=l2h">Price:--low to high</a>
<a class="filter_by" href="?sort=h2l">Price:-- high to low</a>
</form>