怎么获取前台提交数据是几兆_Web怎么获取请求数据,修改响应信息呢?
request对象
为什么要知道request对象?因为我们只要了解到request对象
的属性,我们才能获取请求时附带的数据,而这些数据对我们都是很重要的,比如:当我们需要登录校验、返回客户想要的数据
等,我们都可以通过request对象来获取数据来实现我们想要展现的功能。
常用属性和方法
我们在views.py
文件中打印一下这些属性
def login(request):
if request.method == 'GET':
print('这个是GET请求!')
print(request)
print(request.method)
print(request.path)
print(request.GET)
return render(request,'login.html')
else:
print('这个是POST请求!')
print(request.method)
print(request.POST)
print(request.path)
print(request.get_full_path())
print(request.META)
print(request.body)
return render(request,'index.html')
def index(request):
print('这是一个具有多选功能和上传文件的页面请求!')
print(request.POST)
print(request.FILES)
return render(request,'index.html')
urls.py
文件内容如下
urlpatterns=[
re_path(r'^login/',views.login),
re_path(r'^index/', views.index),
]
login.html
文件(具有登录功能)如下
<body>
<form action="/login/" method="post">
<h1>这是一个登录页面h1>
用户名:<input type="text" name="username">
密码:<input type="password" name="password">
性别:<input type="radio" name="sex" value="male">男
<input type="radio" name="sex" value="female">女
<br>
<button>提交button>
form>
body>