pap
pap

Reputation: 33

Passing value from template to django view

I have a template in which I display some values. Now I want to be able to change that values. So I have an input field with the value "capacity.capacity". The user can now change this value in the input field and click on button "Change". Now I want to process the newly entered value in my django view. How can I pass the new value to my view?

I tried to add the value in the url so that I can retrieve it in my view again.

Is it possible to add the new value to the url or is there a different/better way to do this?

<td> 
    <input type="number" name=new_capacity id="new_capacity" step="1" value= {{ capacity.capacity }}> 
    <button> 
        <span class="glyphicon glyphicon-edit"></span>
        <a href="{% url 'change_capacity' item_id=capacity.pk new_capa=NEWVALUE %}"> Change
    </button> 
</td>

Update: Full code

I have two forms in this template and two submit buttons. Both buttons should redirect to different views. However it seems that always the same view is called.

{% block body %}

<form action="/producer/capacity/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Hinzufügen" />
</form>
<br/>
<br/>

<div class="container">
    <table class="table">
        <thead>
              <tr>
                <th>Maschine</th>
                <th>Verfügbare Kapazität</th>
              </tr>
            </thead>
            <tbody>
               {% for capacity in capacity_list %}
                    <tr>
                      <td>{{ capacity.pk }}</td>
                      <td>
                          <form action="" method="post">{% csrf_token %}
                          <input type="number" name=new_capacity id="new_capacity" step="1" value="{{capacity.capacity}}" />
                          <input type="submit" value="Change"/>
                          </form>
                      </td>
                    </tr>
              {% endfor %}
            </tbody>
    </table>
</div>

{% endblock %}

Upvotes: 2

Views: 1803

Answers (1)

Jose Luis Barrera
Jose Luis Barrera

Reputation: 361

I think it's better to use POST method to make changes in the model data instead of using GET, so you can write a form like this in your template:

<form action="{% url my_app.views.my_capacity_view %}" method="post">{% csrf_token %}
    <input type="number" name=new_capacity id="new_capacity" step="1" value="{{capacity.capacity}}" />
    <input type="submit" value="Change" />
</form>

You can set the action form url in order to send the field to a custom view to modify the capacity value. Then, in the view, you can get the value and make the change, for example:

def my_capacity_view(request):
    if request.method == 'POST':        
        new_capacity = request.POST.get('new_capacity ', '')
        # set new value 'new_capacity' to the object

Set the url to point to the view

(r'^newcapacity', 'my_app.views.my_capacity_view'),

I hope this was useful

Upvotes: 2

Related Questions