使用Django multiupload库在Django中上载多个图像

使用Django multiupload库在Django中上载多个图像,django,multi-upload,Django,Multi Upload,我正试图使用下面的库在Django项目中实现多重上传 精品店/模型.py class Photo(models.Model): store = models.ForeignKey(Store) photo = models.FileField(null=True, blank=True, upload_to='boutique/index/%Y/%m/%d') url = models.CharField(max_length=40, null=True, blank=

我正试图使用下面的库在Django项目中实现多重上传

精品店/模型.py

class Photo(models.Model):
    store = models.ForeignKey(Store)
    photo = models.FileField(null=True, blank=True, upload_to='boutique/index/%Y/%m/%d')
    url = models.CharField(max_length=40, null=True, blank=True, verbose_name='Image URL')
    created_at = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return str(self.photo)

    def get_absolute_url(self):
        return reverse('cms:photo_edit', args=[self.pk])
from django import forms
from boutique.models import Photo
from multiupload.fields import MultiFileField

class PhotoCreateForm(forms.ModelForm):
    class Meta:
        model = Photo
        fields = ['store']

    attachments = MultiFileField(min_num=1, max_num=3, max_file_size=1024*1024*5)
class PhotoCreateView(FormView):
    model=Photo
    template_name='cms/photo_new.html'
    form_class = PhotoCreateForm
    success_url=reverse_lazy('cms:photo')
    queryset = Photo.objects.all()

    def form_valid(self, form):
        for each in form.cleaned_data['attachments']:
            Attachment.objects.create(file=each)
        return super(PhotoCreateView, self).form_valid(form)
class PhotoCreateView(FormView):
model=Photo
template_name='cms/photo_new.html'
form_class = PhotoCreateForm
success_url=reverse_lazy('cms:photo')
queryset = Photo.objects.all()

def form_valid(self, form):
    for each in form.cleaned_data['attachments']:
        Photo.objects.create(photo=each)
    return super(PhotoCreateView, self).form_valid(form)
cms/forms.py

class Photo(models.Model):
    store = models.ForeignKey(Store)
    photo = models.FileField(null=True, blank=True, upload_to='boutique/index/%Y/%m/%d')
    url = models.CharField(max_length=40, null=True, blank=True, verbose_name='Image URL')
    created_at = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return str(self.photo)

    def get_absolute_url(self):
        return reverse('cms:photo_edit', args=[self.pk])
from django import forms
from boutique.models import Photo
from multiupload.fields import MultiFileField

class PhotoCreateForm(forms.ModelForm):
    class Meta:
        model = Photo
        fields = ['store']

    attachments = MultiFileField(min_num=1, max_num=3, max_file_size=1024*1024*5)
class PhotoCreateView(FormView):
    model=Photo
    template_name='cms/photo_new.html'
    form_class = PhotoCreateForm
    success_url=reverse_lazy('cms:photo')
    queryset = Photo.objects.all()

    def form_valid(self, form):
        for each in form.cleaned_data['attachments']:
            Attachment.objects.create(file=each)
        return super(PhotoCreateView, self).form_valid(form)
class PhotoCreateView(FormView):
model=Photo
template_name='cms/photo_new.html'
form_class = PhotoCreateForm
success_url=reverse_lazy('cms:photo')
queryset = Photo.objects.all()

def form_valid(self, form):
    for each in form.cleaned_data['attachments']:
        Photo.objects.create(photo=each)
    return super(PhotoCreateView, self).form_valid(form)
cms/views.py

class Photo(models.Model):
    store = models.ForeignKey(Store)
    photo = models.FileField(null=True, blank=True, upload_to='boutique/index/%Y/%m/%d')
    url = models.CharField(max_length=40, null=True, blank=True, verbose_name='Image URL')
    created_at = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return str(self.photo)

    def get_absolute_url(self):
        return reverse('cms:photo_edit', args=[self.pk])
from django import forms
from boutique.models import Photo
from multiupload.fields import MultiFileField

class PhotoCreateForm(forms.ModelForm):
    class Meta:
        model = Photo
        fields = ['store']

    attachments = MultiFileField(min_num=1, max_num=3, max_file_size=1024*1024*5)
class PhotoCreateView(FormView):
    model=Photo
    template_name='cms/photo_new.html'
    form_class = PhotoCreateForm
    success_url=reverse_lazy('cms:photo')
    queryset = Photo.objects.all()

    def form_valid(self, form):
        for each in form.cleaned_data['attachments']:
            Attachment.objects.create(file=each)
        return super(PhotoCreateView, self).form_valid(form)
class PhotoCreateView(FormView):
model=Photo
template_name='cms/photo_new.html'
form_class = PhotoCreateForm
success_url=reverse_lazy('cms:photo')
queryset = Photo.objects.all()

def form_valid(self, form):
    for each in form.cleaned_data['attachments']:
        Photo.objects.create(photo=each)
    return super(PhotoCreateView, self).form_valid(form)
cms/photo_new.html

{% extends 'cms/base.html' %}
{% load staticfiles %}

{% block page-header %}Add Photo{% endblock %}

{% block content %}
  <form action="" method="post">
    {% csrf_token %}
    <table class="table table-hover store-form">
      {{ form.as_table }}
    </table>
    <input class="btn btn-success btn-block" type="submit" name="" value="Submit">
    <br>
  </form>
{% endblock %}
{% extends 'cms/base.html' %}
{% load staticfiles %}

{% block page-header %}Add Photo{% endblock %}

{% block content %}
    <form action="" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <table class="table table-hover store-form">
          {{ form.as_table }}
        </table>
        <input class="btn btn-success btn-block" type="submit" name="" value="Submit">
        <br>
   </form>
{% endblock %}
{%extends'cms/base.html%}
{%load staticfiles%}
{%block页眉%}添加照片{%endblock%}
{%block content%}
{%csrf_令牌%}
{{form.as_table}}

{%endblock%}
仅供参考,我使用的不是Django默认管理员,而是我自己定制的管理员,即名为
cms
的应用程序。我还在名为
精品店的应用程序中使用模型。
当我上传照片时,什么都没有发生,页面甚至没有移动到成功url。提交文件后,文件输入字段仅显示“此字段是必需的”
,我在数据库上没有看到任何上载。
我的代码有问题吗?

您的型号名称是Photo,那么您为什么要尝试在附件型号中保存照片

cms/views.py

class Photo(models.Model):
    store = models.ForeignKey(Store)
    photo = models.FileField(null=True, blank=True, upload_to='boutique/index/%Y/%m/%d')
    url = models.CharField(max_length=40, null=True, blank=True, verbose_name='Image URL')
    created_at = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return str(self.photo)

    def get_absolute_url(self):
        return reverse('cms:photo_edit', args=[self.pk])
from django import forms
from boutique.models import Photo
from multiupload.fields import MultiFileField

class PhotoCreateForm(forms.ModelForm):
    class Meta:
        model = Photo
        fields = ['store']

    attachments = MultiFileField(min_num=1, max_num=3, max_file_size=1024*1024*5)
class PhotoCreateView(FormView):
    model=Photo
    template_name='cms/photo_new.html'
    form_class = PhotoCreateForm
    success_url=reverse_lazy('cms:photo')
    queryset = Photo.objects.all()

    def form_valid(self, form):
        for each in form.cleaned_data['attachments']:
            Attachment.objects.create(file=each)
        return super(PhotoCreateView, self).form_valid(form)
class PhotoCreateView(FormView):
model=Photo
template_name='cms/photo_new.html'
form_class = PhotoCreateForm
success_url=reverse_lazy('cms:photo')
queryset = Photo.objects.all()

def form_valid(self, form):
    for each in form.cleaned_data['attachments']:
        Photo.objects.create(photo=each)
    return super(PhotoCreateView, self).form_valid(form)
如果要上载任何文件或图像,则需要将enctype=“multipart/form data”添加到HTML表单中

cms/photo_new.html

{% extends 'cms/base.html' %}
{% load staticfiles %}

{% block page-header %}Add Photo{% endblock %}

{% block content %}
  <form action="" method="post">
    {% csrf_token %}
    <table class="table table-hover store-form">
      {{ form.as_table }}
    </table>
    <input class="btn btn-success btn-block" type="submit" name="" value="Submit">
    <br>
  </form>
{% endblock %}
{% extends 'cms/base.html' %}
{% load staticfiles %}

{% block page-header %}Add Photo{% endblock %}

{% block content %}
    <form action="" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <table class="table table-hover store-form">
          {{ form.as_table }}
        </table>
        <input class="btn btn-success btn-block" type="submit" name="" value="Submit">
        <br>
   </form>
{% endblock %}
{%extends'cms/base.html%}
{%load staticfiles%}
{%block页眉%}添加照片{%endblock%}
{%block content%}
{%csrf_令牌%}
{{form.as_table}}

{%endblock%}
更新这两个文件。希望它能起作用。如果没有,请告诉我。

无需使用,简单回答-