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.

aniket
  • 95
  • 5
  • 12
  • 2
    Get values from the Request - `request.POST.values()` . and Get keys(input name) from the Request- `request.POST.keys()`. – Vivek Sable May 18 '15 at 10:41
  • two more questions... request.POST.values will be in list? and can two inputs have same names? – aniket May 18 '15 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.) – Vivek Sable May 18 '15 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? – aniket May 18 '15 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())` – Vivek Sable May 18 '15 at 11:11

3 Answers3

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())

Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
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')
catavaran
  • 44,703
  • 8
  • 98
  • 85
  • So having more than one inputs with same name will not create any problem. – aniket May 18 '15 at 11:43
  • 2
    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). – catavaran May 18 '15 at 12:03
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.

Community
  • 1
  • 1
Wtower
  • 18,848
  • 11
  • 103
  • 80
  • Reason I didn't use Djnago Form is that I am not very comfortable with it. – aniket May 18 '15 at 11:13
  • 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! – Wtower May 18 '15 at 11:15
  • 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? – aniket May 18 '15 at 11:20
  • 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. – Wtower May 18 '15 at 11:23
  • @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? – Vivek Sable May 18 '15 at 11:34
  • my real problem is solved.If I try with forms class and what I meant in my last comment.if in html form somebody fills input fields and then may get more input fields which might vary from person to person and forms.py will give fixed number of input fields? – aniket May 18 '15 at 11:38
  • @user215391: So every value from the template have respective colunm in DB, so Can following will work for you, `request.POST.get("n_13", "")` if `n_13` not in Request then empty string is set or do `if "n_13" in request.POST: obj.col13 = equest.POST["n_13"]` – Vivek Sable May 18 '15 at 12:12
  • @VivekSable actually I made only one column. in that I was joining all the inputs in one string separated by commas, like:"first input ,second input ,third input ,..." . this may not be best way of doing it. – aniket May 18 '15 at 13:38
  • @user215391: ok then `",".join(request.POST.values())` will work, but best to save dictionary means key and value – Vivek Sable May 18 '15 at 13:50