1

I have an HTML file which returns multiple GET values to my django view.py file.

If I submit only one value to view.py and read it with:

request.GET('variable')

then it works fine. But I am not sure how to GET multiple values.

I tried:

request.GET.getlist('variable1')
request.GET.getlist('variable2')

but it doesn't work.

My HTML file is:

<form action="/hello/" method="GET">
First_Name:<input type="text" name="arr[0]"><br>
Last_Name:<input type="text" name="arr[1]"><br>
<input type = "submit" value = "Submit">
</form>

My view.py file in the django app is:

def hello(request):
  fn=request.GET['arr[0]']
  ln=request.GET['arr[1]')
  print fn
  print ln

I have also tried:

fn=request.Get.getlist['arr[0]']
ln=request.Get.getlist['arr[1]']

What am I doing wrong?

2
  • See: stackoverflow.com/questions/801354/… Commented Jun 16, 2015 at 23:39
  • try <input type="text" name="arr[]"> or <input type="text" name="arr"> Commented Jun 17, 2015 at 5:58

2 Answers 2

1

Drop the square brackets and indexes. Those are from PHP or Rails and have no place in Django.

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

Comments

0

Check this out.

For example,

# views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect

from .forms import NameForm

def get_name(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = NameForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:
            return HttpResponseRedirect('/thanks/')

    # if a GET (or any other method) we'll create a blank form
    else:
        form = NameForm()

    return render(request, 'name.html', {'form': form})

and

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

Comments

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.