Django 请求和响应

1、请求

(1)get请求

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

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

在html使用post,login.html

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)请求函数

python 复制代码
# 获取请求方法
print(request.method)

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

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

2、响应

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

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

# 【响应】redirect,重定向
return redirect("https://www.baidu.com/")
相关推荐
m0_59452630几秒前
基于 PyQt5 实现分组列表滚动吸顶效果
开发语言·python·qt
Biehmltym1 分钟前
【SpringMVC】概述 SSM:Spring + SpringMVC + Mybats
java·后端·spring
qw9496 分钟前
SpringMVC
java·后端
customer0825 分钟前
【开源免费】基于SpringBoot+Vue.JS医疗报销系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
B站计算机毕业设计超人29 分钟前
计算机毕业设计SpringBoot+Vue.jst房屋租赁系统(源码+LW文档+PPT+讲解)
vue.js·spring boot·后端·eclipse·intellij-idea·mybatis·课程设计
thinkMoreAndDoMore1 小时前
深度学习(3)-TensorFlow入门(常数张量和变量)
开发语言·人工智能·python
kngines1 小时前
【Python量化金融实战】-第1章:Python量化金融概述:1.4 开发环境搭建:Jupyter Notebook、VS Code、PyCharm
python·量化金融
kngines1 小时前
【Python量化金融实战】-第1章:Python量化金融概述:1.2 Python在量化金融中的优势与生态
python·量化金融
wapicn991 小时前
‌挖数据平台对接DeepSeek推出一键云端部署功能:API接口驱动金融、汽车等行业智能化升级
java·人工智能·python·金融·汽车·php
zidea1 小时前
AI 长篇文本生成面临的挑战,以及解决方案(1)
人工智能·python·deepseek