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"),
)
Related
I am trying to add CSS styling to my html email to be sent so I used django-inlinecss 0.3.0
In my template I am using the following:
{% load inlinecss %}
{% inlinecss "/css/bootstrap.css" %}
TEXT
{% endinlinecss %}
Here is the complete path of the CSS file:
C:\Users\User\Desktop\Project\static_in_env\css\bootstrap.css
I have tried the following:
{% inlinecss static "css/bootstrap.css" %}
After debugging I found that the reason is due to
[Errno 2] No such file or directory: 'C:\\Users\\User\\Desktop\\static_root\\css\\bootstrap.css'
Here is the files structure:
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_in_env')]
VENV_PATH = os.path.dirname(BASE_DIR)
STATIC_ROOT = os.path.join(VENV_PATH, 'static_root')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
So, how should I fix this error?
Follow the below steps:
1) setting.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR/"static/", ]
2) urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
3) Create Static Folder
create a static folder inside startproject (project_name).
The Folder name should be "static"
4) Add your css file
Inside that static folder create a "css" folder and then add your css file (bootstrap.css)
Once you done all the setup for static file, now we can work with html template.
5) Html Template
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Custom Css -->
<link rel="stylesheet" href="{% static 'css/bootstrap.css' %}" />
</head>
</html>
I deploy the static settings like this:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static/"),
]
And I got a static folder in root with some css files, I use the link like:
<link rel="stylesheet" type="text/css" href="{{STATIC_URL}}css/station_list.css'">
<img src="{{ STATIC_URL }}images/logo.png">
The css path is correct but the html can't display the css style, and I check the terminal, it says:
Not Found: /raise/station/css/station_list.css'
Not Found: /raise/station/images/logo.png
Why the path is not /static/ but an app root? Thanks.
For calling static files like css or image {% static 'css/style.css' %} in your href attribute and {% load static %} place this in top of your template
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 %}
My settings.py
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
In my html page is href to my files using
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/default.css">
<img src="{{ STATIC_URL }}images/pythonlogo.jpeg">
I also tried:
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'css/default.css' %}">
<img src="{% static 'images/pythonlogo.jpeg' %}">
The error I am getting in developer tools is
GET http://127.0.0.1:8000/static/css/default.css 404 (NOT FOUND)
GET http://127.0.0.1:8000/static/images/pythonlogo.jpeg 404 (NOT FOUND)
I tried to print the path on to the web page by simply placing {{STATIC_URL}} on the page and /static/ appears.
My project directory path is:
django_test/
admin/
article/ <-- app
templates/
django_test/
templates/
static/
css/
images/
STATIC_ROOT is directory where all your static files will be copied by collectstatic command.
You should specify your path to STATICFILES_DIRS tuple to use it with built in webserver.
I have configured my static settings like so:
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
('js', os.path.join(STATIC_ROOT, 'js')),
('css', os.path.join(STATIC_ROOT, 'css')),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
and these in my urls.py:
urlpatterns = patterns('',
url(r'^login/?$', login, name='login'),
url(r'^logout/?$', logout_then_login, name='logout'),
url(r'^profile/(?P<user_id>\d+)$', 'profiles.views.detail'),
url(r'^profile/edit$', 'profiles.views.edit'),
)
urlpatterns += staticfiles_urlpatterns()
It works very well for the url localhost:8000/login, but when I get to the localhost:8000/profile/edit site, which is handled by my profiles App, the {{ STATIC_URL }} changes all the paths from /static/... to /profile/static/..., so my javascripts and stylesheets are not found any more.
What'd I do wrong?
EDIT: Here would be my base.html
<!DOCTYPE html>
<html>
<head>
<title>Neighr{% block title %}{% endblock %}</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="{{ STATIC_URL }}js/jquery.min.js"></script>
{% block script %}{% endblock %}
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
Since you're using the django built-in developement server, try to remove the following line from your urls.py:
urlpatterns += staticfiles_urlpatterns()
In production, you'de better not serve static files with django, so use the collectstatic command.
edit
If settings.py, try something like this:
STATIC_ROOT = os.path.join(os.path.dirname(__file__), '../../static')
STATIC_URL = '/static/'