程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Introduction to get request and post request of Django framework

編輯:Python

List of articles

  • Request and response
  • GET Request and POST request

Request and response

Request means that the browser passes HTTP The data sent to the server by the protocol , Response refers to the data returned to the browser after the server receives the request and performs corresponding processing

GET Request and POST request

In request , The most common and important requests are GET Request and POST Request the , Each has its own advantages , The former is faster , The latter is safer .

because Django There are certain safety protection measures , At present, for the convenience of seeing POST Request , You can turn off django Of csrf verification .
In profile setting.py Lieutenant general csrf Just verify the comments

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

The following is a small example of data transmission and printing .

urls.py:

from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path("test_get_post", views.test_get_post),
]

views.py:

from django.http import HttpResponse
POST_FORM = ''' <form method="post" action="/test_get_post"> user name :<input type="text" name="uname"> <input type="submit" value=" Submit "> </form> '''
def test_get_post(request):
if request.method == "GET":
print(request.GET.get("a", "no a"))
print(request.GET.get("c", "no c"))
print(request.GET.getlist("a"))
return HttpResponse(POST_FORM)
elif request.method == "POST":
# Process user submitted data 
print(request.POST["uname"])
print(request.POST.get("c", "no c"))
print(request.POST.getlist("uname"))
return HttpResponse("post is ok")
else:
pass
return HttpResponse("test get post")

Configure access
http://127.0.0.1:8000/test_get_post?a=100&c=5505
obtain :

Then background output :


You can see that you have received a and b Value
Then type something casually in the text box

Click on the submit :

Background output :

You can see that the input data has been received .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved