使用Django Admin上传图像? [英] Uploading images using Django Admin?

查看:83
本文介绍了使用Django Admin上传图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个简单的方法来将文件上传功能包含在Django的管理界面中?我看到这个问题,但我不太熟悉Javascript。



有没有任何magick可以添加到models.py或admin.py文件,这将允许我轻松地使用Django内置的CMS系统?



背景:



我正在尝试创建一个包括他们的生物,出生日期的名人数据库,我想包括一张配置文件图片。这是我正在努力刷新Django / Python的迷你项目的一部分。



谢谢。

解决方案

如果我错了,请原谅我,但这听起来像您不需要任何 ImageField



这满足:


  1. 使用Django Admin上传图像

  2. 包括个人资料图片(单数)与名人一起去。

此外,你指向的链接很旧。 django管理员随着javascript启用任意长时间内嵌(至少一年我会想),所以如果你想要多个图像,只需设置一个外部键到你的配置文件模型的第二个模型。设置管理内联,您已经开箱即用功能!

  class Celebrity(models.Model):
name = models.CharField()

类图像(models.Model):
名人= models.ForeignKey(Celebrity)
image = models.ImageField()

class InlineImage admin.TabularInline):
model = Image


class CelebrityAdmin(admin.ModelAdmin):
inlines = [InlineImage]

admin .site.register(Celebrity,CelebrityAdmin)


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.

解决方案

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)

这篇关于使用Django Admin上传图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