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 settings.py.

vim /root/py3/django-test1/test3/test3/settings.py

MIDDLEWARE_CLASSES = (
    #'django.middleware.csrf.CsrfViewMiddleware',
    ...
)

To configure view functions:

vim /root/py3/django-test1/test3/booktest/views.py

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://×××w.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Title</title>
</head>
<body>
<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
<br>
    <input type="submit" value="Submission">
</form>
</body>
</html>

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://×××w.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Title</title>
</head>
<body>
User name:{{ uname }}<br>
Password:{{ upwd }}<br>
Gender:{{ ugender }}<br>
Hobbies:{{ uhobby }}<br>
{% for hobby in uhobby %}
{{ hobby }}
{% endfor %}
</body>
</html>


Configure the url of the app:

vim /root/py3/django-test1/test3/booktest/urls.py

from django.conf.urls import url
from . import views
urlpatterns = [
    ...
    url(r'postTest1/$',views.postTest1),
    url(r'postTest2/$',views.postTest2),
]

Start web Service:

cd /root/py3/django-test1/test3/
python manage.py runserver 192.168.255.70:8000

Browser access: http://192.168.255.70:8000/booktest/postTest1/

Fill in the form:

Fill in the form and click Submit. The browser url changes to: http://192.168.255.70:8000/booktest/postTest2/

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