django's browser uses POST to submit forms, and the back end obtains data

The environment is the same as the previous django article.

Note: use form submission to comment out the middleware crsf in

vim /root/py3/django-test1/test3/test3/


To configure view functions:

vim /root/py3/django-test1/test3/booktest/

def postTest1(request):
    return render(request,'booktest/postTest1.html')
def postTest2(request):
    username = request.POST['uname']
    userpasswd = request.POST['passwd']
    usergender = request.POST.get('ugender')
    userhobby = request.POST.getlist('uhobby')
    context = {'uname':username,'upwd':userpasswd,'ugender':usergender,'uhobby':userhobby}
    return render(request,'booktest/postTest2.html',context)

Add html template:

vim /root/py3/django-test1/test3/templates/booktest/postTest1.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://×××">
<form method="post" action="/booktest/postTest2/">
User name:<input type="text" name="uname"><br>
Password:<input type="password" name="upasswd"><br>
Gender:<input type="radio" name="ugender" value="male" checked="checked">male<input type="radio" name="ugender" value="female">female<br>
Hobbies:<input type="checkbox" name="uhobby" value="Bodybuilding">Bodybuilding
      <input type="checkbox" name="uhobby" value="Basketball">Basketball
      <input type="checkbox" name="uhobby" value="skiing">skiing
    <input type="submit" value="Submission">

Note: the name attribute of all input elements in the html form will be used as the key, and the value attribute will be used as the value. After submitting, it will be passed to the backend request.POST ['key'] to receive.

vim /root/py3/django-test1/test3/templates/booktest/postTest2.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://×××">
User name:{{ uname }}<br>
Password:{{ upwd }}<br>
Gender:{{ ugender }}<br>
Hobbies:{{ uhobby }}<br>
{% for hobby in uhobby %}
{{ hobby }}
{% endfor %}

Configure the url of the app:

vim /root/py3/django-test1/test3/booktest/

from django.conf.urls import url
from . import views
urlpatterns = [

Start web Service:

cd /root/py3/django-test1/test3/
python runserver

Browser access:

Fill in the form:

Fill in the form and click Submit. The browser url changes to:

You can open the browser developer debugging mode to view the form data:

Demo complete.

Tags: Web Development Django vim Attribute Python

Posted on Wed, 04 Dec 2019 08:11:22 -0800 by misty