1

我正在构建一个基于动态表单集 jquery 插件的动态表单集:我构建了一个可以工作的 alpha 模型,只是它不会将任何文件上传字段中的数据提交到数据库。我破坏了我的表单集视图功能吗?

#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))
4

1 回答 1

4

如果它不只是发送您的文件上传,我猜它可能是enctype您的表单上缺少的属性。

所以,试着这样做:

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

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

</form>
于 2013-09-17T20:21:03.020 回答