SoFunction
Updated on 2024-10-29

Django media static external access to images in Django settings tutorial

There are two ways: one is to put the image in static, and the other is to put the image in media.

First.

Namely.

STATIC_URL = '/static/'
STATICFILES_DIRS=[
  (BASE_DIR, "static")
]

Run the project and then

Second way.

Use the file media to store pictures, audio and video files.

Additional knowledge:django {{ MEDIA_URL }} value is null

In the template.

<a href="#nowhere" rel="external nofollow" ><img width="100px" src="{{ MEDIA_URL }}{{ }}"></a> {# img/productSingle_middle/ #}

The result {{ MEDIA_URL }} is null

Solution:

Add in the TEMPLATES:

.context_processors.media

TEMPLATES = [
  {
    'BACKEND': '',
    'DIRS': [(BASE_DIR,'templates')],
    'APP_DIRS': True,
    'OPTIONS': {
      'context_processors': [
        '.context_processors.debug',
        '.context_processors.request',
        '.context_processors.auth',
        '.context_processors.messages',
        '.context_processors.media',Add this or the following line
        #'.context_processors.media', python3 will report error
      ],
    },
  },
]

Above this Django media static external access to Django in the picture setup tutorial is all I have to share with you, I hope to be able to give you a reference, but also hope that you support me more.