I am making a web site for a travel agency. I am using a modal field FileField to add PDF files to more pages.
models.py
class Travel(models.Model): docfile = models.FileField(upload_to='documents')
It works fine when uploading just one file in admin and displaying one file on the template.
file.html
<div> <h4><a href="{{ travel.docfile.url }}">{{ travel.docfile.name }}</a></h4> </div>
But what if I want to upload and display more than one file per page? I now I could loop over all files to get all files for all pages:
{% if documents %} <ul> {% for document in documents %} <li><a href="{{ document.docfile.url }}">{{ document.docfile.name }}</a></li> {% endfor %} </ul> {% else %} <p>No documents.</p> {% endif %}
view.py
def vacation(request): if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): newdoc = Travel(docfile = request.FILES['docfile']) newdoc.save() return HttpResponseRedirect(reverse('myapp.views.vacation')) else: form = DocumentForm() documents = Travel.objects.all() return render_to_response('myapp/file.html',{'documents': documents, 'form': form},context_instance=RequestContext(request))
But how can I upload and display more files for one page?
Thank you for your time.
score:0
there are a few ways (that I can see):
you allow up to N uploads each time: thus you just create a form with N fields to take care of it. It's not very fancy (nor the most usable UI), but it's easy to implement.
if you want to give the user the possibility to upload a "dynamic" number of files, you may implement a view that is called via ajax. This still requires your user to select one file at the time, and load it, but then you can upload several files one after the other, without reload the entire page. You may even find some reusable app, like the following, that already should implement the most of it: https://github.com/skoczen/django-ajax-uploader
you can use the HTML5
multiple
attribute of the<input type="file" ...
field. Even in this case there is an interesting reusable app: https://github.com/Chive/django-multiupload
Hope it helps
Credit To: stackoverflow.com
Related Query
- Using Modal FileField to add PDF files
- How to upload files into BinaryField using FileField widget in Django Admin?
- How to add existing files to project using Eclipse and PyDev
- Django - Creating & Store PDF Files using XHTML2PDF
- How to add total page count in PDF using reportlab
- Save multiple files using FileField
- Add 'comic sans ms' font in pdf using pisa library in django
- How can I add static files without using Djstatic and Whitenoise?
- How to add digital signature or signature image in PDF using python/django?
- Add Text on Image using PIL
- add request.GET variable using django.shortcuts.redirect
- Django: Add response header when using render or render_to_response
- multiple files upload using same input name in django
- Django test FileField using test fixtures
- Should I add Django admin static files to my git repo?
- How to delete files from filesystem using post_delete - Django 1.8
- Django. Using multiple settings files with Heroku
- Connection reset by peer when using s3, boto, django-storage for static files
- PDF output using Weasyprint not showing images (Django)
- Calling rm from subprocess using wildcards does not remove the files
- Adding prefix path to static files in Angular using angular-cli
- Where are the files downloaded using pip stored in virtualenv?
- Using Heroku for Django Media Files
- Using django-storages and the s3boto backend, How do I add caching info to request headers for an image so browser will cache image?
- How to add an initial/default value using Django Filters?
- How to add data to ManyToMany field using Django Rest Framework?
- Renaming files in Django FileField
- Add a dynamic form to a django formset using javascript in a right way
- Using annotate or extra to add field of foreignkey to queryset ? (equivalent of SQL "AS" ?)
- Can we create interactive PDF forms using reportlab?
More Query from same tag
- Django FormSet and PEP8 naming conventions
- How should I go about integrating wagtail-lazyimages with Wagtail's RichTextField?
- Exception Value: ('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
- Change the default dashes in Django Createview from
- Django 1.8.3 - model field validation with related object
- PyCharm: Forcing Django Template Syntax Highligting
- How to compress a specific file using pipeline in Django?
- Django model time stamp range filter
- Django Beginner. How do I update all objects and set a certain field to a value that is a function of another field?
- Django custom template filter feeding cookie or default value
- Django translations not working?
- Django Rest Framework: XMLHttpRequest cannot load http://127.0.0.1:8000/xyz/api/abc
- How to access dictionary values in django template
- How to exclude an item in Django loop?
- Model instance not reflecting data passed from pre_save in custom field