Uploading files using Django Admin

Question:

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.

Asked By: mrqcox

||

Answers:

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>
Answered By: Ian Price
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.