欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > Django 请求和响应

Django 请求和响应

2024/10/24 1:53:01 来源:https://blog.csdn.net/qq_43251445/article/details/140508848  浏览:    关键词: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/")

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com