I've created django project and in settings.py configured all the necessary assets
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATICFILES_DIRS = [
"~/<project_name>/static",
]
I have all of my static files based in the folder static in the project directory. Inside this folder I have css, js, images folders accordingly. In my base template (that is inherited by others) called base.html I have template tag {% load staticfiles %}. I can't figure out why the page still loads without any static attached, because everything seems to be made properly.
Change to this (more in STATICFILES_DIRS setting):
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
And inside your templates fetch static files like this (Django 1.10):
{% load static %}
<link rel="stylesheet" href="{% static 'css/css-file.css' %}">
Related
I have just started learning DJango and am trying to use css file for my html template. As per the documentation, I have added the STATIC_URL and STATIC_ROOT to my settings.py file as follows:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
BASE_DIR is already set as follows:
BASE_DIR = Path(__file__).resolve().parent.parent
In the template, I am using the css file as follows
{% load static %}
<link rel="stylesheet" href="{% static 'style.css' %}">
and the style.css is present at location DJango_App/static, DJango_App being my project directory with the manage.py file. Still I am getting error
"GET /static/style.css HTTP/1.1" 404 1653
DEBUG is set to True
Directory structure is:
DJango_App
|->DJango_App
|->(settings.py, urls.py, views.py, etc)
|->templates
|->(html templates)
|->static
|->style.css
How do I resolve this?
Update:
Looking at your directory structure above, your static files are not under a Django app. In that case, you should also set STATICFILES_DIRS, see:
https://docs.djangoproject.com/en/3.1/howto/static-files/#configuring-static-files
https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATICFILES_DIRS
Original:
Looks like you did not add the static url handler to urlpatterns.
Serving static files during development require add to 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)
So for settings up my django static files I added these code in settings.py
STATIC_URL = '/static/'
STATIC_DIRS = [
os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
So I created A static folder In Base Directory and then I created a folder named css inside of it and I added a file named nav.css into it.
<link rel="stylesheet" href="{% static 'css/nav.css' %}">
But it's not working at all. It gives me error :
Failed to load resource: the server responded with a status of 404 (Not Found)
What I did wrong?
In your settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),]
My style.css is placed in appname/static/appname/.
My settings.py has this code:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static/"),
)
And in my base.html I load it like this:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'appname/style.css' %}">
But the styles are not loading.
If I remove STATICFILES_DIRS and change STATIC_URL = '/static/' to STATIC_URL = '/static/appname/', it works perfectly, but I guess it's not the best practice for the case I'll add any other app to the project later. What I might be doing wrong?
Just change one thing,
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
It will search in static folder inside your app. Also if you want to add a specific directory,
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"), '/your specific directory/',
)
From here you can directly add the particular file name, and djnago will search in that specific directory.
Remove "appname" in {% static 'appname/style.css' %}, you must not place it there because python knows automatically in which application the file is, it get the application name from the request
By default django picks static directory from app's directory. So, if your static directory is inside app directory there is no need to specify STATICFILES_DIRS.
Now /static/ will point to files and directories in the static directory of your app. To refer your css file use
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'appname/style.css' %}">
Django is not loading my static files. However it is loading my templates which are in the static folder. Also chrome is not seeing the static files either I'm not even getting a 404 error and yes they are linked in the html...but they are not showing up in the network panel
Heres my settings.py file
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS =(
os.path.join(BASE_DIR, 'static'),
)
Here's my html
<head>
<title>MySite | Home</title>
<meta charset="UTF-8">
<link rel="stylesheet" type='text/css' src='css/normalize.css'>
<link href='http://fonts.googleapis.com/css?family=Questrial|Josefin+Sans' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" src='css/main.css'>
<script src="https://maps.googleapis.com/maps/api/js"></script>
</head>
Sorry I know this question has been asked multiple times and i've tried all those solutions with no luck. I've spent 2 days trying to figure this out
The approach I take with static files is basically what's outlined in the docs.
In local development, Django will serve static files automatically from the directory specified in STATIC_ROOT as long as django.contrib.staticfiles is in your INSTALLED_APPS and DEBUG = True
My project structure typically looks like this:
my_project/
some_app/
lib/
static/ <------- STATIC_ROOT - used in production. `collectstatic` collects here
static_assets/ <- STATICFILES_DIRS - used in local dev
css/
less/
js/
images/
templates/ <---- TEMPLATE_DIRS
manage.py
settings.py is typically:
INSTALLED_APPS = (
. . .
'django.contrib.staticfiles',
. . .
)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static_assets'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
Then in templates, you can again use the staticfiles app's template tags to build out paths to static files:
{% load static from staticfiles %}
<link rel="stylesheet" href="{% static 'css/normalize.css' %}" />
Also note that with <link> tags, you need to use the href property for the url instead of src.
In your setting.py file add this
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
create a folder in your app named static and in your template file add this {% load static %}
I am learning Django and trying to create modular templates and I am coming across this issue
In my Developer tools I am getting this error:
GET http://127.0.0.1:8000/static/assets/css/default.css 404 (NOT FOUND) 127.0.0.1/:8
GET http://127.0.0.1:8000/static/assets/images/pythonlogo.jpeg 404 (NOT FOUND) 127.0.0.1/:84
From my understanding, having STATIC_URL = '/static/' allows {% static %} to be used and also appends /static/ to the path of your static folder. Also, using STATICFILES_DIR is for locating the static files in your project.
Currently I have:
STATIC_URL = '/static/'
STATICFILES_DIR = (
('assets', '/Users/BobDole/Development/django-brad/django_test/'),
)
From reading the documentation, it seems to me that 'assets' is used as a namespace or a variable to represent /Users/BobDole/Development/django-brad/django_test/
In my html page I used
<img src="{% static 'assets/images/pythonlogo.jpeg' %}">
<link rel="stylesheet" type="text/css" href="{% static 'assets/css/default.css' %}">
My current project directory structure
django_test/
admin/
article/ <-- app
templates/
django_test/
templates/
images/
static/
css/
I believe that I am using STATIC_URL and STATICFILES_DIR improperly could some provide me with some suggestions? Thank you!
try this:
<img src="{{ STATIC_URL }}assets/images/pythonlogo.jpeg">
Django will change STATIC_URL for /whatever/whatever/static/, so the url the img will access is: /whatever/whatever/static/assets/images/pythonlogo.jpeg
The thing is /whatever/whatever/static/ has to be the path until the "static" folder inclusive
I use in my projects STATIC_URL like this(settings.py):
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH, "static")
STATIC_URL = '/static/'
EDIT You have to add this 3 lines in your settings.py
What makes you think assets is magic in some way? It's not, it's simply the name of a directory. You don't have a directory called that, so you shouldn't use it. Use {% static 'images/pythonlogo.jpeg' %} etc.