I am new to Django and really confused about this.
Here are relevant parts in settings.py:
import os
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATIC_URL = 'http://http://127.0.0.1:8000/static/'
STATICFILES_DIRS = (
PROJECT_ROOT+'/static/')
TEMPLATE_DIRS = (
PROJECT_ROOT + '/templates/')
My project file structure is like this:
MyProj
manage.py
MyProj
settings.py
urls.py
templates
base.html
static
css
js
In base.html starts with:
{% load staticfiles %}
{% load i18n %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bootstrap, from Twitter</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!-- Le styles -->
<link rel="shortcut icon" href="http://twitter.github.com/bootstrap/assets/ico/favicon.ico">
<link href="{{ STATIC_URL }}css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
</style>
<link href="{{ STATIC_URL }}css/bootstrap-responsive.min.css" rel="stylesheet">
</head>
<body>
but django does not get none of the static files:
[15/Oct/2013 05:36:06] "GET /accounts/login/ HTTP/1.1" 200 3402
[15/Oct/2013 05:36:06] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 2089
[15/Oct/2013 05:36:06] "GET /static/css/bootstrap-responsive.min.css HTTP/1.1" 404 2122
[15/Oct/2013 05:36:06] "GET /static/js/jquery.min.js HTTP/1.1" 404 2074
[15/Oct/2013 05:36:06] "GET /static/js/prettify.js HTTP/1.1" 404 2068
[15/Oct/2013 05:36:06] "GET /static/js/bootstrap.min.js HTTP/1.1" 404 2083
[15/Oct/2013 05:36:06] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 2089
[15/Oct/2013 05:36:06] "GET /accounts/login/ HTTP/1.1" 200 3402
[15/Oct/2013 05:36:06] "GET /static/css/bootstrap-responsive.min.css HTTP/1.1" 404 2122
[15/Oct/2013 05:36:19] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 2089
[15/Oct/2013 05:36:20] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 2089
I'v read the Django docs and tried several alternative settings but none did help to resolve the problem. So appreciate your hints.
url.spy
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
#url(r'^$', 'myproj.views.home', name='home'),
# url(r'^myproj/', include('myproj.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url (r'^accounts/', include('registration.backends.default.urls')),
)
Staticfiles in Django seems to always confuse people who are just starting out, and imo, the documentation could be improved.
Here's a quick breakdown of what's what:
MEDIA_ROOT: The base directory where files you upload as part of a form or model form go. The upload_to property of a File/ImageField is appended to this path.
MEDIA_URL: The URL pattern that files located in MEDIA_ROOT are accessible via.
STATIC_ROOT: The directory that holds static files to be served in production.
STATIC_URL: The URL pattern that files located in STATIC_ROOT are accessible via.
STATICFILES_DIRS: The director(y/ies) that hold static files to be served in development.
When you have the staticfiles app in your INSTALLED_APPS, it will only serve static files located in STATICFILES_DIRS when DEBUG = True.
When you run $ python manage.py collectstatic, it will gather any static directories for your application, and any 3rd party applications and copy them to STATIC_ROOT.
So, to fix your paths to static files in production, make sure your settings are correct and that you've run collectstatic either before you deploy or as part of your deployment process.
There is also a new template tag for static files:
{% load i18n %}
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
...
<link href="{% static 'css/bootstrap.min.css' %}"
rel="stylesheet">
<link href="{% static 'css/bootstrap-responsive.min.css' %}"
rel="stylesheet">
</head>
<body>
...
In your project structure, re-name "/static" to "/static-assets" and set that directory in STATICFILES_DIRS.
Change your STATIC_ROOT to point to "/static", and don't put any directories or files there yourself. Let the collectstatic management command do that.
Here's how I typically structure a project:
/my_app/
/my_app/
__init__.py
settings.py
urls.py
manage.py
/another_app_module/
__init__.py
models.py
...
/static/
/static-assets/
/css
/js
/images
/templates
Given this file structure, and assuming my project sits at: /home/btaylor/django-projects/my_app/ my settings would be:
STATIC_ROOT = '/home/btaylor/django-projects/my_app/static'
STATIC_URL = '/static/'
MEDIA_ROOT = '/home/btaylor/django-projects/my_app/static/uploads'
MEDIA_URL = '/static/uploads/'
STATICFILES_DIRS = ('/home/btaylor/django_projects/my_app/static-assets',)
Related
I tried to ling CSS file in Django framework by using " "
It shows error
but, it is showing error ""GET /static/css/stylesheet.css HTTP/1.1" 404 1813file Oder"
define STATIC_URL in settings.py like that:
STATIC_URL = 'static/'
and template file must be like that
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'app/css/style.css' %}">
Did you use following command after adding static files?
python manage.py collectstatic
My site on django doesn't load static files
base.html:
{% load static %}
<link rel="stylesheet" href="{% static 'bootstrap/dist/css/bootstrap.css' %}">
<script src="{% static 'jquery/dist/jquery.min.js' %}"></script>
<script src="{% static 'bootstrap/dist/js/bootstrap.min.js' %}"></script>
files:
from terminal:
[13/Mar/2022 03:36:26] "GET / HTTP/1.1" 200 24975
[13/Mar/2022 03:36:26] "GET /static/bootstrap/dist/css/bootstrap.css HTTP/1.1" 404 1758
[13/Mar/2022 03:36:26] "GET /static/jquery/dist/jquery.min.js HTTP/1.1" 404 1751
[13/Mar/2022 03:36:26] "GET /static/bootstrap/dist/js/bootstrap.min.js HTTP/1.1" 404 1760
[13/Mar/2022 03:36:26] "GET /media/cache/77/1c/771c04f6935d264617dd3dec309a41d0.jpg HTTP/1.1" 404 1773
What could be be the reason of this?
Alxender, you should write these link and scripts in the base.html file and whenever you want static in a file write {% load static %} each time in each file.
You should also run this code in the terminal before running the server
python manage.py collectstatic
You should also check if you have set these settings in project/settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (str(BASE_DIR.joinpath('static')),)
STATIC_ROOT = str(BASE_DIR.joinpath('staticfiles'))
STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
]
From the comments:
With debug= false django will not serve static files. That is intended behavior. > See docs.djangoproject.com/en/4.0/howto/static-files
Thanks to #Razenstein for answer!
I am struggling with django static files. I want to link "screen.css" stored in "all_statics/css" folder to "index.html".
Here is my html file :
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://fonts.googleapis.com/css2?family=Gwendolyn:wght#700&display=swap" rel="stylesheet">
<link href="{% static '/css/screen.css' %}" rel="stylesheet" >
</head>
<body>
<h1>Test</h1>
</body>
</html>
In my settings.py, I have set these variables :
STATIC_URL = 'static/'
STATICFILES_DIR = [
BASE_DIR / 'all_statics',
]
STATIC_ROOT = BASE_DIR / 'static'
Here is my project structure :
Project structure
When I run python manage.py collectstatic , the static files from admin app is copied to static folder, but not the files from 'all_statics' .
When I run python manage.py runserver and browse to the index page, I see that the css can't be found (404) :
[18/Jan/2022 11:30:26] "GET / HTTP/1.1" 200 308
[18/Jan/2022 11:30:27] "GET /static/css/screen.css HTTP/1.1" 404 1801
When I run python .\manage.py findstatic css/screen.css I get : No matching file found for 'css/screen.css'.
I have created a sample project to show my problem. You can clone it from https://github.com/doolieSoft/django_project.git .
Do you have an idea why my css can't be found ?
Thanks for your help !
I've fired up a new Django 1.8 project and am hitting the wall of trying to serve static files in the development environment. I'm pretty sure I've followed the docs explicitly, but it's just not working.
Settings.py
STATICFILES_DIR = (
os.path.join(BASE_DIR, 'static/'),
BASE_DIR
)
STATIC_URL = '/static/'
STATIC_ROOT = '/srv/www/cpm2/static/'
urls.py:
from django.conf.urls import include, patterns, url
from django.conf import settings
from django.conf.urls.static import staticĀ·
from django.contrib import admin
from django.views.generic import TemplateView
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^test/', TemplateView.as_view(template_name="basics/base.html")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
templates/basics/base.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
{% load staticfiles %}
<link href="{% static 'css/bootstrap.css' %}" rel="stylesheet">
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>
When I go to http://my_server_ip:8000/test/ none of the CSS is loaded. Viewing the page source, I see this where that CSS link is <link href="/static/css/bootstrap.css" rel="stylesheet"> which is good, but when I try to follow that link directly, it fails giving me a 404 error.
Now, I've set up my Apache server to serve the files directly, they work. So, going to http://my_server_ip/static/css/bootstrap.css works - so I know the files are readable. It's just they don't work in development.
What am I doing wrong? As so many others before me, I'm pulling my hair out!
EDIT Trying to access the file directly gives me this:
Page not found (404)
Request Method: GET
Request URL: http://my_server_ip:8000/static/css/bootstrap.css
Raised by: django.views.static.serve
'css/bootstrap.css' could not be found
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
It doesn't even show a list of places its tried, just that the file isn't found.
You are missing an 's' from STATICFILES_DIRS.
Also, you shouldn't be including your BASE_DIR in that setting - that could end up serving all your python code, which would be a bad idea.
I have two templates index.html and search_results.html in the same directory.
Both have the following code snippet
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}bootstrap/css/bootstrap.min.css" media="screen" />
The css file loads on the index template:
"GET /static/bootstrap/css/bootstrap.min.css HTTP/1.1" 304 0
However, does not for the search_results template:
"GET /search/static/bootstrap/css/bootstrap.min.css HTTP/1.1" 404 2133
Since it includes the /search.
I'm wondering how to omit the /search/ portion in trying to load the css file in my second template, since that template all have urls starting with /search/.
my urls.py:
urlpatterns = patterns('',
url(r'^$', 'home.views.index'),
url(r'^search/$', 'home.views.film_chart_view')
)
Clearly, you are missing the slash / at the beginning of the static URL.
The dirty fix:
Change the line in your template to
<link rel="stylesheet" type="text/css" href="/{{ STATIC_URL }}bootstrap/css/bootstrap.min.css" media="screen" />
The clean fix:
Add a slash at the beginning of the STATIC_URL variable in your settings file. It must probably look like this:
STATIC_URL = '/static/'