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/")

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值