Django——请求与响应
文章目录
一、请求和响应流程
- 浏览器输入网址
- 经过TCP/IP协议创建连接
- 请求页面
- 通过路径找到对应的视图函数
- django创建HttpRequest对象(该对象包含了关于请求的源数据)
- 后台处理
- 视图返回一个HttpResponse对象
二、HttpRequest对象
服务器接收到http协议的请求后,会根据报文创建HttpRequest对象视图函数的第一个参数是HttpRequest对象在django.http模块中定义了HttpRequest对象的API
1.属性
注:is_agax
在3.1已经移除
2. debug查看
要在pycharm中查看对象,可以使用debug模式。
3. Form中的get和post
在HTML中,form表单的作用是收集标签中的内容,<form>...</form>
中间可以由访问者添加类似于文本,选择,或者一些控制模块等等.然后这些内容将会被送到服务端。
一个表单必须指定两样东西:
- form的method参数用于设置表单的提交方式,默认使用POST.
- action用于设置表单的提交url,如果不写或者保持空字符串,那么将使用当前的URL.
3-1 get
Templates/Student/login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="{% url 'students:login' %}" method="get">
<p>username: <input type="text" name="username" placeholder="pleacs put username"></p>
<p>password: <input type="password" name="password" placeholder="pleacs put password"></p>
<p><input type="submit" value="login"></p>
</form>
</body>
</html>
Student/views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def login(request):
return render(request, 'Student/login.html')
如果提交方式是get,那么:
1.get提交的参数会在url中显示.
2.可以通过request.GET.get的方法来获取提交的参数.