1

I have a website in production that serves media files properly.

Media Setting in settings.py file :

MEDIA_URL = 'media/'    
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

I have created a media folder inside my project where my all media are being stored. Due to the media URL it severs media files as https://domain_name/media/file_name.

I want to serve my media files as https://domain_name/images/file_name.

I try to change the MEDIA_URL setting in settings.py file, but it shows 404 error for the images.

Updated settings.py file

MEDIA_URL = 'images/'    
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

I am using apache2 as a webserver. Apache file setting:

Alias /static /path_to_the_project/static
  <Directory /path_to_the_project/static>
      Require all granted
  </Directory>


Alias /media /path_to_the_project/media
        <Directory /path_to_the_project/media>
               Require all granted
        </Directory>

  Alias /media /path_to_the_project/images
        <Directory /path_to_the_project/images>
               Require all granted
        </Directory>

<Directory /path_to_the_project/clorrr_tailors>
        <Files wsgi.py>
            Require all granted
        </Files>
      </Directory>

And In template am accessing the images as :

<img src="{{object.image.url}}">
Manoj Kamble
  • 620
  • 1
  • 4
  • 21
  • 1
    Are your media files being served by a webserver (Nginx, Apache, etc.) and if so can you show the config? Can you also show how you are referencing the file urls in your template/view? – Iain Shelvington Sep 16 '22 at 07:00
  • You need to change your Apache config to map `/images` to your media folder – Iain Shelvington Sep 16 '22 at 07:29
  • **I have tried that way also @Iain Shelvington** My all media files are inside the media folder. And I just want to change the media_url. In my local server when I changed the setting MEDIA_URL = 'images/', it working fine. But only in production it not showing proper images. – Manoj Kamble Sep 16 '22 at 07:38
  • In production when changing the media url you also need to change the apache config to serve the media directory from that same url. You need to change both the Django setting and Apache directory alias. It works locally because Django serves media files directly when running the dev server. – Iain Shelvington Sep 16 '22 at 07:40
  • @Iain Shelvington I have uploaded the full apache2 configuration. Check whether I need to change anything ? – Manoj Kamble Sep 16 '22 at 07:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248095/discussion-between-manoj-kamble-and-iain-shelvington). – Manoj Kamble Sep 16 '22 at 08:21
  • 1
    `I need to change` - of course. Your aliases are still mapping `media` url to folders instead of `images` url. – Ivan Starostin Sep 16 '22 at 22:02

1 Answers1

0

As per the documentation

MEDIA_ROOT is the Absolute file path to the directory that will hold user-uploaded files.

MEDIA_URL on the other hand is a placeholder for the url a client should hit to access your media.

So in your case MEDIA_URL is 'images/' that means url will be pointed to images to access the data in MEDIA_ROOT ie the directory media in your BASE Directory

ilyasbbu
  • 1,468
  • 4
  • 11
  • 22