我有这个表格。
instance = get_object_or_404(Post, user = request.user, slug = slug )
files = file.objects.all().filter(Post=instance)
FormSet = modelformset_factory(file, fields=('file',), can_delete=True)
formset=FormSet(request.POST or None, request.FILES or None, queryset=files)
models.py:
class file(models.Model):
file = models.FileField(upload_to = upload_location, blank = True, null = True)
Post = models.ForeignKey(Post, blank = True, null = True)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True, blank = True, null = True)
def filename(self):
f = self.file.name
f1 = f.split('/', 3)[-1]
return f1
def __unicode__(self):
return self.file.name
def __str__(self):
return self.file.name
这就是我在模板中使用它的方式:
{{ formset.management_form }}
{% for form in formset %}
{{form.file}}
{{form.DELETE.label}}
{{form.DELETE}}
{% endfor %}
在我的浏览器中显示如下:
我试过这一切,但我无法摆脱目前,我真的不知道如何设计这个。请帮帮我们。