Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

python - Uploading images using Django Admin?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...