在Django Admin中上传图像(Uploading Images in Django Admin)

编程入门 行业动态 更新时间:2024-10-20 07:49:20
在Django Admin中上传图像(Uploading Images in Django Admin) python

我有一个充满城市的Django数据库。 我想使用Django管理面板将每个城市的多张图片上传到我的服务器,例如/ images / country_name / state / city /。 这可能会添加到城市管理员表单中,因此可以在一个页面上编辑图像和信息。 我还需要选择主图像并将其转换为缩略图,以便可以在搜索结果中使用。 有哪些实现此类功能的好方法? 有没有好的django插件可以帮助我完成这些任务?

I have a Django database full of cities. I'd like to use the Django admin panel to upload multiple pictures of each city to my server in some folder say /images/country_name/state/city/. This would probably be added to a city admin form, so images and information can all be edited on one page. I also need to select a primary image and convert it to a thumbnail so that it can be used in search results. What are good ways to implement this type of functionality? Are there good django addons that could help me out with these tasks?

最满意答案

你可以做一些彼此相关的模型,并在django-admin中将图像添加为TabularInline ,如:

# models.py class City(models.Model): # your fields class CityImage(models.Model): city = models.ForeignKey('City', related_name='images') image = models.ImageField(upload_to=image_upload_path) # admin.py from django.contrib import admin from myapp.models import City, CityImage class CityImageInline(admin.TabularInline): model = CityImage class CityAdmin(admin.ModelAdmin): inlines = [CityImageInline] admin.site.register(City, CityAdmin)

对于缩略图,您需要在City模型中确定要将哪些相关图像用作缩略图,然后执行以下操作:

import Image try: from cStringIO import StringIO except ImportError: from StringIO import StringIO from django.core.files.base import ContentFile # other imports and models class City(models.Model): # your fields def get_thumbnail(self, thumb_size=None): # find a way to choose one of the uploaded images and # assign it to `chosen_image`. base = Image.open(StringIO(chosen_image.image.read())) # get the image size = thumb_size if not thumb_size: # set a default thumbnail size if no `thumb_size` is given rate = 0.2 # 20% of the original size size = base.size size = (int(size[0] * rate), int(size[1] * rate)) base.thumbnail(size) # make the thumbnail thumbnail = StringIO() base.save(thumbnail, 'PNG') thumbnail = ContentFile(thumbnail.getvalue()) # turn the tumbnail to a "savable" object return thumbnail

我希望这会派上用场! :)

you can do a couple models related to each other and add the image as a TabularInline in django-admin, like:

# models.py class City(models.Model): # your fields class CityImage(models.Model): city = models.ForeignKey('City', related_name='images') image = models.ImageField(upload_to=image_upload_path) # admin.py from django.contrib import admin from myapp.models import City, CityImage class CityImageInline(admin.TabularInline): model = CityImage class CityAdmin(admin.ModelAdmin): inlines = [CityImageInline] admin.site.register(City, CityAdmin)

As for the thumbnail, you would need in your City model a way to decide which of the related images you want to use as thumbnail and then do something like:

import Image try: from cStringIO import StringIO except ImportError: from StringIO import StringIO from django.core.files.base import ContentFile # other imports and models class City(models.Model): # your fields def get_thumbnail(self, thumb_size=None): # find a way to choose one of the uploaded images and # assign it to `chosen_image`. base = Image.open(StringIO(chosen_image.image.read())) # get the image size = thumb_size if not thumb_size: # set a default thumbnail size if no `thumb_size` is given rate = 0.2 # 20% of the original size size = base.size size = (int(size[0] * rate), int(size[1] * rate)) base.thumbnail(size) # make the thumbnail thumbnail = StringIO() base.save(thumbnail, 'PNG') thumbnail = ContentFile(thumbnail.getvalue()) # turn the tumbnail to a "savable" object return thumbnail

I hope this comes in handy! :)

更多推荐

city,Django,城市,images,电脑培训,计算机培训,IT培训"/> <meta name="descrip

本文发布于:2023-08-04 09:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1413106.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   上传   Admin   Django   Uploading

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!