preface

我们知道提交表单有2种方式,一种直接通过submit页面刷新方法来提交,另一种通过ajax异步局部刷新的方法提交,上回我们说了通过ajax来提交文件到后台,现在说说通过submit来提交文件到后台。

看看代码

我们前端使用html语言写的时候,要注意每一个input、select标签需要有name属性,这样我们后端在在获取值的时候,就以name作为key来获取对应value。

首先看看前端html页面

<form id="updatecode"  method="post" action="/BatchM/apply_update.html/apply"   enctype="multipart/form-data" role="form">   # 申明加密类型,上传文件必须这样。
{% csrf_token %}
<label for="exampleInputEmail1">请选择归属项目</label>
<div>
<select name="flow_project" id="flow_project" class="form-control"> # 都设置了name属性
{% for project in projects %}
<option value="{{ project }}" >{{ project }}</option>
{% endfor %}
</select>
</div> <label for="exampleInputEmail1">请选择归属应用</label>
<div>
<select id="flow_app" name="flow_app" class="form-control"> # 都设置了name属性
{% for app in apps %}
<option value="{{ app }}" >{{ app }}</option>
{% endfor %}
</select>
</div> <div class="form-group">
<label for="exampleInputEmail1">哪台服务器需要更新</label>
<!-- <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email"> -->
<input type="text" name="target_host" class="form-control" id="target_host" placeholder="请输入主机ip/域名"> # 都设置了name属性
</div>
<div class="form-group">
<label for="exampleInputFile">附件上传</label>
<input type="file" name="file" id="file_upload"> onclick="FileUpload()">开始上传附件</button> --> # 都设置了name属性
</div>
'''''省略一堆类似的代码'''''
<button type="submit" class="btn btn-success" >提交工单</button>
</form>

我们看看后台,处理这里提交数据的逻辑代码,django作为web框架。

def apply_update_apply(request):
'''
显示申请更新到页面
:param request:
:return:
'''
if request.method == 'GET':
apps = models.TypeOfApp.objects.all()
projects = models.TypeOfProject.objects.all()
return render(request,'apply_update.html',{'btitle':'申请更新','apps':apps,'projects':projects}) elif request.method == "POST": # 提交数据 file_obj = request.FILES.get('file') # 使用这个 request.FILES.get方法来获取上传文件的句柄
upload_file = None
if file_obj: # 处理附件上传到方法
accessory_dir = settings.accessory_dir # 获取上传路径,在settings配置好的
if not os.path.isdir(accessory_dir): # 判断是否有这个目录,没有就创建
os.mkdir(accessory_dir)
upload_file = "%s/%s" % (accessory_dir, file_obj.name) # 拼接上传路径
with open(upload_file, 'wb') as new_file: # 开始写入文件
for chunk in file_obj.chunks(): # 必须使用chunks方法,因为文件上传采用块上传
new_file.write(chunk) project_name = request.POST.get('flow_project') # 获取表单里input标签的参数,所有get的key都是name指定的
flow_project = models.TypeOfProject.objects.get(name_of_project=project_name)
app_name=request.POST.get('flow_app')
flow_app = models.TypeOfApp.objects.get(app_name=app_name)
order_id = time.strftime("%Y%m%d%H%M%S", time.localtime())
#print('usernane',request.user.username) #打印用户到名字
#print('email',request.user.email) # 打印用户的邮箱地址
request_set = { # 做成一个字典,方便下一步入库,所有get的值都是在html页面通过input标签的name属性指定的。
'OrderId':order_id,
'username': request.user.email, # 通过这个方法获取登陆用户的email
'flow_project':flow_project,
'flow_app':flow_app,
'target_host':request.POST.get('target_host'),
'code_source':request.POST.get('code_source'),
'configfile_path':request.POST.get('configfile_path'),
'configfile_content':request.POST.get('configfile_content'),
'sql_command':request.POST.get('sql_command'),
'crond_task':request.POST.get('crondtab_task'),
'system_env_change':request.POST.get('change_sys_env'),
'update_of_reason':request.POST.get('Upreason'),
'accessory_path': upload_file
}
email_issend = core.selfmail(request_set) # 调用发送邮件的功能,发送到指定到地址,成功返回True,失败返回False
request_set['email_issend']= email_issend
data_obj = models.WorkOrderOfUpdate(**request_set)
data_obj.save() # 保存数据
return HttpResponseRedirect('/BatchM/apply_update.html/search/%s'%order_id) # 返回数据给前端

这样我们在前端提交的文本框数据以及文件都可以同时被后台处理啦。。。

