I'm having problems with my Django > css its not loading on the page for some reason am I missing something ?
I have attached snippets of my current code
Settings.py
urls.py
.html
put in you'r setting.py
INSTALLED_APPS = [
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
STATIC_DIR = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS = [
STATIC_DIR
]
then create folder static/file.css(nameyourfile.css)
and then put in your ...html :
<link rel="stylesheet" href= "{% static "file.css" %}">
{% load sataticfiles %}
if you use version 3. put :
{% load static %}
You need to do a few more things to configure your static files.
First, you need to make sure django.contrib.staticfiles is included in your INSTALLED_APPS in your django settings.
Also, you need to add this line in your html document:
{% load static %}
I would follow this guide which explains static files in the django docs.
Related
I am developing an agency web app with django templates & static files. But I am having trouble with loading static files on pages other than index.
Here's my folder heirarchy.
Again, I am having no problems loading static files on my index page.
Here's code of my additional page services.html (one I am trying to load static files on):
{% extends 'base.html' %}
{% load static %}
{% block content %}
...
...
...
{% endblock %}
My base.html is working totally fine with the homepage index.html.
I've included this on my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
And, my views.py and urls.py files are working fine too.
Also my CSS file is correctly linked to the services.html file. I can tell this from the inspection.
Here's the error list shown on inspection. Help will be appreciated. Thank you.
Your static files are stored in the core folder, so one way is to change the settings.py to :
STATIC_URL = '/static/'
STATIC_ROOT = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'core/static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
The other way is to use python manage.py collectstatic. A new staticfiles directory will be created which has folders for admin (the built-in admin has its own static files), staticfiles.json, and whatever directories are in your static folder.
RESOLVED.
I was apparently not using Django HTML tags to link my static CSS/JS files.
So, I initialized static files on my base.html with {% load static %} and replaced links with href="{% static 'assets/css/style.css' %} format. This worked out. Remember that full path shouldn't be written i.e {% static './static/assets/...' %} instead just use {% static 'assets/...' %}.
The current project I've started working on, uses django 2.2 and most of the links are hardcoded.
All static content is in folder named media and used in base.html as follows
in base.html
{% load staticfiles %} ---- using this as first line of base.html
.......
.......
<head>
<link href="/media/css/backoffice/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>
in settings.py
STATIC_URL = '/media/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'media'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'media/')
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
I've also added following to the main urls.py file:
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Yet I'm not able to render the static file listed in the head section of base.html
I've also experimented with commenting out static root and staticfiles_dir but it does not work
You need to put all files in project root's static folder and run this command:
python manage.py collectstatic
For loading static files in the template you need to write this:
{% load static %}
And in all the static files like css, js and images you can do like this:
{% static "<absolute path of file>" %}
And you can refer from documentation.
I hope this will help you!
I know that this problem has already been asked several times here. I have searched and read a number of answers but no help. I guess, I am missing something very basic.
In my settings.py, I have:
STATIC_URL = '/static/'
STATIC_ROOT = join(APPS_DIR, "static/")
# STATICFILES_DIRS = [join(APPS_DIR, 'static')]
MEDIA_ROOT = join(APPS_DIR, 'media')
MEDIA_URL = "/media/"
In my config/urls.py, I have:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I have a file located at /static/core/js/jquery_countdown/jquery.countdown.min.js which I am trying to load in template as below:
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="{% static 'core/js/jquery_countdown/jquery.countdown.min.js' %}"> </script>
The top of the same template looks like
{% extends "contest/base.html" %}
{% load static %}
This results in following server error:
[23/Mar/2018 10:12:08] "GET /static/core/js/jquery_countdown/jquery.countdown.min.js HTTP/1.1" 404 1858
What am I missing?
Create folder static_files in your app directory. And place all your static files inside it. Then use the following settings
STATIC_URL = '/static/'
STATIC_ROOT = join(APPS_DIR, "static/")
STATICFILES_DIRS = [join(APPS_DIR, 'static_files')]
If it does not solve your issue, then run the command python manage.py collectstatic. It will copy all the static files (Not only from your app but also from django admin, third party apps etc) to the STATIC_ROOT folder.
Details
For local serving, django server will look in to the STATICFILES_DIRS. So you dont need to run the python manage.py collectstatic command. All the external apps may have STATICFILES_DIRS where they place their static files. For production server you need to collect all these static files scattered around all your apps in to a single place. Thats what python manage.py collectstatic command will do. It will copy all the static files in to STATIC_ROOT directory
You are searching for static files in app directories only but have your file in global static files. You should use the commented STATICFILES_DIRS setting to specify all places to search for static files.
I found that this worked for me. (Development)
(I chose to name it "zStatic" to keep it at the bottom of the root (project folder), so that it isnt amidst all my apps. Easier to locate.)
Settings.py
STATIC_URL = 'zStatic/'
STATICFILES_DIRS = (
BASE_DIR/'zStatic',
)
INSTALLED_APPS =
[
...
'django.contrib.staticfiles',
...
]
base.html (base template)
<head>
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/mycss.css' %}">
</head>
(I'm using Django 1.3 - Python 2.7)
Been through all the related questions without any resolution.
The syntax error is in line 5, not line one:
{% load static %}
<html>
<head>
<link rel="stylesheet" href="{% static 'css/home.css' %}">
After reviewing related questions, some suggested first line should change to:
{% load staticfiles %}
I tried that, instead of "Invalid block tag: 'static'" error, I get :
'staticfiles' is not a valid tag library: Template library staticfiles not found, tried django.templatetags.staticfiles,django.contrib.admin.templatetags.staticfiles
I double checked the setting files:
STATIC_ROOT = '/home/username/mysite/static'
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'mysite.myapp',
)
Turned "Debug = False"
... and other suggestions. But I still get the same two errors.
The file structure is:
mysite contains the following folders/files: myapp
, media
, static
, settings.py
, ...
myapp contains the following folders/files: static
, templates
, views.py
, ...
both mysite -> static and myapp -> static contain identical folders and files.
Please ask if you require more information.
Any help/direction would be appreciated. Thanks.
In Django 1.3 there's no static tag, and you must use it like this:
{% load static %}
<img src="{% get_static_prefix %}images/hi.jpg" />
or add static to TEMPLATE_CONTEXT_PROCESSORS and use STATIC_URL directly from template:
# settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
# other processors
’django.core.context_processors.static’,
)
# some.html
<img src="{{ STATIC_URL }}images/hi.jpg" />
My django project is pictured above:
I'm trying to set a path to my css using:
<link href="{% static "css/bootstrap.min.css" %} rel="stylesheet">
In my settings.py I have:
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
When I run the project I get:
TemplateSyntaxError at /index/
Invalid block tag: 'static'
What am I doing wrong?
The problem maybe is: You don't loaded tag for staticfiles.
{% load staticfiles %}
You should load staticfiles first, and then you can use static tag.