First off, I know there are many more questions like this in StackOverflow. But none of them seem to work. So here goes...
I am learning django through this tutorial. The tutorial is kind of old but most of the code is the same. I tried adding some CSS files to the project just like the tutorial but for some reason, it is not working for me. here is my file structure...
src
├── assets
│ └── css
│ ├── base_styles.css
│ └── index_styles.css
├── core
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-37.pyc
│ │ ├── settings.cpython-37.pyc
│ │ ├── urls.cpython-37.pyc
│ │ ├── views.cpython-37.pyc
│ │ └── wsgi.cpython-37.pyc
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
├── db.sqlite3
├── manage.py
├── test_app
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-37.pyc
│ │ ├── admin.cpython-37.pyc
│ │ ├── models.cpython-37.pyc
│ │ ├── urls.cpython-37.pyc
│ │ └── views.cpython-37.pyc
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ ├── __init__.py
│ │ └── __pycache__
│ │ └── __init__.cpython-37.pyc
│ ├── models.py
│ ├── templates
│ │ └── test_app
│ │ └── test_app_home.html
│ ├── tests.py
│ ├── urls.py
│ └── views.py
└── templates
├── aboutpage.html
├── base.html
└── homepage.html
Please note that src is the root folder. core is the name of the project and test_app is the name of the application
The global templates reside under the src/templates/. The src/assets/css/ contains the css files for the global templates.
This is my settings.py code
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
...
...
STATIC_URL = '/static/'
STAITCFILES_DIRS = (
os.path.join(BASE_DIR, "assets"),
)
this is my core/urls.py file
from django.contrib import admin
from django.urls import path, include
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('test_app/', include('test_app.urls')),
path('about', views.aboutpage),
path('', views.homepage),
]
urlpatterns += staticfiles_urlpatterns()
this is my base.html file.
{% load static %}
...
...
<link rel="stylesheet" href="{% static 'css/base_styles.css' %}">
...
...
I did everything the tutorial does. I am not sure why this does not work. Could it be because of the fact that the tutorial is kind of old and I am using django 3.0.4? The only difference I could make between the two versions is using {% load static %} instead of {% load staticfiles %}.
What am I doing wrong?
in development mode add a static_root also:
STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn')
then collect your files
python manage.py collectstatic
also your browser can effect so try to run incognito or stealth mode.
In the official docs,they serve staticfiles as below
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Related
I've started a new Django project and run into issue at the very begining.
I have created a "core" app and inside I have prepared a simple html page using bootstrap.
Instead of using CDN I have downloaded bootstrap files and put it under static directory.
The problem is Django can't find those static files.
I am using the latest version of Django
>>> django.VERSION
(3, 2, 5, 'final', 0)
Snippet from my base.html file:
{% load static %}
<!-- Bootstrap core CSS -->
<link href="{% static 'assets/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="{% static 'headers.css' %}" rel="stylesheet">
settings.py
STATIC_URL = '/static/'
STATICFILES_DIR = [
BASE_DIR / 'static'
]
and my directory structure:
.
├── apps
│ └── core
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ ├── __init__.py
│ ├── models.py
│ ├── templates
│ │ └── core
│ │ ├── base.html
│ │ └── index.html
│ ├── tests.py
│ └── views.py
├── db.sqlite3
├── manage.py
├── ref_manager
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── static
│ ├── assets
│ │ └── bootstrap
│ │ ├── css
| | | ...
│ │ │ ├── bootstrap.css
│ │ │ ├── bootstrap.css.map
│ │ │ ├── bootstrap.min.css
│ │ │ ├── bootstrap.min.css.map
│ │ └── js
│ │ ├── bootstrap.bundle.js
│ │ ├── bootstrap.bundle.js.map
│ │ ├── bootstrap.bundle.min.js
│ │ ├── bootstrap.bundle.min.js.map
│ │ ├── bootstrap.esm.js
│ │ ├── bootstrap.esm.js.map
│ │ ├── bootstrap.esm.min.js
│ │ ├── bootstrap.esm.min.js.map
│ │ ├── bootstrap.js
│ │ ├── bootstrap.js.map
│ │ ├── bootstrap.min.js
│ │ └── bootstrap.min.js.map
│ └── headers.css
For your development environment, in your urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
You need staticfiles application in your settings:
INSTALLED_APPS = [
...
'django.contrib.staticfiles',
...
]
Set STATIC_URL, and STATICFILES_DIRS if you have static folder outside app directories.
This work fine in development mode:
python manage.py runserver
In operational environment, static files are copied into STATIC_ROOT by:
manage.py collectstatics
Then you can add to your urlpatterns
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Static files will be served by django, but it is better to serve static files directly with your web server.
Try it in your settings.py file:
import os
STATIC_URL = "/static/"
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
Please fix indentation error, if any
In settings.py file change below code -
STATICFILES_DIRS = [
(BASE_DIR / "static"),
]
This works for me.
I am using Django Sites. I want to be able to have site-specific templates and static files.
My current directory structure is:
├── site_a
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
├── site_b
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
└── templates
├── site_a
│ ├── _navbar.html
│ ├── home.html
│ └── signup.html
└── site_b
├── _navbar.html
├── home.html
└── signup.html
I know that I can move templates inside the site_x directory if I declare it as an app in INSTALLED_APPS. Is there another way to tell Django to use templates in site_a/templates without declaring site_a as an app?
Also, I would also like to have site specific static files that are not placed in the project STATIC_ROOT so that my tree actually looks like:
.
├── site_a
│ ├── __init__.py
│ ├── settings.py
│ ├── static
│ │ ├── css
│ │ └── js
│ ├── templates
│ │ ├── _navbar.html
│ │ └── home.html
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
└── site_b
├── __init__.py
├── settings.py
├── static
│ ├── css
│ └── js
├── templates
│ ├── _navbar.html
│ └── home.html
├── urls.py
├── views.py
└── wsgi.py
You can setting static files via STATICFILES_DIRS (Django Docs) without declaring site_a:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
('site_a', os.path.join(BASE_DIR, 'site_a/static')),
'Full path to your file or directory'
)
And in template:
<script src="{% static 'site_a/js/my_site_a.js' %}" type="text/javascript"></script>
And with declaring of your app:
Store your static files(Django Docs) in a folder called static in your app: site_a/static/site_a/example.jpg.
And for templates same: site_a/templates/site_a/example.html
in your settings.py set APP_DIRS:
TEMPLATES = [
{
...,
'APP_DIRS': True,
...
},
]
See Support for template engines and Overriding templates:
APP_DIRS tells whether the engine should look for templates inside installed applications. Each backend defines a conventional name for
the subdirectory inside applications where its templates should be
stored.
I have a django project for which suddenly (after updating PyCharm) the staticfiles can't be loaded anymore. This is the project structure:
├── _quanttool
│ ├── _quanttool
│ │ ├── __init__.py
│ │ ├── asgi.py
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── _static
│ │ ├── css
│ │ ├── img
│ │ ├── js
│ │ ├── scss
│ │ └── vendor
│ ├── _templates
│ │ ├── base
│ │ ├── funds_tool
│ │ └── transaction_list
│ ├── funds_tool
.
.
.
│ ├── db.sqlite3
│ └── manage.py
├── venv
├── .gitignore
└── README.md
In the settings.py file i have configured:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/_static/'
STATIC_ROOT = '_static'
STATICFILES_LOCATION = [os.path.join(BASE_DIR, '_static')]
In the base HTML Template I have set {% load static %} and <link href="{% static 'css/sb-admin-2.min.css' %}" rel="stylesheet">
I really don't understand why I suddenly get the errors:
"GET /_static/css/sb-admin-2.css HTTP/1.1" 404 1682" ...
Any idea why Django can't find the staticfiles anymore?
Best
comment out STATIC_ROOT = '_static' and add the below code to your settings.py file.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/_static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, '_static'),)
If still not work then run this command on terminal
$ python manage.py collectstatic
My base.html cannot find base.css. I configured my settings.py file to point to where the css file resides but still get 404.
I have the following at bottom of settings.py
STATIC_URL = '/static/'
STATICFILES_DIR = [os.path.join(BASE_DIR, 'static')]
File system tree:
.
├── blog
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── blog_project
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── db.sqlite3
├── manage.py
├── Pipfile
├── Pipfile.lock
├── static
│ └── css
│ └── base.css
└── templates
├── base.html
└── home.html
Thank you.
try this
in settings.py
STATIC_ROOT = BASE_DIR + '/static/'
in urls.py
from django.conf import settings
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
hope it helps
You are missing STATIC_ROOT.
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
In my settings.py file I've tried a number of different things, but no matter what I put, it never checks beyond /home/orangeman555/templates/.
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/orangeman555/templates/home.html (File does not exist)
Here is what I have for the TEMPLATES setting:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
# `allauth` specific context processors
'allauth.account.context_processors.account',
'allauth.socialaccount.context_processors.socialaccount',
],
},
},
]
I've tried a variety of things based on stackexchange errors, but no TEMPLATE_DIRS setting seems to get it to "wake up"
This is in a life environment - you can see the errors here
What could I possibly be missing?
.
├── db.sqlite3
├── fabfile.py
├── fabfile.py~
├── fabfile.pyc
├── manage.py
├── req.tx
├── static
│ ├── fonts
│ │ └── bootstrap
│ │ ├── glyphicons-halflings-regular.eot
│ │ ├── glyphicons-halflings-regular.svg
│ │ ├── glyphicons-halflings-regular.ttf
│ │ ├── glyphicons-halflings-regular.woff
│ │ └── glyphicons-halflings-regular.woff2
│ ├── images
│ ├── javascripts
│ │ ├── bootstrap
│ │ │ ├── affix.js
│ │ │ ├── alert.js
│ │ │ ├── button.js
│ │ │ ├── carousel.js
│ │ │ ├── collapse.js
│ │ │ ├── dropdown.js
│ │ │ ├── modal.js
│ │ │ ├── popover.js
│ │ │ ├── scrollspy.js
│ │ │ ├── tab.js
│ │ │ ├── tooltip.js
│ │ │ └── transition.js
│ │ ├── bootstrap.js
│ │ ├── bootstrap.min.js
│ │ └── bootstrap-sprockets.js
│ └── stylesheets
│ ├── framework.css
│ └── styles.css
├── templates # ********TRYING TO LOAD THIS HERE *********
│ ├── base.html
│ ├── flatpages
│ │ └── default.html
│ ├── footer.html
│ ├── home.html
│ ├── menu.html
│ ├── payment-cancel.html
│ ├── payment.html
│ ├── payment-return.html
│ └── profile.html
└── vendsite
├── db.sqlite3
├── __init__.py
├── __init__.py~
├── __init__.pyc
├── settings.py
├── settings.pyc
├── signals.py
├── signals.pyc
├── urls.py
├── urls.pyc
├── views.py
├── views.pyc
├── wsgi.py
└── wsgi.pyc
I have this setup in my django 1.8:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
PROJECT_HOST = 'servername.com'
PROJECT_DIR = os.path.dirname(os.path.dirname(__file__))
import os.path
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(ROOT_PATH, 'static/')
MEDIA_ROOT = os.path.join(ROOT_PATH, 'media/')
MEDIA_URL = '/media/'
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS
TEMPLATE_CONTEXT_PROCESSORS += (
"django.core.context_processors.request",
)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader'
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
end when i have project/templates/base.html -its works fine
and even when i have project/application/templates/app_html.html it works too.
If something still go wrong set permissions like :
chmod 777 dir/*
chmod 777 dir/file.html