Loading MEDIA_ROOT files with Django Webpack Loader - django

After I setup a vue frontend with a django webpack loader, the media files are not rendering.
settings.py
STATIC_URL = '/static/'
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
WEBPACK_LOADER = {
"DEFAULT": {
"BUNDLE_DIR_NAME": "dist/",
"STATS_FILE": os.path.join(BASE_DIR, "frontend", "webpack-stats.json"),
}
}
And urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/', include("companies.routes.urls")),
path('api/v2/', include("projects.routes.urls")),
path('accounts/register/', RegistrationView.as_view(
form_class=CompanyUserForm,
success_url="/",
), name="django_registration_register"),
path('accounts/', include("django_registration.backends.one_step.urls")),
path('accounts/', include("django.contrib.auth.urls")),
path('api-auth/', include("rest_framework.urls")),
path('api/rest_auth/', include("rest_auth.urls")),
path('api/rest_auth/django_registration/', include("rest_auth.registration.urls")),
re_path(r'^.*$', IndexTemplateView.as_view(), name="entry-point")
]
urlpatterns += [
re_path(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT
}),
]
# adding the media root path.
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
But when I browse the media file url, image isn't rendering. The issue lies inside urlpatterns at this line re_path(r'^.*$', IndexTemplateView.as_view(), name="entry-point") when I comment out this part. I can see the image. But i need the re_path for the frontend view. How should fix this issue?

you need to serve your media files add this to your urls.py
from django.views.static import serve
urlpatterns += [
re_path(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT
}),
]

Related

Static Files not loading when DEBUG = False

I'm using namecheap shared hosting and I hosted my site using cpanel. My site is working fine but if i made DEBUG = False in project settings.py file the static files are not loading. My site url: https://drshahidabegum.com/
settings.py
-----------
# Static files (CSS, JavaScript, Images)
STATIC_DIR = [
BASE_DIR / "static",
]
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py in project folder
-------------------------
"""
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('dr_shahida.urls')),
]
# Configure Admin Titles
admin.site.site_header = "DR. Shahida Begum"
admin.site.site_title = "DR. Shahida Begum"
admin.site.index_title = "Welcome to Admin Dashboard"
handler404 = 'dr_shahida.views.error_404_view'
urls.py in app folder
-------
from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', views.index, name='index'),
path('testimonial/', views.testimonial, name='testimonial'),
path('contact/', views.contact, name='contact'),
path('blog/', views.blog, name='blog'),
path('blog/post/<int:post_id>', views.blogdetailview, name='blogdetailview'),
path('set_language/<str:ln>', views.set_lang, name='set_lang'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
I want my static files to work when DEBUG = False in project settings.py file.

User uploaded images not serve after debug=false in django?

install whitenoise urls.py setting
urlpatterns += static(settings.STATIC_URL, document_root = STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root = MEDIA_ROOT)
urlpatterns += [re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT, }), ]
also configure static root and static url in settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/images/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
MEDIA_ROOT = BASE_DIR / '/static/images/'
STATICFILES_DIRS = [(os.path.join(BASE_DIR, 'static'))]
In urls.py file:
add this:
from django.views.static import serve
and add those two urlpatterns:
re_path('media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT})
re_path('static/(?P<path>.*)$', serve, {'document_root': settings.STATIC_ROOT})

Folder for static images can't be accessed/found

I tried following this or this or this but nothing works for me - I have a directory called media which has uploaded files in it - but I cannot access it with http://127.0.0.1:8000/media/ - Page not found (404).
My settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'PROJECT/static'),
]
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
MEDIA_URL = "/media/"
and my urls.py:
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from django.views.generic.base import RedirectView,
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('home/', RedirectView.as_view(url='/')),
path('admin/', admin.site.urls),
path('', include('appname.urls')),
path('', include('django.contrib.auth.urls')),
path(r'^login', auth_views.LoginView.as_view(template_name='registration/login.html')),
]
if settings.DEBUG is True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Bonus question because I could not try it until now: How would I access an image with filename.jpg in /media/ in a template?
I fixed the issue with trial and error:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'projectname/static'),
]
MEDIA_ROOT = os.path.join(BASE_DIR, "projectname", "media")
MEDIA_URL = "/media/"
try it worked
your_app urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
....
]
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
STATIC_URL = "/static/"
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static")
)
STATIC_ROOT = (
os.path.join(BASE_DIR, "staticfiles")
)
MEDIA_URL = "/media/"
MEDIA_ROOT = (
os.path.join(BASE_DIR, "media")
)

django display uploaded by user images

I have ImageUploadField
I save images at my_project/forum_attachments directory.
But when I try to display them and see by this link: http://127.0.0.1:8000/forum_attachments/1466786056166112161_Nrns2WL.jpg
I get an error
Page not found (404) Request Method: GET Request
URL: http://127.0.0.1:8000/forum_attachments/1466786056166112161_Nrns2WL.jpg
What do I do?
UPD: urls.py:
urlpatterns = [
url(r'^polls/', include('some_app.urls')),
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^about/', views.about, name='about'),
url(r'^login/$', auth_views.login, name='login'),
url(r'^user_logout/$', views.user_logout, name='user_logout'),
url(r'^index_old/', views.index_old, name='index_old'),
url(r'^forum/', views.forum, name='forum'),
url(r'^vip/', views.vip, name='vip'),
url(r'^test/', views.test, name='test'),
url(r'^forum_new/', views.forum_new, name='forum_new'),
]
First set in settings.py:
MEDIA_URL = '/forum_attachments/'
Then in your main urls.py add following piece of code:
from django.conf.urls.static import static
from django.conf import settings
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In case you've not set your MEDIA_URL & MEDIA ROOT in settings or your local settings file, you should follow the below procedure.
Settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(PROJECT_PATH,'../media/')
MEDIA_URL = '/media/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
STATIC_URL = '/static/'
Now in your Urls.py, do this:
from django.conf.urls.static import static
urlpatterns = [
url(r'^polls/', include('some_app.urls')),
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^about/', views.about, name='about'),
url(r'^login/$', auth_views.login, name='login'),
url(r'^user_logout/$', views.user_logout, name='user_logout'),
url(r'^index_old/', views.index_old, name='index_old'),
url(r'^forum/', views.forum, name='forum'),
url(r'^vip/', views.vip, name='vip'),
url(r'^test/', views.test, name='test'),
url(r'^forum_new/', views.forum_new, name='forum_new'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The 'static' was appended to the urlpatterns because your DEBUG might be set to True in your settings.py.

django static files weird behaviour

in my settings.py I have:
import os
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
# ... #
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
# ... #
STATIC_URL = '/static/'
in my urls.py I have:
urlpatterns = patterns('',
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
)
and in my template:
<img src="{{ STATIC_URL }}images/myimage.png">
Which doesn't work. The weird thing is that if I change my settings.py:
STATIC_URL = 'static/'
and my template to:
<img src="/{{ STATIC_URL }}images/myimage.png">
does work!! I have been chasing round in circle to fix this, and I have looked at lots of forum posts, but I can't seem to figure out what I'm doing wrong.
if you use staticfiles app you don't need this (assuming you are using Django 1.3):
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
})
Its handled by the app automatically. Try to remove and see what happens.