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 %}
Related
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' %}">
I have created a project in Django. Also, I am using django-allauth for sign up and login.
In order to use my own templates with django-allauth, I have created a html file called signup.html in a folder called account inside a folder called templates which is outside of of all my apps (/templates/account/signup.html). That works.
I tried to use some custom css file inside signup.html:
<link rel="stylesheet" href="/templates/account/signup.css">
It says that the file can not be found. Though, it is located in templates/account.
your css file must under STATICFILES_DIRS in settings.py,set this in settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
and put account/signup.css file to /static/account/signup.css,you can get it like:
<link rel="stylesheet" href="/static/account/signup.css">
or
{% load static %}
<link rel="stylesheet" href="{% static 'account/signup.css' %}">
I'm having trouble sorting my static directory and linking css files through templates in html pages with django. I keep getting the error "Not Found: /CSS/bootstrap.min.css"
I know this is a problem with how my directory is set up in settings.py but I can't seem to fix the issue. Below is my code for settings.py and layout.html (the page i'm using the call the css file).
layout.html
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Testing {% block title %}{% endblock %}</title>
<link href="{% static 'css/bootstrap.min.css' %}" type="text/css" rel="stylesheet">
</head>
<body>
<div class="container">
{% block content %}
{% endblock %}
</div>
</body>
</html>
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = 'C:/Users/Luke/Desktop/Capstone/CapstoneNotespool/capstonenotespool/capstonenotespool/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
You are almost there! You just need to add the correct static file directory to your STATICFILES_DIRS.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# STATIC_URL tells django the url path to use for all static files. It
# doesn't really have anything to do with your file locations on your
# computer.
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
# Add this line here.
os.path.join(BASE_DIR, "capstonenotespool", "static"),
]
I think You must check again your static url, I think you config be wrong.
Here is answer you looking for.
Example of tree file
And this is my config for this
STATIC_URL = '/static/'
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", >"static-only")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", >"media")
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "static"),
)
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' %}">
I have spent the last few days figuring out how to include an css file into a Django template. I still did not succeed so am hoping someone can help me out.
I have the following settings:
--settings.py--
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
I have not set anything in STATICFILES_DIRS() either.
--urls.py--
urlpatterns = patterns('', (r'^$', 'reviewsite.views.my_homepage_view'),)
urlpatterns += staticfiles_urlpatterns()
--views.py--
def my_homepage_view(request):
return render_to_response('test.html', context_instance=RequestContext(request))
--test.html template--
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/style.css"/>
--source code localhost--
<link rel="stylesheet" type="text/css" href="/static/css/style.css"/>
According to the Django documentation it seems that I have set everything correctly, but the css style is still not applied. The static folder is in the correct place (C:reviews/reviewsite/static) where the rest of my apps also reside. Even if I hardcode the style.css location (C:reviews/reviewsite/static/css/style.css) in the test.html template the css style is not applied. I have checked the style.css and it works without Django.
Any idea of what I am doing wrong?
This is how you call the files in the static
{% static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}"/>
settings.py
import os
import sys
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')
SITE_ROOT = PROJECT_ROOT
MEDIA_ROOT = os.path.join(SITE_ROOT, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
//create staticfiles folder
os.path.join(SITE_ROOT, 'staticfiles'),
)
Everyone, thanks for your help and sorry for the late reply. I tried your suggestions but it didn't work unfortunately. However, after some more time trying I got it working now. This is what worked for me:
--settings.py--
MEDIA_ROOT = ''
MEDIA_ROOT = ''
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = 'http://localhost:8000/static/'
--urls.py--
urlpatterns = patterns('', (r'^$', 'reviewsite.views.my_homepage_view'),)
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT }), )
I now have placed the css file in the static folder in my apps directory. In the template I am using {{ STATIC_URL }}/style.css.