11👍
The URL to the static-Files is “yourDomain/static/”
When you want to access to your “style.css” you should use “/static/style.css” instead of “/polls/style.css”
EDIT:
Change this part of your settings.py
STATICFILES_DIRS = (
'/polls/static/'
)
to
STATICFILES_DIRS = (
'C:/django poll project/mysite/static'
)
better would be:
STATICFILES_DIRS = (
os.path.join(SITE_ROOT, '..', 'static'),
)
Then the folder is called “static” and is on the same level where the “manage.py” is. When you put your style.css in this “static”-folder you can call it with “/static/style.css”
16👍
Updated Answer For Django 2.2 – 2019
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
BASE_DIR is already defined in
settings.py
Also when loading, do {% load static %}
instead of {% load staticfiles %}
5👍
for Django version 4 2022
if anyone’s static file is not working,make sure your static folder is in the right location
shortcut:
Keep your static folder in the same directory where your database is located.
-->ProjectName
-->app1
-->app2
-->db.sqlite3
-->static
and make sure you have added this lines in settings.py
STATIC_URL = 'static/'
STATICFILES_DIRS = (
BASE_DIR/'static',
)
- Django – Check diference between old and new value when overriding save method
- How to profile django application with respect to execution time?
- Django KeyError: "'__name__' not in globals"
- How do you make a case for Django [or Ruby on Rails] to non-technical clients
2👍
It worked for me :
PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, '..', 'static'),
)
2👍
If problem bad variable {{STATIC_URL}}
in template files, add
'django.template.context_processors.static'
to this part of settings.py
module:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': TEMPLATE_DIRS,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.static',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
- 404 because of restarting the webpack-dev-server
- How can I prevent RuntimeError("Unable to create a new session key.")?
- Why this database migration error after I upgrade my version django-mptt?
- Why use Django's collectstatic instead of just serving the files directly from your static directory?
2👍
Following the documentation: https://docs.djangoproject.com/en/3.2/howto/static-files/
And restarting the server worked for me
- Django days-of-week representation in model
- Python manage.py collectstatic error: cannot find rest_framework bootstrap.min.css.map (from book 'Django for APIs')
- Python – ERROR – failed to write data to stream: <open file '<stdout>', mode 'w' at 0x104c8f150>
- Python / Django Rest Framework weird error happening only when using debugger
2👍
- Remove
django.contrib.staticfiles
fromINSTALLED_APPS
. - Set
STATIC_ROOT
to the drive folder with the static files. - Set
STATIC_URL
to the browser folder/prefix that will point at the files.
The problem is STATIC_ROOT
and STATICFILES_DIRS
cannot be in the same folder and try to act differently…
STATICFILES_DIRS
-
exists because different installable apps have different static resources.
-
Through django magic, in development these files are served from each app.
-
This allows editing assets and re-running the server without worrying about the
STATIC_ROOT
cache. -
Also requires a collector that assembles static files into
STATIC_ROOT
.
STATIC_ROOT
- The place the files are served from in production.
- A simple browser path to folder mapping.
- Not recommended for production because usually
NGINX
or AppEngine handles this. - Works because you are skipping the collector and telling Django to look here always.
- Will not work if an app has its own assets, you will need to use
STATIC_DIRS
.
- Restarting Gunicorn/Nginx when changes are made to files
- Sending request.user object to ModelForm from class based generic view in Django
- How does Waitress handle concurrent tasks?
1👍
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, 'images')
- AssertionError: The field ' ' was declared on serializer ' ', but has not been included in the 'fields' option
- Django- session cookies and sites on multiple ports
- Add user specific fields to Django REST Framework serializer
- Django Rest Framework, ajax POST works but PATCH throws CSRF Failed: CSRF token missing or incorrect
0👍
Currently works on Django Version 3.x
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>Website</title>
<link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
<body>
<h1>Hello user!</h1>
<p>something you want</p>
</body>
</html>
To work above Implementation Following code must be added in project setting file.
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
Here static is a directory in project root.
0👍
For the newer version of Django use the following command. It solved my issues.
STATIC_URL = ‘static/’
STATICFILES_DIRS = [
BASE_DIR / "static",
‘/var/www/static/’,
]
{% load static %}
I followed this documentation (https://docs.djangoproject.com/en/4.1/howto/static-files/)
- Django rest framework: 'create( )' NotImplementedError when making Http POST request
- Django 1.8, makemigrations not detecting newly added app
- Error Map Keys Must be unique while using YAML extension field in docker compose for django application
- Django runserver does not respond when opened in the browser
-1👍
When you run ‘python manage.py runserver’ add any post number like 2000 or whatever it will look like ‘python manage.py runserver 2000’ it will solve the problem
- Django raises MultiValueDictKeyError in File Upload
- How to include prerequisite apps in INSTALLED_APPS
- Learning the Django framework
- Instantiate model instance with manytomany field in Django