动态formset无法正确处理文件上载数据

时间:2013-09-17 19:43:07

标签: django django-forms

我正在构建一个基于dynamic-formset jquery插件的动态formset:我构建了一个alpha模型,除了它不会将任何fileupload字段中的数据提交到数据库之外。我是否已经破坏了我的formset视图功能?

#forms.py

class PostEntryForm(ModelForm):
    class Meta: 
        model = PostEntry

    ContactFormset = formsets.formset_factory(PostEntryForm)


#models.py

class PostEntry(models.Model):
        client = models.CharField(max_length=50, choices=CLIENT_CHOICES)
        job_number = models.CharField(validators=[RegexValidator(regex='^\w{8}$', message='Please enter a valid job number', code='nomatch')], max_length=8, unique=False, blank=False, null=False)
        cell_number = models.CharField(max_length=4, unique=False, blank=True, null=True)
        post_title = models.CharField(max_length=64, unique=False, blank=True, null=True)
        date = models.DateField(("Date"), default=datetime.date.today)
        post_type = models.CharField(max_length=64, choices=POST_CHOICES)
        post_round = models.CharField(max_length=20, blank=False, null=False)
        docfile = models.FileField(upload_to=content_file_name, blank=True, null=True, max_length=300)
        url_link = models.URLField(blank=True, null=False, max_length=300)
        misc_link = models.CharField(max_length=64, blank=True, null=True)
        link_misc = models.FileField(upload_to=content_file_name, blank=True, null=True, max_length=300)
        misc_link2 = models.CharField(max_length=64, blank=True, null=True)
        link_misc2 = models.FileField(upload_to=content_file_name, blank=True, null=True, max_length=300)
        mobile_view_url = models.URLField(validators=[RegexValidator(regex='^(http|https)://', message='url must begin with http or https', code='nomatch')], blank=True, null=False, max_length=300)
        link_pdf = models.FileField(upload_to=content_file_name, blank=True, null=True, max_length=300)
        link_html = models.FileField(upload_to=content_file_name, blank=True, null=True, max_length=300)
        link_report = models.FileField(upload_to=content_file_name, blank=True, null=True, max_length=300)
        link_text = models.FileField(upload_to=content_file_name, blank=True, null=True, max_length=300)
        link_zip = models.FileField(upload_to=content_file_name, blank=True, null=True, max_length=300)


    def __unicode__ (self):
            return u'%s %s %s %s %s' % (self.client, self.job_number, '-', self.cell_number, self.post_title)

    def save(self, force_insert=False, force_update=False):
            self.job_number = self.job_number.upper()
            self.cell_number = self.cell_number.upper()
            super(PostEntry, self).save(force_insert, force_update)

    class Meta:
            ordering = ['-date', 'cell_number']

    class Admin: 
            pass


#views.py

def formset(request, formset_class, template):
    if request.method == 'POST':
        formset = formset_class(request.POST, request.FILES)
        if formset.is_valid():
            for form in formset.forms:
                form.save()

            return HttpResponseRedirect('/main')

    else:
        formset = formset_class()
        return render_to_response(template, {'formset': formset},
            context_instance=RequestContext(request))

1 个答案:

答案 0 :(得分:4)

如果它没有仅发送您的文件上传量,我猜您的表单上可能缺少enctype属性。

所以,试着这样做:

<form action="" method="POST" enctype="multipart/form-data">
    {{ formset.management_form }}

    {% for form in formset %}
        {{ form }}
    {% endfor %}

</form>