mosi
mosi

Reputation: 39

how can I fetch data from database in django with parameter in url?

** URl : **

path('products_filter/', views.products_filter, name='products_filter'),

** VIEW :**

def products_filter(request):
    product = request.GET.get('product')
    selling = request.GET.get('selling')
    
    products = Product.objects.filter(selling='best_seller')

    return render(request, 'product/products.html', {'products':products})

** TEMPLATE : **

<a href="/product/products_filter?product={{'carpet'|urlencode}}&selling={{'best_seller'|urlencode}}">

Upvotes: 0

Views: 575

Answers (1)

Vincent
Vincent

Reputation: 1564

add the parameters you want to retrieve from the url to your url and make sure the url in the template adds them. For example:

urls.py:

path('products_filter/<str:product>/<str:selling>/', views.products_filter, name='products_filter'),

template:

<a href="{% url 'products_filter' product=carpet|urlencode selling=best_seller|urlencode %}">

Upvotes: 1

Related Questions