使用 Django Admin 上传图像? [英] Uploading images using Django Admin?
问题描述
是否有一种简单的方法可以将文件上传功能包含到 Django 的管理界面中?我看到了这个问题,但我不精通Javascript.
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.
是否有任何魔法可以添加到 models.py 或 admin.py 文件中,让我可以使用 Django 的内置 CMS 系统轻松完成此操作?
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?
背景:
我正在尝试创建一个名人数据库,其中包括他们的个人简介、出生日期,并且我想包含一张个人资料图片来配合它.这是我正在努力复习我的 Django/Python 的一个小项目的一部分.
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.
谢谢.
推荐答案
如果我错了,请原谅我,但听起来除了 ImageField
.
Forgive me if I'm wrong, but it sounds like you don't need anything more than the default admin widget for an ImageField
.
这满足:
- 使用 Django Admin 上传图片
- 包括一张名人头像(单数).
此外,您指向的链接已经很旧了.django 管理员随附启用了任意长内联的 javascript(我认为至少一年),因此如果您想要多个图像,只需设置第二个模型,该模型具有您的个人资料模型的外键.设置管理员内联开箱即用的功能!
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)
这篇关于使用 Django Admin 上传图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!