Home > Blockchain >  How to get the value of input on button click in django
How to get the value of input on button click in django

Time:10-27

I have a django template as follows

<table class="table">
    <thead>
        <tr>
            <th>#</th>
            <th>Master Title</th>
            <th>Retailer title</th>
            <th>To add</th>
        </tr>
    </thead>
    <tbody>
        {% if d %}
        {% for i in d %}
        <tr>
            <th scope="row">{{forloop.counter}}</th>
            <td>{{i.col1}}</td>
            <td>{{i.col2}}</td>
            <td>
                <input type="hidden" name='row_value' value="{{i.col1}}|{{i.col2}}">
                <a class='btn btn-success' href="{% url 'match' %}">Match</a>
                <a class='btn btn-outline-danger' href="{% url 'mismatch' %}">Not a Match</a>
            </td>
        </tr>
        {% endfor %}
        {% endif %}
    </tbody>
</table>

When the match button is clicked, I want to retireve the value of the hidden input in the views. Here is my views.py

def match(request):
    print(request.GET.get('row_value'))
    print('match')

But this returns None.

I want the values of col1 and col2 to be printed as follows

col1|col2

I am not sure where I am going wrong.

CodePudding user response:

You should use Django URL params with GET:

urls.py

   urlpatterns = [
       path('your-path/<int:first_param>/<int:second_param>', views.match),
   ]

source: https://docs.djangoproject.com/en/3.2/topics/http/urls/#example

views.py

def match(request, first_param=None, second_param=None):
    # then do whatever you want with your params
    ...

source: https://docs.djangoproject.com/en/3.2/topics/http/urls/#specifying-defaults-for-view-arguments

template:

        <td>
            <a class='btn btn-outline' href="your-path/{{i.col1}}/{{i.col2}}">Example</a>
        </td>

This is a basic example, so change it to your use case.

  • Related