request media files in django when the debug is false - python

i tried to set media root and media url in but when the debug is false don't return anything
settings.py
...
DEBUG = False
ALLOWED_HOSTS = [
'127.0.0.1',
'0.0.0.0',
...
]
...
STATIC_URL = 'static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
STATIC_ROOT = BASE_DIR / 'staticfiles'
MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_URL = 'media/'
urls.py
...
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
...
What should i do to return media files in my projects

I found the answer
urls.py
...
from django.urls import re_path
from django.conf import settings
from django.conf.urls.static import static
from django.views.static import serve
...
urlpatterns = [
...
re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
...
]
...
# urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
...
This re_path function will can request all the files in the MEDIA_ROOT in settings.py

When DEBUG=False, local files will not be accessible. You need to try any online storage for static and media files e.g. AWS S3 bucket etc.
Guide:
https://simpleisbetterthancomplex.com/tutorial/2017/08/01/how-to-setup-amazon-s3-in-a-django-project.html

Related

Django Media Files Not Showing

My Django files are loading after upload and are shown in the media folder, however I cannot access them at localhost:<PORT>/media/<FILENAME._EXT>. I've looked at several other answers on stackoverflow and they haven't worked. For example adding the urlpatterns += static(...), having DEBUG=True in settings.py.
When accessing: http://localhost:8000/media/controller.gif:
Error:
lightchan-backend-1 | Not Found: /media/controller.gif
lightchan-backend-1 | 2022-03-06 16:37:34,875 WARNING Not Found: /media/controller.gif
In settings.py:
DEBUG = True
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
In my urls.py:
from django.urls import path
from django.conf.urls.static import static
from django.conf import settings
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('comment/<int:comment_id>/', views.comment, name='comment'),
path('comments/', views.comments, name='comments'),
path('reply/<int:incoming_id>/', views.reply, name='reply')
]
# if settings.DEBUG is True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Django Oscar Image saved in cache folder but not showing

Could anyone point me the direction to solve the image not displaying properly, since the image was saved in cache folder with 500 error. I am not sure if it is to due to permission or something deeper?
This is what I had configure but not sure if I doing it right.
URLS.py:
from django.apps import apps
from django.urls import include, path
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from oscar.views import handler403, handler404, handler500
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(apps.get_app_config('oscar').urls[0])),
]
if settings.DEBUG:
import debug_toolbar
# Server statics and uploaded media
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# Allow error pages to be tested
urlpatterns += [
path('403', handler403, {'exception': Exception()}),
path('404', handler404, {'exception': Exception()}),
path('500', handler500),
path('__debug__/', include(debug_toolbar.urls)),
]
Setting.py:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
MEDIA_ROOT = '/media/'
MEDIA_URL = '/media/'
THUMBNAIL_DEBUG = DEBUG
THUMBNAIL_KEY_PREFIX = 'oscar-sandbox'
THUMBNAIL_KVSTORE = env(
'THUMBNAIL_KVSTORE',
default='sorl.thumbnail.kvstores.cached_db_kvstore.KVStore')
THUMBNAIL_REDIS_URL = env('THUMBNAIL_REDIS_URL', default=None)
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
I had the same problem. I was helped by adding in urls.py applications
url string
templates += static (settings. MEDIA_URL, document_root=settings. MEDIA_ROOT)
In settings.py were also added:
# Path helper
location = lambda x: os.path.join(
os.path.dirname(os.path.realpath(__file__)), x)
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = location("public/media")
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = location('public/static')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATICFILES_DIRS = (
location('static/'),
)

Django 3.1 not serving media files correctly

I have a project running in Django 3.1, and suddenly it has started to fail serving media files (static files uploaded by users), even though I haven't changed anything in settings.py or elsewhere.
My main urls.py:
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('cart/', include('cart.urls', namespace='cart')),
path('', include('contacts.urls', namespace='contacts')),
path('customers/', include('customers.urls', namespace='customers')),
path('orders/', include('orders.urls', namespace='orders')),
path('account/', include('account.urls')),
path('', include('catalog.urls', namespace='catalog')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
From my settings.py:
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath( os.path.join(__file__, os.pardir))))
DEBUG = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
STATICFILES_DIRS = ( os.path.join('static'), )
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
I don't know and can't figure out where the mistake is, but runserver just keeps throwing 404 when trying to load media files, even though static files (CSS/JS) are being served correctly.
your Urls looks fine, although i notes that you missing from django.contrib import admin.
About your settings, here is mine that works just fine for me.
Add import os to your settings.py if it is not there.

Only Django Admin pages visible while rest of Site blank in production

Im trying to host my site on heroku on my localhost my project is fine with no issues , on deploying it all I get are blank pages (I inspected it and elements are shown but nothing is visible on webpage)apart from admin pages I installed all the necessary packages as I was deploying that is to say gunicorn and whitenoise as well as setting STATIC_ROOT and STATICFILES_STORAGE . I at first set DISABLE_COLLECTSTATIC = 1 and later set it to 0 when deploying, I have failed to see any error that might be causing the issue
some of my files for context
settings
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_dev')]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
urls
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import handler404
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('user.urls')),
path('users/', include('django.contrib.auth.urls')),
path('accounts/', include('allauth.urls')),
path('', include('pages.urls')),
path('store/', include('store.urls')),
#path("djangorave/", include("djangorave.urls", namespace="djangorave")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
#error handlers
handler404 = 'store.views.error_404'
#handler500 = 'store.views.my_custom_error_view'

Django test server gives 404 to static files

In production, I'm using NGINX and set an alias for static files, so this is fine.
However, in test server mode, I get 404 for all of the static files. They are located in the base of the project and here are my relevant settings:
DEBUG = True
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
And here is my urls.py
from django.contrib import admin
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
urlpatterns = patterns(
'',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'home.views.home', name='home'),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
When running the test server, I try to get http://127.0.0.1:8000/static/home/img/logo.png, which is located in $BASE_DIR/static/home/img/logo.png, but get 404.
'home/img/logo.png' could not be found
and in the test server log
[03/Feb/2015 17:49:08] "GET /static/home/img/logo.png HTTP/1.1" 404 1655
Any ideas?
STATIC_DIRS should be STATICFILES_DIRS

Categories

Resources