Django static files are broken - django

I'm trying to add a background picture to the hero section in Django, But whenever I open that URL: http://127.0.0.1:8000/static/img/bg.png it says 'img\bg.png' could not be found I also attempted to open other urls, but they are broken.
#settings
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = 'E:\coding\django\pawnhost\static'
#urls
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("core.urls")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
#html
<section class="Hero">
...
</section>
#CSS (base.html)
{% load static %}
<style>
.Hero {
background-image: url({% static 'img/bg.png' %});
}
</style>

url:
static(settings.STATIC_URL, document_root=settings.STATIC_DIR)
settings:
STATIC_DIR = BASE_DIR / 'static'
STATIC_ROOT = BASE_DIR/"static_root"
STATIC_URL = '/static/'
STATICFILES_DIRS = [
STATIC_DIR,
]
try to use STATIC_DIR & STATIC_ROOT different name for confusion.

Related

CSS files not loading in django

This is my first Django project. I cannot load CSS files onto my website. i've done everything possible.
Project Directries
firstproject/
assets/
static/
css/
<CSS files>
Settings.py/ static files
STATIC_URL = 'static/'
STATICFILE_DIRS = [
os.path.join(BASE_DIR, "static")
]
STATIC_ROOT = os.path.join(BASE_DIR,'assets')
urls.py
urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
HTML file
<link rel="stylesheet" href="{% static 'css/bootstrap-grid.css' %}">
I think you made spelling mistake in staticfilesdirs in settings.py file.
Change this:
STATIC_URL = 'static/'
STATICFILE_DIRS = [ #Here you made spelling mistake. It should be `STATICFILES_DIRS`
os.path.join(BASE_DIR, "static")
]
To this:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
And in link tag just add type="text/css".

Django. Serving staticfiles with DEBUG=false on local [duplicate]

This question already has answers here:
Why does DEBUG=False setting make my django Static Files Access fail?
(20 answers)
Closed 5 months ago.
http://127.0.0.1:8000/static/apis/icons/random-svgrepo-com.svg gives me error 404 not found.
settings.py
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'staticfiles/'),
]
MEDIA_URL = 'media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')
template.html
{% load static %}
<img src="{% static 'apis/icons/random-svg.svg' %}">
root urls.py
from django.conf.urls.static import static
from django.conf import settings
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Work directory:
I am running ./manage.py collectstatic, and then run the server. But it doesn't show me my image.
My questions are:
Difference between STATICFILES_DIR and STATICFILES_DIRS
What I am doing wrong? Why my static files doesn't appear to work fine?
Thanks!
Changed my root urls.py to:
from django.conf import settings
from django.views.static import serve
urlpatterns += [
re_path(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
re_path(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT})
]

Django not rendering static image files localted in MEDIA directory

im relatively new to Django. I have an app where I specify MEDIA_URL and MEDIA_ROOT in settings.py file in project root. My pared down project structure looks like this:
/
../files
../files/media
../files/media/logo.png
../solid/settings.py
../solid/urls.py
/web/templates (contains all my HTML)
inside settings I have
MEDIA_ROOT = BASE_DIR / 'files'
MEDIA_URL = '/media/'
DEFAULT_FILE_STORAGE = BASE_DIR /'files'
in my template web/template/index.html I'm trying to reference it
<img src="{{% MEDIA_ROOT %}}/images/ourlogo.png" class="" alt="Logo" height="40">
In solid/urls.py I have:
urlpatterns = [
path('admin/', admin.site.urls),
path('',web_views.index,name="index"),
######################################################
path('login/',login_page,name="login"),
path('logout/',log_out,name="logot"),
path('register/',register,name="register"),
#######################################################
path('upload/',web_views.upload,name="upload"),
path('list/',web_views.list_uploads,name="list"),
path('details/<int:oid>', web_views.view_download,name='view_download'),
path('download/<str:filename>', web_views.download_file,name='download_file'),
#######################################################
path('paypal/', include("paypal.standard.ipn.urls")),
path('payment/',web_views.upgrade,name="payment"),
path('paypal-cancel/', PaypalCancelView.as_view(), name='paypal-cancel'),
path('paypal-return/', PaypalReturnView.as_view(), name='paypal-return'),
#######################################################
]+static('s/',document_root=settings.MEDIA_ROOT)
the image is not rendering.
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS =[
os.path.join(BASE_DIR,'project_name/static')
]
MEDIA_URL ='/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
project_name/urls.py
add this two lines with urlspatterns
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
if want to get the image from database
<img src="{{ img.photo_2.url }}" alt="" class="img-fluid">
here "img" is the dictionary key name. photo_2 is the database field name. and to show the image you have put the .url after that.
static image
<link href="{% static 'img/favicon.png' %}" rel="icon">

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: Migrating from MEDIA_URL to STATIC_URL

Since Django 1.3 the concept of STATIC_URL has been introduced to separate use media files from css and js files.
I have set my STATIC_ROOT = '/home/user/project/static/' and
STATIC_URL = '/static/'.
In my base.html, i have changed the path like this:
<link href="{{ STATIC_URL }}bootstrap/css/bootstrap.min.css" rel="stylesheet">
And in url.py I have added the following two lines:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
...
urlpatterns += staticfiles_urlpatterns()
My view renders the template with RequestCOntext:
return render(request, 'main_page.html', variables)
But in the development I still get 404 when running runserver.
[18/Aug/2012 17:12:04] "GET /static/jquery/jquery-1.8.0.min.js HTTP/1.1" 404 1682
What could I be missing?
settings.py
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'site_media')
MEDIA_URL = '/site_media/'
STATIC_URL = '/static/'
if DEBUG:
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
else:
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static'),
)
urls.py
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
That should work :)