2

I am new to django so it could be a very basic problem.
In my django template form, I have put a lot of input fields to fill
like this

<form method="POST">
        <input type="text" name="n_1" value="" />
        <input type="text" name="n_2" value="" />
        <input type="text" name="n_3" value="" />
        .
        .
        .
        <input type="text" name="n_ " value="" />
    <input type="submit"  value="submit" />
</form>

To access all inputs,I can do it one by one like asking request.POST["n_i"] by varying i in loop.
I am looking to find a way by which I can get all the values in a list or in string and I don't have to look by name of input field.

5
  • 2
    Get values from the Request - request.POST.values() . and Get keys(input name) from the Request- request.POST.keys(). Commented May 18, 2015 at 10:41
  • two more questions... request.POST.values will be in list? and can two inputs have same names? Commented May 18, 2015 at 10:47
  • 1. yes, it return list. 2.yes, in template we can set same names to input tags, but during URL call Only One Value (may be last) from the same input tags is passed to Request and Python view get only one value becuase its key and value mapping. So Do not use same input name in the template.(In Redio button case we can use.) Commented May 18, 2015 at 10:52
  • So if I don't give any name it will work. But it will be hard to find which value is from which input field? Commented May 18, 2015 at 11:03
  • No, It will not work when you do not give name ti input tag. 2. do zip(request.POST.keys(), request.POST.values()) Commented May 18, 2015 at 11:11

3 Answers 3

3

Get form values into Django View:

Get values from the Request - request.POST.values()

Get keys(input name) from the Request- request.POST.keys()


Get all keys and values from the request in dictionary:

zip(request.POST.keys(), request.POST.values())

Sign up to request clarification or add additional context in comments.

Comments

2

You can iterate through the whole request.POST and get the values of the text fields:

values = [value for name, value in request.POST.iteritems()
                if name.startswith('n_')]

startswith() is required to filter out the submit value of the button.

But imho the better option is to use the inputs with the same name and get the values with the getlist() method:

<form method="POST">
        <input type="text" name="n" value="" />
        <input type="text" name="n" value="" />
        <input type="text" name="n" value="" />
        .
        .
        .
        <input type="text" name="n" value="" />
    <input type="submit"  value="submit" />
</form>

And the in the view:

values = request.POST.getlist('n')

2 Comments

So having more than one inputs with same name will not create any problem.
Yes. If you have several GET/POST parameters with the same name then getlist() will return a list of all they values. But simple request.POST['n'] will return only the single value (I am not sure the first or the last one).
1

As @Vivek Sable mentioned in his comment, you can use request.POST.values() and request.POST.keys(). Another possibility is to convert the POST dictionary into a list of tuples with request.POST.items().

Apart from those, I would strongly recommend you to consider using a standard Django Form class:

forms.py:

from django import forms

class NameForm(forms.Form):
    your_name = forms.CharField(label='Your name', max_length=100)

template:

<form action="/your-name/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>

Then you would be able to construct the form from the post data as such:

form = NameForm(request.POST)

And after calling form.is_valid() he collected data will be in form.cleaned_data.

More on forms on Django documentation: Working with forms.

9 Comments

Reason I didn't use Djnago Form is that I am not very comfortable with it.
I understand, but after you get a grip on it you will see that it is not very difficult after all and makes things very simple!
Any way suppose I was asking for names to fill but numbers of inputs may vary depending on form-filler, will input create a problem because in forms.py I don't exactly know how many inputs needs to be filled?
Sorry, I didn't understand what you're asking, but I would recommend you that you edit your post and provide more details with your issue.
@user215391: Ok. in view what you are going to do with these values from the templates? means are you coming to save into database or in any files or any other?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.