5.10 JLL实习日志-实现upload 功能实现
来源:互联网 发布:js。push Array[0] 编辑:程序博客网 时间:2024/05/22 08:26
1view_detil 里面有一个popup里面有一个js,base里面有一个js
<div class="col-lg-12"> <span onclick="javascript: popup('file_upload', 10, '{{ ui_setting.FileUpload }}');">{{ ui_setting.FileUpload }}</span> </div><script type="text/javascript"> function popup(target_item, order, ui_label){ var url_path = "/property/view_detail/{{ property_instance.id }}/?item=" + target_item;跳转到后台views var subheader = "{{ ui_setting.modal_SubTitle }}"; var header = ui_label var target = "#modal-default" + order var target_id = "modal-default" + order $(target).iziModal({ title: ui_label, subtitle: subheader, icon: 'icon-document-edit', transitionIn: 'transitionIn', //transitionOut: 'transitionOut', headerColor: '#000', zindex:1001, iframe: true, fullscreen:true, overlayClose: false, overlayColor: 'rgba(0, 0, 0, 0.7)', iframeURL: url_path, onOpening: function(target_id){ document.getElementById('overflow_fixed').style.overflow="hidden"; }, onClosing: function(e, target){ document.getElementById('overflow_fixed').style.overflow="auto"; } }); $(target).iziModal('open'); $('body').append($(target)); }</script>
2.通过forms 形式传数据到后台实现
<form class="form" action="/property/upload_file/{{ property_instance.id }}/"method="post" enctype="multipart/form-data" onsubmit="javascript: return submit_confirm();" > <fieldset> <div class="row" style="margin:10px 10px 0px -5px;"> {% csrf_token %} {% for field in form %} {% if field.html_name in select_box %}这里的select_box是一个全局变量在第三步里面会解释的,还需要了解的field.html_name 是什么? <div class="col-xs-3 col-md-1"> {{ field }} <script type="text/javascript"> setTimeout(function() { var field_id = "id_" + "{{ field.html_name }}"; var a = document.getElementById(field_id); //var b = document.getElementById(test) a.setAttribute("class","form-control"); a.setAttribute("display","inline"); }, 100); </script> </div> {{ field.error }} {% endif %} {% endfor %} <div class="col-xs-9 col-md-4" > <div class="form-group" style="margin:0px 0px 0px -21px;"> <div class="file" id ='test'> <label for="id_file" style="display:none;">File:</label> <input type="file" name="file" id="id_file" style='margin-top:10px;'><span id="showFileName" class="showFileName">{{ ui_setting.SelectFile }}</span></input> </div> </div> </div> <div class="row col-lg-12"> <div class="form-actions" style="margin:30px 0 0 15px;"> <input class="btn btn-primary" style="margin-top:-40px" disabled="true" type="submit" id="submit" value="{{ ui_setting.Upload }}" > <input type="button" class="btn btn-default" style="margin-top:-40px" onclick="goBack();" value="{{ ui_setting.Back }}" > </div> </div> </div> </fieldset> </form>
3. 把下拉菜单设置为全局变量的形式
selection_box_names = selection_box.selection_boxviews 里面设置全局变量def settings(request): """在全局变量里面设置 TEMPLATE_CONTEXT_PROCESSORS """ user_role = 0 count_set = 0 user_name = 0 tenant_out_of_date_num = 0 context = { 'settings': _settings } language_setting = default_language_setting label_setting = default_label_setting map_type_switch = map_setting.default_map_type module_setting = ui_setting.module_sets select_box = [] if request.user.is_authenticated(): user_instance = UserRole.objects.filter(user=request.user).first() if user_instance: user_role = user_instance.userRole user_name = user_instance.user.username language_setting = language_setting_list[user_instance.language_setting] #notice, not working now label_setting = label_setting_list[user_instance.language_setting] count_set = get_count_set(request) tenant_out_of_date_num=tenant_out_of_date_count(request) select_box = selection_box_names #..... 这里设置全局变量 context['select_box'] = select_box return context
4.form表单在views 里面的使用方式
- 定义一个form表单的类型
class UploadFileFormChoice(forms.Form):
file = forms.FileField()
choices = forms.ChoiceField(widget=forms.Select(), choices= employee_department_choices, initial=’PRF’) - 在views里面使用这个form 表单
elif item == ‘file_upload’:
form = UploadFileFormChoice()定义一个form类型的表单传送到file_upload.html前端
url_to_go =’property/file_upload.html’ - 在另外一个地方使用到表单,接受到表单的位置
def add_files(request, property_id):
if request.method == ‘POST’: # 如果表单被提交
form = UploadFileFormChoice(request.POST, request.FILES) # 获取Post表单数据
if form.is_valid():
file_type =request.POST[‘choices],这边的choice 是根据你定义的forms里面的列称决定的
now_time = datetime.datetime.utcnow()
yesterdaytime = now_time + datetime.timedelta(days=-1)
userid = User.objects.filter(id=request.user.id).first().id
property_instance=Property.objects.all().first()
user_role_instance = UserRole.objects.filter(user=request.user).first().userRole
propertyfiles_count = PropertyFile.objects.filter(Q(Property_File_Timestamp__gt=yesterdaytime)&Q(Q(UploadBy=userid)|Q(DeleteBy=userid))).count()
if(propertyfiles_count < 500):
if request.FILES.getlist(‘file’):
image_flag=’0’
for file_item in request.FILES.getlist(‘file’):
property_instance = get_object_or_404(Property,pk=int(property_id))
file_name_original = file_item.name
file_name_extension = file_item.name.split(‘.’)[-1].lower()
#file_name 拼接实现
file_name = file_type + ‘’ + property_id + ‘’ + datetime.datetime.now().strftime(“%Y_%m_%d_%H_%M_%S_%f”) + ‘.’ + file_name_extension
file_item.name = file_name
record_start_date=timezone.now()
property_file = PropertyFile(Property=property_instance, Property_File=file_item, Property_File_Path=file_name, Property_File_Name = file_name_original, Property_File_Type = file_type, Property_File_Subpart = file_name_extension, RecordStartDate=record_start_date, view_flag = user_role_instance, control_flag = user_role_instance, UploadBy = userid, primary_image_flag = image_flag)
property_file.save()
views.write_operation_log(property_file, “Add”, request)
return HttpResponseRedirect(“/property/view_detail/%s?message=success&item=files” % property_id)
else:
return views.page_not_found(request)
else:
return HttpResponse(“2”)
else:
return HttpResponse(“1”)
0 0
- 5.10 JLL实习日志-实现upload 功能实现
- 5.11 JLL实习日志-实现download功能实现
- 5.12 JLL实习日志-实现download功能实现
- 5.12 JLL实习日志-实现Anjular Js 功能实现
- 7.3-7.5 JLL实习日志-实现DropDownBox的功能
- 5.2JLL实习 import 功能的实现
- 6.22-6.27 JLL--实习日志--Ajax+js 端实现
- 5.3 JLL 实习日志
- 5.5 JLL 实习日志
- 5.9 JLL实习日志
- 5.27 JLL--实习日志--
- 6.5 JLL--实习日志
- 5.17 JLL实习日志-实现get方式过滤数据,表格形式展现在前端
- 7.6-7.7 JLL实习日志-AngularJs的实现+windows下连接数据库
- 6.16-6.19 JLL--实习日志--
- 5.26 JLL--实习日志--Primary_Image+total_setting
- 6.15 JLL--实习日志--SetupNewWorkSpace on cloud9
- 7.14-7.21 JLL实习日志-DropDownBox Upgrade
- weasis调节窗位相关代码分析
- Zstack之RSSI信号值获取以及显示
- 使用HttpClient4,post提交multipart/form-data数据
- HashTable深入理解
- centos tftp配置
- 5.10 JLL实习日志-实现upload 功能实现
- 卡特兰数(Catalan数)
- hadoop
- Policy Reuse
- Linux 源码包和RPM包的区别
- jsp设置
- android屏幕分辨率自适应问题
- ROI_PAC自带测试数据处理流程
- QT创建c/c++动态库