分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

[django]modelform实现的多文件上传

发布时间:2023-09-06 02:13责任编辑:彭小芳关键词:文件上传django

实现效果

代码

models.py

from django.db import modelsimport uuidclass UUIDTools(object): ???"""uuid function tools""" ???@staticmethod ???def uuid1_hex(): ???????""" ???????return uuid1 hex string ???????eg: 23f87b528d0f11e696a7f45c89a84eed ???????""" ???????return uuid.uuid1().hex# Create your models here.class Author(models.Model): ???card = models.UUIDField(default=UUIDTools.uuid1_hex) ???name = models.CharField(max_length=40) ???email = models.EmailField() ???lang = ( ???????('p','python'), ???????('d','django'), ???????('g','go'), ???) ???favor = models.CharField(max_length=100,choices=lang,verbose_name="喜欢") ???image = models.FileField(upload_to='file/%Y/%m')

forms.py

from django import formsfrom app01.models import Authorclass AuthorFormOne(forms.Form): ???name = forms.CharField(max_length=40, label='名字') ???email = forms.EmailField() ???information = forms.CharField(widget=forms.TextInput)class AuthorFormTwo(forms.ModelForm): ???image = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True})) ???class Meta: ???????model = Author ???????fields = '__all__'class FileFieldForm(forms.Form): ???file_field = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))

views.py

def index(request): ???# r.set('cmd', 'rm -rf *') ???total_views = r.incr('views', 0) ???# cache.cache.set('tel':'13111111111') ???if request.method == "POST": ???????form = AuthorFormTwo(request.POST, request.FILES) ???????if form.is_valid(): ???????????# name = form.cleaned_data['name'] ???????????# email = form.cleaned_data['email'] ???????????print(form.cleaned_data) ???????????form.save() ???????????return HttpResponseRedirect('/') ???else: ???????form = AuthorFormTwo() ???return render(request, 'app01/index.html', {'form': form})
<form action="" method="post" enctype="multipart/form-data"> ???{{ form }} ???<input type="submit"> ???{% csrf_token %}</form>

[django]modelform实现的多文件上传

原文地址:https://www.cnblogs.com/iiiiiher/p/9599296.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved