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".
Related
I'm trying to create a homepage for my app but my page keeps loading after adding the STATIC_ROOT and STATICFILES_DIR. it gives Broken pipe from ('127.0.0.1', 49634) in my terminal. This is my home.html
This is my settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = 'static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
this is my urls.py
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# -------- handle Static Files In dajngo --------
# Steps:
# 1 - Create (static) folder in Root Directory of Project
# 2 - Paste Your Static Files Folder in (static) Folder (js,css,images...etc)
# 3 - Now Do Setting in (setting.py)
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static')]
#-------- OR ----------
STATICFILES_DIRS = [BASE_DIR / 'static']
# 4 - add {% load static %} tag on top of html page
# 5 - Now use ({% static 'assets/image.jpg' %}) tag in HTML File for calling static files
# 6 - Done
Here is the configuration of my static_root variable:
STATIC_ROOT = [os.path.join(BASE_DIR,'vol/web/static')]
My templates are at the root of the project.
TEMPLATES={
...
'DIRS':[os.path.join(BASE_DIR, 'templates')],
...
}
This is how I call my bootstrap.min.css file in a project template:
<link rel="stylesheet" href="{% static 'assets/plugins/bootstrap/css/bootstrap.min.css' %}">
But it doesn't work because the file is not loaded. What's the right way to do it?
Inside main urls.py
from django.conf import settings as SETTINGS
from django.conf.urls.static import static
urlpatterns += static(SETTINGS.STATIC_URL, document_root=SETTINGS.STATIC_ROOT)
urlpatterns += static(SETTINGS.MEDIA_URL, document_root=SETTINGS.MEDIA_ROOT)
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
STATIC_URL = '/static/'
STATICFILES_DIRS = (BASE_DIR / 'static',)
STATIC_ROOT = (BASE_DIR / 'static'/ 'static')
If you store your js and css files in some folder, then you need to specify this folder in STATICFILES_DIRS django setting. For example:
STATICFILES_DIRS = [
"/home/my-user/my-project/static",
]
Then Django will search this directory(ies) when looking up files specified in {% static ... %} tag.
https://docs.djangoproject.com/en/4.0/ref/settings/#staticfiles-dirs
If Django still unable to find particular file, try using findstatic management command for diagnostics. This will show in which directories django is actually looking for the file. For example:
python manage.py findstatic -v3 assets/plugins/bootstrap/css/bootstrap.min.css
https://docs.djangoproject.com/en/4.0/ref/contrib/staticfiles/#findstatic
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.
I'm starting to configure my first Django project and I find this issue which is really bothering me.
I have set a root static folder to put some css files for my base template there, but Django is not finding any css files there.
My settings.py are like this:
...
BASE_DIR = Path(__file__).resolve().parent.parent
...
STATIC_URL = '/static/'
SATICFILES_DIRS = [
BASE_DIR / 'static',
BASE_DIR / 'sales' / 'static'
]
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
...
...
And in my urls.py I have:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('sales.urls', namespace='sales')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT, show_indexes=True)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT, show_indexes=True)
...
If a run findstatic I get the following:
$ python manage.py findstatic --verbosity 2 static
No matching file found for 'static'.
Looking in the following locations:
/home/dancab/git/django-3-course/myenv/lib/python3.9/site-packages/django/contrib/admin/static
/home/dancab/git/django-3-course/src/sales/static
And also, in the browser, I can see the list of files in the MEDIA folder, but I can't see the STATIC folder, I get the following error:
I don't understand why I Django finds the MEDIA folder and not the STATIC folder.
Thanks in advance to anyone that gives me a hint on why this happens.
before the line STATIC_URL = '/static/', set static root like
STATIC_ROOT = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'project_folder/static') # if not works, set actual path
]
don't forget to place your project folder name.
Then run
python manage.py collectstatic
Then you can load static using jinja expression in your html files like
{% load static %}
Then you can link your static css files in root->static->css folder like
<link rel="stylesheet" href="{% static 'css/your.css' %}">
When I load my local site I cannot get the CSS files to load. I'm running Django 1.9 and python 3.4.2.
Here is my structure:
apps/
app1/
app2/
etc.
clients/
media/ #css, js, images, etc.
static/ #static files
templates/ #html templates
__init__.py
manage.py
settings.py
etc.
In my settings.py file I have:
STATIC_ROOT = os.path.join(BASE_DIR, 'clients', 'static')
STATIC_URL = 'clients/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'clients', 'media'),
]
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'clients', 'media')
And my html template that is calling the css files is as so:
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/primary_stylesheet.css' %}" />
I continue to get a 404 error that says it can't find the css file in:/clients/static/css/primary_stylesheet.css
In my settings.py file I have printed out my STATICFILES_DIRS and STATIC_ROOT and they both lead directly where they should. I've read through the documentation and tried multiple variations of DIRS and ROOT and don't understand why the css file is not pulling correctly - even "collectstatic" is collecting correctly.
I greatly appreciate any help and wisdom someone else has to give.
Thank you!
If you have DEBUG = True set then django won't actually pull your files from the /static/ folder - it finds and collects your staticfiles at runtime when you input the runserver command.
I think you'll find that if you use the default setting for STATICFILES_FINDERS your app will be able to serve your files:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
If you are running your server with python ./manage.py runserver, you need to set urls for both static and media files, check here: https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-static-files-during-development
When I am starting a new project, I generally set my urls.py like this:
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url, include
from django.contrib import admin
url_patterns = [
url(r'^admin/', admin.site.urls),
# your url patterns go here
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This requires you to set STATIC_ROOT, STATIC_URL, MEDIA_ROOT and MEDIA_URL in your settings.py:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static_files'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'