4 django系列之HTML通过form标签来同时提交表单内容与上传文件的更多相关文章

  1. php使用jquery Form ajax 提交表单,并上传文件

    在html5中我们通过FormData就可以ajax上传文件数据,不过因为兼容问题.我们选用jquery.form.min.js来进行ajax的表单提交.   一.jquery.form.js下载地址 ...

  2. Django框架 之 Form表单和Ajax上传文件

    Django框架 之 Form表单和Ajax上传文件 浏览目录 Form表单上传文件 Ajax上传文件 伪造Ajax上传文件 Form表单上传文件 html 1 2 3 4 5 6 7 <h3& ...

  3. $_FILES参数详解及简单<form>表单无刷新上传文件

    $_FILES:经由 HTTP POST 文件上传而提交至脚本的变量,类似于旧数组$HTTP_POST_FILES 数组(依然有效,但反对使用)详细信息可参阅 POST方法上传 $_FILES数组内容 ...

  4. <button>标签也能提交表单问题

    如何避免<button>标签也能提交表单的问题: 只需加上一个属性:type='button'即可:如<button type="button"> < ...

  5. FastAPI框架入门 基本使用, 模版渲染, form表单数据交互, 上传文件, 静态文件配置

    安装 pip install fastapi[all] pip install unicorn 基本使用(不能同时支持,get, post方法等要分开写) from fastapi import Fa ...

  6. SpringMVC 使用Form标签库制作登录表单

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  7. 使用jQuery.form库中ajaxSubmit提交表单时遇到的一些问题

    初入前端,网上找的很多资料都不够详细,导致遇到很多问题,现记录如下: 1.首先引入 <script src="~/Scripts/jquery-1.10.2.js">& ...

  8. Liferay 6.2 改造系列之十九:修改站点设置的表单内容

    在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Input a list of sections that will b ...

  9. 实现简单的django上传文件

    本文用django实现上传文件并保存到指定路径下,没有使用forms和models,步骤如下: 1.在模板中使用form表单,因为这个表单使用于上传文件的,所以method属性必须设置为post,而且 ...

随机推荐

  1. VisualStudio 调试Linux

    微软自从换了CEO之后,拥抱开源的步伐真实越来越快了,这部,现在VS可以跟踪Linux程序了 http://blogs.msdn.com/b/vcblog/archive/2015/11/18/ann ...

  2. VS2012 Unit Test —— 我对IdleTest库动的大手术以及对Xml相关操作进行测试的方式

    [1]我的IdleTest源码地址:http://idletest.codeplex.com/ [2]IdleTest改动说明:2013年10月份在保持原有功能的情况下对其动了较大的手术,首先将基本的 ...

  3. 阶段一:通过网络请求,获得并解析JSON数据(天气应用)

    “阶段一”是指我第一次系统地学习Android开发.这主要是对我的学习过程作个记录. 在上一篇阶段一:解析JSON中提到,最近在写一个很简单的天气预报应用.即使功能很简单,但我还是想把它做成一个相对完 ...

  4. 手游录屏直播技术详解 | 直播 SDK 性能优化实践

    在上期<直播推流端弱网优化策略 >中,我们介绍了直播推流端是如何优化的.本期,将介绍手游直播中录屏的实现方式. 直播经过一年左右的快速发展,衍生出越来越丰富的业务形式,也覆盖越来越广的应用 ...

  5. 学习Maven之Maven Clean Plugin

    1.maven-clean-plugin是个什么鬼? maven-clean-plugin这个插件用maven的人都不陌生.我们在执行命令mvn clean时调用的就是这个插件. 这个插件的主要作用就 ...

  6. java对xml节点属性的增删改查

    学习本文之前请先看我的另一篇文章JAVA对XML节点的操作可以对XML操作有更好的了解. package vastsum; import java.io.File; import java.io.Fi ...

  7. 在tmux中的vi 上下左右键变为了ABCD等字符

    在本机上用vim编辑时,上下左右键没有问题,但是在tmux中确出现ABCD等字符. 原因是在tmux这个终端,默认做了字符转换,网上搜了很多答案,解决问题的设置是: set term=xterm

  8. shell-script的简单举例

    #!/bin/bash #defind the path PATH=/usr/local export PATH read -p "please input your first name: ...

  9. 【转】[fix] Wireshark error: There are no interfaces on which a capture can be done. on Mac OS X

    I got the following error message when trying to open a network interface for capture using Wireshar ...

  10. 【转】XenServer架构之XAPI

    一.XAPI对资源池的管理 作为XenServer的管理工具集,XAPI管理XenServer的主机,网络和存储.不管是OpenStack还是CloudStack,如果使用XenServer作为虚拟化 ...