【7.0】Django框架入门之request对象

【一】request对象引入

【1】提交GET请求

(1)前端

  • form表单中action属性,不写默认是当前路由地址
  • form表单中的method属性,不写默认是GET请求
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="" method="get">
    <p>username : <input type="text" name="username"></p>
    <p>password : <input type="password" name="password"></p>
    <p><input type="submit"></p>
</form>
</body>
</html>

(2)后端

  • app01/views.py
from django.shortcuts import render

def register(request):

    return render(request, 'register.html')
  • urls
from django.contrib import admin
from django.urls import path
from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path("register/", views.register)
]

【2】提交POST请求

(1)前端

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="" method="post">
    <p>username : <input type="text" name="username"></p>
    <p>password : <input type="password" name="password"></p>
    <p><input type="submit"></p>
</form>
</body>
</html>

(2)后端

from django.shortcuts import render

def register(request):

    return render(request, 'register.html')

(3)POST请求报错

  • 当前端form表单发送POST请求时,会报一个错,导致后端崩溃
Forbidden (403)
CSRF verification failed. Request aborted.

You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hijacked by third parties.

If you have configured your browser to disable cookies, please re-enable them, at least for this site, or for “same-origin” requests.

(4)解决办法

  • 将settings.py文件中的配置注释掉,在后面的学习中我们会逐步的解决掉这个问题
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',
]

【二】request对象属性和方法

【1】request.method

  • 获取发起请求的请求方式
  • get请求携带的数据是由大小限制的
  • post请求携带的请求参数没有限制

【2】request.POST

  • 获取用户输入的请求数据,但不包含文件
  • 返回的是:大写字符串

【3】get/getlist

  • get 只会获取列表最后一个元素
  • getlist 直接将列表取出(多选项)

【4】示例

(1)前端

  • register.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="" method="post">
    <p>
        username : <input type="text" name="username">
    </p>
    <p>
        password : <input type="password" name="password">
    </p>
    <p>
        hobby :
        music <input type="checkbox" name="hobby" value="music">
        sport <input type="checkbox" name="hobby" value="sport">
        dance <input type="checkbox" name="hobby" value="dance">
    </p>
    <p><input type="submit"></p>
</form>
</body>
</html>
  • login.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form action="">
    <p>
        username : <input type="text" name="username">
    </p>
    <p>
        password : <input type="password" name="password">
    </p>
    <p><input type="submit"></p>
</form>
</body>
</html>

(2)后端

  • app01/views.py
from django.shortcuts import render, HttpResponse, redirect


def register(request):
    # 【一】如果是正常访问页面,发送的请求是 GET 请求
    print(request.method)  # GET
    # 【二】如果是通过表单提交数据,发送的请求是 POST 请求
    if request.method == 'POST':
        print(request.method)  # POST
        # 【三】从POST请求中获取到用户输入的参数
        data = request.POST
        print(data)  # <QueryDict: {'username': ['dream'], 'password': ['1314521'], 'hobby': ['sport', 'dance']}>
        # 【四】从POST请求中提取出参数
        username = data.get('username')
        password = data.get('password')
        hobby = data.get('hobby')
        print(username)  # dream
        print(password, hobby)  # 1314521
        print(hobby)  # ['sport', 'dance']
        return redirect(to='/login/')
    # 发送GET请求,则渲染注册页面
    return render(request, 'register.html')


def login(request):
    # 【一】获取到当前请求方式
    print(request.method)
    # 【二】获取到当前GET请求携带的参数
    data = request.GET
    print(data)  # <QueryDict: {'username': ['dream'], 'password': ['521']}>
    if data:
        username = data.get("username")
        password = data.get("password")
        if username == "dream" and password == "1314521":
            return HttpResponse("登录成功")
        else:
            return HttpResponse("用户名或密码失败")
    return render(request, 'login.html')
  • urls.py
from django.contrib import admin
from django.urls import path
from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path("register/", views.register),
    path("login/", views.login)
]
posted @ 2024-02-27 08:24  Chimengmeng  阅读(55)  评论(0编辑  收藏  举报
/* */