How To style a Django formset for fileField

378 views
Skip to first unread message

Rohit Chopra

unread,
Apr 13, 2017, 7:54:27 PM4/13/17
to Django users
stackoverflow question


I have this formset.

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

thats how i used it in my template:

 {{ formset.management_form }}
 {% for form in formset %}
 {{form.file}}
 {{form.DELETE.label}}
 {{form.DELETE}}
 {% endfor %}

and it shows like this in my browser:

look

i tried every this but i could not able to to get rid of currently and i literally do not know how to style this. please help me guys.


ludovic coues

unread,
Apr 14, 2017, 4:10:13 AM4/14/17
to django...@googlegroups.com
What version of django are you using ?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/372952eb-6a2c-4422-9706-00a1a6dbc444%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

m712 - Developer

unread,
Apr 14, 2017, 8:07:06 AM4/14/17
to ludovic coues, django...@googlegroups.com

My apologies. I have solved this problem myself. I separated my models to their own files because they got over 2K lines, and I named the file the attachment model lives in Attachment.py. Because the module-level function getUploadPath and Attachment's getUploadPath both have the same name, Django's migration autodetector tries to access "backend.models.Attachment.getUploadPath" but the Attachment module is overwritten by the Attachment class in backend/models/__init__.py: "from .Attachment import Attachment". I ended up renaming all the model files to remove the ambiguity and then recreating my migrations. Apologies for wasting your time! ^^

Best Regards,
m712 - Developer

m712 - Developer

unread,
Apr 14, 2017, 8:07:36 AM4/14/17
to django...@googlegroups.com

Ahaha, apologies again. I am very new to mailing lists and ended up replying to someone who didn't reply to me. Please remove my messages if possible. Sorry!

Reply all
Reply to author
Forward
0 new messages