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