Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to Pass variable in link to the django view

I have a html form which display data using a for loop.

<tbody id="table">
    {% for sku, lid, stk, mrp, sp, stts in product_data %}
    <tr>
        <td>
            <a class="btn-link" href="/product/product.html" value="{{sku}}">{{sku}}</a>
        </td>
        <td>{{lid}}</td>
        .....

This code prints data in a table using a for loop with a link in first colyumn of table.
The link points to a new page where i want some data to be displayed.
Now The displayed data is dynamically generated from a mongodb database. I want when i click on the link it pass the value as a parametre to a django view so can fetch data which contains the parametre and show it on the next page. How to do that?

My views.py:

from django.shortcuts import render
from django.http import HttpResponse
from inventory.models import GetProductData

def inventory(request):
    pd = GetProductData().skuData()
    sku = pd[0]
    listing_id = pd[1]
    stock_count = pd[2]
    mrp = pd[3]
    status = pd[5]
    selling_price = pd[4]

    product_data = zip(sku, listing_id, stock_count, mrp, selling_price, status)
    context_dict = {'product_data':product_data}
    return render(request, 'inventory/inventory.html', context_dict)

def product(request):
    return render(request, 'inventory/product.html')
like image 702
Manish Gupta Avatar asked Sep 21 '15 08:09

Manish Gupta


People also ask

How do you pass variables from Django view to a template?

And this is rather simple, because Django has built-in template modules that makes a transfer easy. Basically you just take the variable from views.py and enclose it within curly braces {{ }} in the template file.

How do I link views and urls in Django?

A request in Django first comes to urls.py and then goes to the matching function in views.py. Python functions in views.py take the web request from urls.py and give the web response to templates. It may go to the data access layer in models.py as per the queryset.


2 Answers

First of all, its not advisable to use html name when you add a url. Instead of having

href="/product/product.html"

you could have just had something like

href="/product/"

so in your urls.py you should have it defined as below

url(r'^product/$', product),

where 'product ' is the corresponding view handling that request.

Now if you want to send some parameters to django from the html

render your template as below

<tbody id="table">
    {% for sku, lid, stk, mrp, sp, stts in product_data %}
    <tr>
        <td>
            <a class="btn-link" href="/product/?sku={{ sku }}">{{sku}}</a>
        </td>
        <td>{{lid}}</td>
        .....

and in your view i.e; at products

def product(request):
    if request.method=='GET':
        sku = request.GET.get('sku')
        if not sku:
            return render(request, 'inventory/product.html')
        else:
            # now you have the value of sku
            # so you can continue with the rest
            return render(request, 'some_other.html')
like image 127
Manjunath Satyamurthy Avatar answered Sep 30 '22 19:09

Manjunath Satyamurthy


It can be done in 2 ways, using get and using URL parameter.

Using get is simply and flexible, but will lead to ugly URLs:

            <a class="btn-link" href="/product/product.html?parameter={{ your_parameter_here }}" value="{{sku}}">{{sku}}</a>

And inside view, you can access it like this:

def product(request):
    your_parameter = request.GET['parameter']
    return render(request, 'inventory/product.html')

Using url parameters is much better way, it is also more DRY than get parameters (especially when not using {% url %} tag with get).

Your urls.py should look like this:

urlpatterns = [
    url(r'^product/product-(?P<parameter>[\w-]+).html', 'views.product', name="product"),
]

Getting that parameter in view:

def product(request, parameter): # it's just passed as kwarg into view
    return render(request, 'inventory/product.html')

and creating URL in your template:

            <a class="btn-link" href="{% url "product" parameter=your_parameter_here %}" value="{{sku}}">{{sku}}</a>
like image 39
GwynBleidD Avatar answered Sep 30 '22 19:09

GwynBleidD