首页 技术 正文
技术 2022年11月12日
0 收藏 751 点赞 2,238 浏览 1734 个字

实现效果

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

代码

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>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902