Folder for static images can't be accessed/found - django

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")
)

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.

Issue with image in django

I have problem with image in django. I get 404.
Settings.py
MEDIA_ROOT = 'static/image/'
MEDIA_URL = 'image/'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
Models.py
img = models.ImageField(upload_to = '', verbose_name= 'Картинка',null=True )
(P.S I tried everything,but it isnt working)
Have you got your answer or not if not try this
Add this in your settings.py file
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') #new
MEDIA_URL = '/media/' #new
After Add in your main urls.py file add these config
from django.conf import settings #new
from django.conf.urls.static import static #new
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('App.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) #media

Refused to apply style because its MIME type ('text/html') in django and next js

hello i did everything with integration but nothing seems working,its again saying the same error
all next js files are default no changes
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'frontend/.next/static/css')
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py
from django.contrib import admin
from django.urls import path,include
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.test)
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
pls help me find a solution to this problem i have done everything that i can do with this integration

how to clear the 'The joined path is located outside of the base path component'?

I am try to add the static fills in python manage.py collectstatic but doesn't work
settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'yuvaraj/static/')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', jobs.views.home, name='home'),
path('blogs/', include('blogs.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

MEDIA_URL path doesn't show up

I have a problem with my static and media settings, so my uploaded images doesn't show up on my site page.
In my "settings.py" I have:
MEDIA_ROOT = (
os.path.join(BASE_DIR, '..', 'static', 'media')
)
MEDIA_URL = '/media/'
STATIC_ROOT = (
os.path.join(BASE_DIR, '..', 'static'),
)
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, '..', 'static'),
)
In my "models.py" I have:
expert_photo = models.ImageField(upload_to='profiles')
Some "urls.py":
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
url(r'^experts/all/$', 'expert.views.experts', name='experts'),
url(r'^experts/get/(?P<expert_id>\d+)/$', 'expert.views.expert', name='expert'),
)
And after all of that, when I go to my page, I see, that the picture have link, like this:
http://127.0.0.1:8000/static/profiles/i_vagin.jpg
But it must be:
http://127.0.0.1:8000/static/media/profiles/i_vagin.jpg
So how can I fix that?
Media files are not, by default, served during development. To enable media file serving, you need to add the following code in your urls.py file:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Source: Django docs
Update
You'll also need to access images in your templates like this: {{ expert_photo.url }}