[django]modelform實現的多檔案上傳

來源:互聯網
上載者:User

標籤:author   print   token   HERE   uid   inpu   har   uuid   分享圖片   

實現效果

代碼

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實現的多檔案上傳

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.