简体   繁体   中英

How to add images to Django blog

I hope this question is not off topic.

I've created a Django blog. It currently displays a title, the blog body itself, the creation date and the author name. I now wish to add images.

Initially I'd be happy to be able to upload and display one image in my blog post but eventually I may wish to be able to upload several images and have them display in some neat way.

I'm thinking I'd like the actual image file to be sotred in and AWS bucket and for my database to reference it. This is all new to me though so if I'm completely wrong, I'd be keen to hear of a better solution. Is there any standard way of doing something like this or any libraries that may help me?

Update

I was first able to upload a single image to my blog by following this tutorial.

I then switched from the media folder in my project root to an AWS bucket with this tutorial.

I'm still trying to figure out how to upload multiple images.

@HenryM, I've made several attempts at your suggestion but so far I haven't managed to get it working.

To add multiple images simply create a model with ForeignKey relationship to the blog

class Blog (models.Model):
    ...

class BlogImage (models.Model):
    blog = models.ForeignKey(Blog, on_delete = models.CASCADE)
    image = models.ImageField(upload_to='blog_images/')

To use AWS for your media storage read about boto3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM