Skip to content Skip to sidebar Skip to footer

How To Retrieve Input Value Of Html Form If Form Is In Django For Loop

I got a situation.I have a django template ip_form.html given bellow.
{% for val in value_list %} {{

Solution 1:

You need to add a name attribute to your inputs, and then you can use this name to retrieve a list of values, using Django QueryDict getlist method:

HTML:

<formmethod="POST">
  {% for val in value_list %}
    <inputtype='text'value='{{ val }}'name='my_list'>{{ val }}</input>
  {% endfor %}
</form>

View:

defview1(request):
    value_list = [1,2,3,4,5] # it will change every time view1 will get requestif request.method == "POST":
        values_from_user = request.POST.getlist('my_list')
    return render ('ip_form.html', 'value_list': value_list)

values_from_user will be a list of input values from your form (or an empty list if form had zero input elements).

Please note that I changed your form method to POST, because otherwise the request.method test in your view would be meaningless.

Post a Comment for "How To Retrieve Input Value Of Html Form If Form Is In Django For Loop"