Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading files using Django Admin

Tags:

python

django

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.

like image 552
mrqcox Avatar asked Apr 28 '16 19:04

mrqcox


1 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>
like image 103
Ian Price Avatar answered Oct 04 '22 16:10

Ian Price