When I set DEBUG=False
in app/settings.py
{{object.img.url}}
not working. How to fix this?
when I inspect it's img.url
getting /images/image_name.jpg
like this. In DEBUG=True
http://127.0.0.1:8000/images/image_name.jpg
it shows image. But when I set DEBUG=False
this http://127.0.0.1:8000/images/image_name.jpg
this didn't show anything.
my media root
MEDIA_URL = '/images/'
MEDIA_ROOT = (BASE_DIR / 'static/images')
in my URL I added
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
How to show image/MEDIA_ROOT/ in production.
As django doc says, it is:
Helper function to return a URL pattern for serving files in debug mode: See more here . So, this function returns no URL patterns if not in
DEBUG
mode.
In general, as a rule of thumb, Django should not serve static content, ie static
or media
files. Some other, like nginx, apache, etc. should serve static content in production environment.
Following 4 settings are important.
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = "https://xyz.abc.com/static/"
STATIC_ROOT = "/home/kcsl/web/xyz.abc.com/public_html/static/"
MEDIA_URL = "https://xyz.abc.com/media/"
MEDIA_ROOT = "/home/kcsl/web/xyz.abc.com/public_html/media/"
Use collectstatic command also
$ python manage.py collectstatic
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.