模拟django查看get和post的区别
目标页面:通过建立本地服务器,通过ip地址加端口号访问页面。
ip地址:http://127.0.0.1:8090/xiaohu
1、首先新建django项目:
2、新建index.html文件。
3、在index.html里面输入下面代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>hello girl</h1> <form action="/xiaohu" method="post"> <p>用户名:<input type="text" name="username" ></p> <p>密码:<input type="password" name="pwd" ></p> <p>爱好:男<input type="radio" name="sex" value="0"></p> <p> 女<input type="radio" name="sex" value="1"></p> <p>爱好:蓝球<input type="checkbox" name="hobby" value="bsk"></p> <p> 足球<input type="checkbox" name="hobby" value="football"></p> <p> 乒乓球<input type="checkbox" name="hobby" value="pingpang"></p> <p><input type="submit" value="发送"></p> </form> </body> </html>
4、在views.py里面输入下面代码:
from django.shortcuts import render # Create your views here. def xiaohu(req): print('前端数据GET',req.GET) print('前端数据POST',req.POST) return render(req,"index.html")
备注:测试post需要设置下面:
5、在Terminal里面输入python manage.py runserver 8090回车
6、在浏览器里面输入:http://127.0.0.1:8090/xiaohu 回车即可。
get请求打印的数据:
post请求打印的数据:
文件上传代码index代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>hello girl</h1> <form action="/xiaohu" method="post" enctype="multipart/form-data"> {#enctype为文件上传需要设置的#} <p>用户名:<input type="text" name="username" ></p> <p>密码:<input type="password" name="pwd" ></p> <p>爱好:男<input type="radio" name="sex" value="0"></p> <p> 女<input type="radio" name="sex" value="1"></p> <p>爱好:蓝球<input type="checkbox" name="hobby" value="bsk"></p> <p> 足球<input type="checkbox" name="hobby" value="football"></p> <p> 乒乓球<input type="checkbox" name="hobby" value="pingpang"></p> <p><input type="file" name="longfei"></p> {#文件上传代码,name为文件名称。#} <p><input type="submit" value="发送"></p> </form> </body> </html>
文件上传views.py代码:
from django.shortcuts import render # Create your views here. def xiaohu(req): print('前端数据GET',req.GET) print('前端数据POST',req.POST) print('file:',req.FILES) # for item in req.FILES: # obj = req.FILES.get(item) # filename = obj.name # f = open(filename,'wb') # for line in obj.chunks(): # f.write(line) # f.close() for item in req.FILES: fileObj = req.FILES.get(item) f = open(fileObj.name, 'wb') iter_file = fileObj.chunks() for line in iter_file: f.write(line) f.close() return render(req,"index.html")
作者:龙飞
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!