Uploading images using Django Admin?

前端 未结 1 900
孤城傲影
孤城傲影 2020-12-13 21:29

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

相关标签:
1条回答
  • 2020-12-13 22:34

    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:

    1. Uploading images using Django Admin
    2. Including a profile picture (singular) to go with a celebrity.

    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)
    
    0 讨论(0)
提交回复
热议问题