0

Pardon me, if it sounds like a noob doubt

I am making a blog website, where I am storing the text in MySQL DB, and planning to store image as a url, since otherwise it will make db bulky. So hosting image elsewhere seems to be a reasonable choice.

What I wanted to know is:

1. As soon as user uploads the image(s) in his blog post somewhere, where should I upload that image and how to get the url of that image, so that I can store it in db.

2. How to render that image when fetching from the database at particular place. What I mean by this is, may be the image cane uploaded at top, somewhere in the middle or maybe at the end or the mixture of some or many.

3. And should I create a create a different column to store that image link, if so, then if the user has uploaded multiple images then should I store all under image column of db. If so, then how to tackle my problem #2

Help me, by pointing me towards right direction.

Thanks in Advance :)

1 Answers1

0

User uploaded files are usually placed in a media directory inside your project. You can configure it using MEDIA_ROOT in django settings. And the file's relative path is stored in the database.

For example if you have this config in your settings: MEDIA_ROOT = os.path.join(BASE_DIR, 'media_dir/')

The uploaded file is stored in this path: /path/to/project/media_dir/my_file.csv and your db record has my_file.csv as the value for your FileField.

It would also help you to read this article: https://docs.djangoproject.com/en/4.1/topics/http/file-uploads/

Arash M.
  • 61
  • 5