request.method,request.POST,request.GET


登錄注冊

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    {% load static %}
    <link rel="stylesheet" href="{% static 'bootstrap-3.4.1-dist/css/bootstrap.min.css' %}">
    <script src="{% static 'bootstrap-3.4.1-dist/js/bootstrap.min.js' %}"></script>
</head>
<body>
<h1 class="text-center">登錄</h1> <!--居中-->
<div class="container">
    <div class="row">
        <div class="col-md-7 col-md-offset-2">
            <form action="">
                <p>username:<input type="text" name="username" class="form-control"></p>
                <p>password:<input type="text" name="password" class="form-control"></p>
                <input type="submit" class="btn-block">
            </form>
        </div>
    </div>
</div>
</body>
</html>

image

未指定提交方式,默認按照get方式。

from表單action參數

  1. 不寫,默認向當前所在url提交數據
  2. 全寫,指名道姓
  3. 只寫后綴/自定義urls對應關系/

在前期我們使用django提交post請求的時候 需要取配置文件中注釋掉一行代碼

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',
]

image

無論是post請求還是get請求都將觸發login視圖函數。

get請求和post請求應該有不同的處理機制

  • get請求只需獲取頁面
  • post請求獲取用戶數據

request對象方法初始

請求相關的數據對象,里面有很多簡易的方法

如何獲取當前請求方式: request.method

from django.shortcuts import render, HttpResponse, redirect


# Create your views here.
# 視圖函數必須接收一個形參(request)
def login(request):
    """
    get請求和post請求應該有不同的處理機制
    get請求只需獲取頁面
    post請求獲取用戶數據
    :param request:
    :return:
    """
    print(request.method)
    if request.method == 'POST':
        return HttpResponse("收到")
    return render(request,'login.html')

返回請求方式,並且是全大寫的字符串方式 <class 'str'>

![截屏2022-02-25 23.22.59](/Users/macintoshhd/Desktop/截屏2022-02-25 23.22.59.png)

獲取用戶數據 : request.POST 與 request.GET

獲取用戶post請求提交的普通數據,不包含文件。

from django.shortcuts import render, HttpResponse, redirect


# Create your views here.
# 視圖函數必須接收一個形參(request)
def login(request):
    """
    get請求和post請求應該有不同的處理機制
    get請求只需獲取頁面
    post請求獲取用戶數據
    :param request:
    :return:
    """
    print(request.POST)
    print(request.method)
    if request.method == 'POST':
        return HttpResponse("收到")
    return render(request,'login.html')

服務端返回客戶端請求數據:request.POST.get()

字典的形式,鍵取決於前端name屬性

[25/Feb/2022 15:29:40] "GET /login/ HTTP/1.1" 200 763
<QueryDict: {}>
GET
<QueryDict: {'username': ['junjie'], 'password': ['123']}> # 字典
POST
[25/Feb/2022 15:29:42] "POST /login/ HTTP/1.1" 200 6

並且獲取的數據類型均為字符串(str)

[25/Feb/2022 15:35:15] "GET /login/ HTTP/1.1" 200 764
<QueryDict: {'username': ['junjie'], 'password': ['123']}>
POST
junjie <class 'str'>
[25/Feb/2022 15:35:16] "POST /login/ HTTP/1.1" 200 6

再次驗證

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    {% load static %}
    <link rel="stylesheet" href="{% static 'bootstrap-3.4.1-dist/css/bootstrap.min.css' %}">
    <script src="{% static 'bootstrap-3.4.1-dist/js/bootstrap.min.js' %}"></script>
</head>
<body>
<h1 class="text-center">登錄</h1> <!--居中-->
<div class="container">
    <div class="row">
        <div class="col-md-7 col-md-offset-2">
            <form action="" method="post">
                <p>username:<input type="text" name="username" class="form-control"></p>
                <p>password:<input type="text" name="password" class="form-control"></p>
                <p>
                    <input type="checkbox" name="hobby" value="111">111
                    <input type="checkbox" name="hobby" value="222">222
                    <input type="checkbox" name="hobby" value="333">333
                </p>
                <input type="submit" class="btn-block">
            </form>
        </div>
    </div>
</div>
</body>
</html>

views.py

from django.shortcuts import render, HttpResponse, redirect


# Create your views here.
# 視圖函數必須接收一個形參(request)
def login(request):
    """
    get請求和post請求應該有不同的處理機制
    get請求只需獲取頁面
    post請求獲取用戶數據
    :param request:
    :return:
    """
    print(request.POST)
    print(request.method)
    hobby = request.POST.get('hobby')
    print(hobby,type(hobby))
    if request.method == 'POST':
        return HttpResponse("收到")
    return render(request,'login.html')

服務端接收客戶端返回信息

<QueryDict: {}>
GET
None <class 'NoneType'>
[25/Feb/2022 15:40:04] "GET /login/ HTTP/1.1" 200 1020
<QueryDict: {'username': [''], 'password': [''], 'hobby': ['111', '222', '333']}>
POST
333 <class 'str'>
[25/Feb/2022 15:40:04] "POST /login/ HTTP/1.1" 200 6

由此得出:request.POST.get() ,只獲取列表的最后一個元素

直接將列表取出 : request.POST.getlist()

username = request.POST.getlist('username')
print(username,type(username))
GET
[] <class 'list'>
<QueryDict: {'username': ['junjie'], 'password': ['123'], 'hobby': ['111', '222', '333']}>
POST
['111', '222', '333'] <class 'list'>

獲取用戶提交的get請求數據: request.GET

request.GET.get()  # 只獲取列表最后一個元素
request.GET.getlist()  # 直接將列表取出
"""
get請求攜帶的數據是有大小限制的 大概好像只有4KB左右
而post請求則沒有限制
"""


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM