ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 在网页上做加减法 [demo下载][1] ### 1 采用 /add/?a=4&b=5 这样GET方法进行 views.py ```python from django.http import HttpResponse def add(request): a = request.GET.get('a', 0) b = request.GET.get('a', 0) c = int(a)+int(b) return HttpResponse(str(c)) ``` Django 1.7.x 及以下 urls.py ```python from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # Examples: url(r'^add/$', 'learn.views.add', name='add'), # 注意修改了这一行 ) ``` Django 1.8.x及以上,Django 官方鼓励(或说要求)先引入,再使用。 urls.py ```python from django.conf.urls import url from learn import views as learn_views urlpatterns = [ url(r'^add/$', learn_views.add, name='add'), # 注意修改了这一行 ] ``` ### 2 采用 /add/3/4/ 这样的网址的方式 修改 calc/views.py文件,再新定义一个add2 函数 ```python def add(request, a, b): c = int(a) + int(b) return HttpResponse(str(c) ``` 接着修改 urls.py 文件,再添加一个新的 url #### 使用位置和分组进行赋值 Django 1.7.x 及以下: ```python urlpatterns = [ url(r'^add/(?P<a>\d+)/(?P<b>\d+)/$', 'calc.views.add', name='add'), url(r'^add/(\d+)/(\d+)/$', 'calc.views.add', name='add'), ] ``` Django 1.8.x 及以上, 视图函数可以**不加引号** ```python urlpatterns = [ url(r'^add/(?P<a>\d+)/(?P<b>\d+)/$', calc_views.add, name='add'), url(r'^add/(\d+)/(\d+)/$', calc_views.add, name='add'), ] ``` ### 3 采用 /add-3-4.html/ 这样的网址的方式 ```python def add(request, a, b): c = int(a) + int(b) return HttpResponse(str(c) ``` Django 1.7.x 及以下: ```python urlpatterns = [ url(r'^add(?P<a>\d+)-(?P<b>\d+).html/$', 'calc.views.add', name='add'), url(r'^add(\d+)-(\d+).html/$', 'calc.views.add', name='add'), ] ``` Django 1.8.x 及以上, 视图函数可以**不加引号** ```python urlpatterns = [ url(r'^add(?P<a>\d+)-(?P<b>\d+).html/$', calc_views.add, name='add'), url(r'^add(\d+)-(\d+).html/$', calc_views.add, name='add'), ] ``` ## 数据提交 ### 提交用户名和密码 后台可以根据input标签中的name获取数据 #### templates ~~~ <form action="/login/" method="post"> <p> <label for="username">用户名:</label> <input id="username" type="text" name="username" placeholder="用户名"/> </p> <p> <label for="password">密码:</label> <input id="password" type="password" name="password" placeholder="密码"/> <br /> <input type="submit" value="提交" /> </p> </form> ~~~ #### 网页效果 ![](http://om4h63cja.bkt.clouddn.com/17-6-19/98223613.jpg) #### 后台数据处理 使用get获取数据,并提供默认值,避免程序出错 ~~~ def login(request): err_msg = '' if request.method == 'POST': username = request.POST.get('username', None) password = request.POST.get('password', None) if username == 'hiyang' and password == 'hiyang': return redirect('/home/') else: err_msg = '密码错误' return render(request, 'login.html', {'err_msg': err_msg}) ~~~ ### 提交单选复选框 #### templates ~~~ <form action="postdata" method="POST"> {# 单选框 #} <p> <label for="favor">篮球</label><input type="radio" name="favor" value="lq"> <label for="favor">羽毛球</label><input type="radio" name="favor" value="ymq"> <label for="favor">乒乓球</label><input type="radio" name="favor" value="ppq"> <input type="submit" value="提交"> </p> {# 复选框 #} <p> <label for="favor">篮球</label><input type="checkbox" name="favor" value="lq"> <label for="favor">羽毛球</label><input type="checkbox" name="favor" value="ymq"> <label for="favor">乒乓球</label><input type="checkbox" name="favor" value="ppq"> <input type="submit" value="提交"> </p> </form> ~~~ #### 网页效果 ![](https://box.kancloud.cn/13a0e59380e08e65e88d79457066572e_284x86.png) #### 后台处理 ~~~ def postdata(request): if request.method == 'GET': return render(request, 'post.html') elif request.method == 'POST': # radio 单选 print(request.POST.get('favor')) # checkbox 多选,如果使用get只能获取多个值中的最后一个 # print(request.POST.getlist('favor')) return render(request, 'post.html') else: return HttpResponse('404') ~~~ ### 提交单选和复选下拉框 #### templates ~~~ <form action="postdata" method="POST"> {# 单选和复选下拉框 #} <p> <select name="city" id="#" multiple> <option value="sh">上海</option> <option value="bj">北京</option> <option value="nj">南京</option> </select> <input type="submit" value="提交"> </p> </form> ~~~ #### 网页效果 ![](http://om4h63cja.bkt.clouddn.com/17-6-19/84512870.jpg) #### 后台处理 ~~~ def postdata(request): if request.method == 'GET': return render(request, 'post.html') elif request.method == 'POST': # select 单选 print(request.POST.get('city')) # select 多选 # print(request.POST.getlist('city')) return render(request, 'post.html') else: return HttpResponse('404') ~~~ ### 上传文件 #### templates ~~~ <form action="postdata" method="POST" enctype="multipart/form-data"> <p> <input type="file" name="file"> <input type="submit" value="提交"> </p> </form> ~~~ #### 网页效果 ![](http://om4h63cja.bkt.clouddn.com/17-6-19/52086204.jpg) #### 后台处理 ~~~ def postdata(request): if request.method == 'GET': return render(request, 'post.html') elif request.method == 'POST': # 获取文件名 # print(request.POST.get('file')) f_obj = request.FILES.get('file') fd = open(f_obj.name, 'wb+') # for chunks in f_obj.chunks(): # 遍历chunk对象 for chunks in f_obj # 遍历文件对象 fd.write(chunks) fd.close() return render(request, 'post.html') else: return HttpResponse('404') ~~~ >[info] 说明 文件对象 = reqeust.FILES.get() 文件对象.name 文件对象.size 文件对象.chunks() ### 其他参数 所有的请求参数都可以从`request.environ`获得 比如`User-Agent` ~~~ request.environ['HTTP_USER_AGENT'] ~~~ [1]:http://files.cnblogs.com/files/hiyang/demo_add_165.tar.gz [2]:http://files.cnblogs.com/files/hiyang/demo_redirect_165.tar.gz