13

I want to be able to upload .PDF files using the Django Admin interface and for those files to be reflected in a page on my website. Is this at all possible? If so, how do I do that? Otherwise, what could be a workaround for an admin user to privately upload files that will later be displayed in the site?

What I'm getting from the Django documentation on File Uploads is that the upload happens on the website itself, but I want to do this using the Admin interface so I know only an admin user can upload these files. Found this question but this dealt with images. Any sort of help would be highly appreciated.

Daniel Porteous
  • 5,536
  • 3
  • 25
  • 44
mrqcox
  • 145
  • 1
  • 1
  • 8

1 Answers1

27

Nothing special to do, django-admin has it already.

models.py

class Router(models.Model):
    specifications = models.FileField(upload_to='router_specifications')

admin.py

from django.contrib import admin
from my_app import models

admin.site.register(models.Router)

You'll see a file upload field in your model's admin now. If already a file for the model instance, you'll see a link to it as well.

When rendering a view to a user, pass the model(s) in the view's context and use the field's url property to link to the file.

<a href="{{ my_model_instance.specifications.url }}">Download PDF</a>
Ian Price
  • 7,416
  • 2
  • 23
  • 34
  • Thank you! The backend db of this application is MS SQL, hosted on Azure. Do I change the value of the upload_to parameter to the name of the table or? I have the database open on Management Studio and I've added the tables accordingly but I still get errors. (Should this be an entirely different question or was it appropriate for me to ask this as a comment?) – mrqcox Apr 29 '16 at 08:25
  • 1
    Regardless of database used, files will not be stored IN the db, only a link to the file's location relative to the MEDIA_ROOT setting of your project. As such, it is probably good to go through some tutorials on static and media files in Django, there's a lot to learn but for good reason! – Ian Price Apr 29 '16 at 09:04
  • Got it. Thanks for your help! – mrqcox Apr 29 '16 at 10:39