Is there an easy way to include file upload capabilities to the admin interface in Django? I saw this question but I'm not well versed in Javascript.
Is there any magick I can add to the models.py or admin.py files that will allow me to easily do this with Django's built in CMS system?
Background:
I'm trying to create a database of celebrities that include their bio, birth dates and I want to include a profile picture to go with it. This is part of a mini project I'm working on to brush up on my Django/Python.
Thanks.
In Django, a default database is automatically created for you. All you have to do is add the tables called models. The upload_to tells Django to store the photo in a directory called pics under the media directory. The list_display list tells Django admin to display its contents in the admin dashboard.
To add the image file to a HTML template, open a template and add the the HTML img tag along with {% load static %} and {% static “<<image file url>>” %} placeholders, like this.
Forgive me if I'm wrong, but it sounds like you don't need anything more than the default admin widget for an ImageField
.
This satisfies:
Also, the link you point to is pretty old. The django admin ships with javascript enabled arbitrarily long inlines these days (for at least a year I'd think), so if you want multiple images, just set up a second model that has a foreign key to your profile model. Set up an admin inline and you've got out of the box functionality!
class Celebrity(models.Model):
name = models.CharField()
class Image(models.Model):
celebrity = models.ForeignKey(Celebrity)
image = models.ImageField()
class InlineImage(admin.TabularInline):
model = Image
class CelebrityAdmin(admin.ModelAdmin):
inlines = [InlineImage]
admin.site.register(Celebrity, CelebrityAdmin)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With