[django]modelform实现的多文件上传
Posted iiiiiher
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[django]modelform实现的多文件上传相关的知识,希望对你有一定的参考价值。
实现效果
代码
models.py
from django.db import models
import uuid
class 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 forms
from app01.models import Author
class 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实现的多文件上传的主要内容,如果未能解决你的问题,请参考以下文章
ModelForm (Django) 中 FileField 上传的进度条
使 ModelForm 与 Django 中的中间模型的多对多关系工作的步骤是啥?