10

Hello everyone I have an HTML form as follows:

<body class="">
    <div class="navbar navbar-static-top navbar-inverse"></div>
    <div style="height:20%; width:30%; margin-top:12%; margin-left:40%;">
        <div class="list-group">
            <form role="form">
            <br/>
                <div class="form-group input-group">
                    <span class="input-group-addon"><i class="fa fa-circle-o-notch"  ></i></span>
                    <input type="text" name="Username" class="form-control" placeholder="Username" />
                </div>
                <div class="form-group input-group">
                    <span class="input-group-addon"><i class="fa fa-tag"  ></i></span>
                    <input type="text" name="first_name" class="form-control" placeholder="FIRST_NAME" />
                </div>
                <div class="form-group input-group">
                    <span class="input-group-addon"><i class="fa fa-tag"  ></i></span>
                <input type="text" name="last_name" class="form-control" placeholder="LAST_NAME" />
                </div>
                ...
                ...
                <a href="/submit" class="btn btn-success ">POST</a>
            </form>
        </div>
    </div>
</body>

and after clicking on post i am redirecting it to views.py. can any one tell me how to get the field values of all the fields of the form into views.py. thanks in advance

Pavel Antspovich
  • 1,111
  • 1
  • 11
  • 27

2 Answers2

20

Each input should have a name attribute:

<input type="text" name="username" class="form-control"
                                      placeholder="Desired Username" />

And then in your view you can access that data using request.POST or request.GET dictionary-like objects (it depends on the method attribute of the <form> tag):

def register(request):
    username = request.POST['username']
    ...

But you shouldn't render/handle forms this PHPish way. Learn the django forms.

catavaran
  • 44,703
  • 8
  • 98
  • 85
  • Request parameters are case-sensitive, so you should access the username as `request.GET['Username']`. `GET` is the default request method for forms. To change it to `POST` add the `
    ` attribute.
    – catavaran Mar 24 '15 at 09:26
  • how to check whether i got values in or not –  Mar 24 '15 at 09:45
  • suppose i want to display the information what i got in views into other html page, is it possible –  Mar 24 '15 at 09:46
  • Yes, add the following line to the `register()` function: `return render(request, 'mytemplate.html', {'username': username})` and then in the `mytemplate.html` output the value with `{{ username }}` tag. Look at the tutorial: https://docs.djangoproject.com/en/1.7/intro/tutorial03/#a-shortcut-render – catavaran Mar 24 '15 at 09:52
  • CSRF verification failed. Request aborted. error occuring –  Mar 24 '15 at 09:59
  • This is a completely different question. Please close up this question and ask a new one if you're still having problems. – rnevius Mar 24 '15 at 10:04
  • i have added {% csrf_tork %} but no use –  Mar 24 '15 at 10:13
  • Oops, mistype. Add the csrf token after the
    tag:
    {% csrf_token %}. See the example here: docs.djangoproject.com/en/1.7/ref/contrib/csrf/#how-to-use-it
    – catavaran Mar 24 '15 at 10:22
7

To catch the data in python backend

for POST request:

def register(request):
    username = request.POST.get('username')

for GET request

def register(request):
    username = request.GET.get('username')

.get was missing on earlier answer

Mohit Khandelwal
  • 541
  • 6
  • 14
cryptoKTM
  • 2,593
  • 22
  • 21