Django 请求和响应

1、请求

(1)get请求

用户直接在浏览器输入网址,参数直接在url中携带

http://127.0.0.1:8000/login/?a=1&b=%221243%22
(2)post请求

在html使用post,login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>用户登录</h1>
<form method="post", action="/login/">
<!--  {% csrf_token %} 必须添加这个,否则报错Forbidden  -->
    {% csrf_token %}
    <input type="text" name="user" placeholder="用户名">
    <input type="password" name="passward" placeholder="密码">
    <input type="submit" value="提交">
    {{error_message}}
</form>
</body>
</html>

(3)请求函数

# 获取请求方法
print(request.method)

# 获取get请求的参数
print(request.GET)

# 获取post请求的参数
print(request.POST)

2、响应

(1)HttpResponce
(2)render
(3)redirect
# 【响应】HttpResponse("返回内容“),返回内容给请求者
return HttpResponse("heool")

# 【响应】 render(request对象,返回的静态页面,页面中的模板符号)
return render(request, 'something.html', {"title":"你好"})

# 【响应】redirect,重定向
return redirect("https://www.baidu.com/")

相关推荐

  1. Django请求响应

    2024-07-18 09:32:05       39 阅读
  2. django框架、断言、请求响应

    2024-07-18 09:32:05       58 阅读
  3. Django响应‘表单请求’过程

    2024-07-18 09:32:05       27 阅读
  4. selenium获取请求响应

    2024-07-18 09:32:05       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 09:32:05       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 09:32:05       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 09:32:05       58 阅读
  4. Python语言-面向对象

    2024-07-18 09:32:05       69 阅读

热门阅读

  1. ios 设置行距和获取文本行数

    2024-07-18 09:32:05       22 阅读
  2. (86)组合环路--->(01)RGB值

    2024-07-18 09:32:05       19 阅读
  3. 详细说一下axios的特点

    2024-07-18 09:32:05       23 阅读
  4. log4j.appender.Logfile.File=./logs/its_log

    2024-07-18 09:32:05       20 阅读
  5. 七、python函数基础

    2024-07-18 09:32:05       20 阅读
  6. Junit单元测试常用断言

    2024-07-18 09:32:05       25 阅读
  7. app自动化测试缓存问题如何解决?

    2024-07-18 09:32:05       21 阅读
  8. 【Go系列】Go语言的测试

    2024-07-18 09:32:05       22 阅读
  9. OPPO 2024届校招正式批笔试题-后端(C卷)

    2024-07-18 09:32:05       23 阅读