Django Serving Static Files not loading - python

Setting up a new django application with the following settings:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DEBUG = True
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'kb/media')
STATIC_ROOT = os.path.join(BASE_DIR, 'kb/static/')
Setup a template
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title>HomePage</title>
</head>
<body>
<img src="{% static 'img/girl.png' %}">
<img src="/media/boy-512.png">
</body>
</html>
I assume there's an issue with the settings of how it's supposed to get the static files. In this case the directory for the static image girl.png is kb\kb\static\img\girl.png

If I can help you, this is my settings.py file and one example in my html template :
I have a project : MyProject and one application : my_project (inside I have static document > images documents > test.png file)
My settings.py file looks like :
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "my_project/static"),)
And my HTML template file with image looks like :
<img src="{% static 'images/test.png' %}" />
Hopfully to help you

add your project name in settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'yourprojectname',]

Related

Css not loading in Django

My css file is not getting loaded in the webpage. I have css and image file in the same location. The image is getting loaded but not the css.Also I have included the directory in staticfile_dirs.
Setting.py
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'technicalCourse.apps.TechnicalcourseConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'website.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
r'C:\Users\Kesavan\PycharmProjects\Django web development\website\technicalCourse\template'
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'website.wsgi.application'
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
r'website\technicalCourse\static',
]
This the template file
<!DOCTYPE html>
{% load static %}
<head>
<link rel="stylesheet" type="text/css" href="{% static 'css/simple.css' %}">
</head>
<body>
<img src="{% static 'image/img.png' %}">
<h1>Welcome to course on programming</h1>
<ol>
{% for x in ac %}
<li>{{x.courseName}}</li>
{% endfor %}
</ol>
</body>
For testing I simply change the color of the h1 tag alone. The css file.
h1{
color:black;
}
Structure
C:.
├───migrations
│ └───__pycache__
├───static
│ ├───css
│ └───image
├───template
│ └───technicalCourse
└───__pycache__
But there is no reflection on the webpage.
Hoping for the solution.
Thanks in advance.
try this command:
python manage.py collectstatic
and check again.
It's weird that images are working and CSS isn't. There could be a multitude of possibilities for your problem.
The simplest way to solve this is to set the path to the CSS files via an absolute or a relative path.
Relavtive path case
<link href="/static/ui/css/base.css" rel="stylesheet">
I was suffering with same problem but solved by adding the
{% load static %}
{
<link rel="stylesheet" type="text/css" href="{% static 'thumbnail_searcher/style.css' %}">
}
in same tag. For example in your case add load and link into <head> tag.
clear browser cache
in settings.py change STATIC_URL = "static/" to STATIC_URL = "/static/"
run python manage.py runserver port_number(optional)
Try
$ python manage.py findstatic css/style.css
instead of css/style.css add your own path
If Django project able to locate the CSS, it will return path to your CSS file.
Then restart your project and it should work.

Django doesn't see static files

So the thing is that django simply can't recognize the static files. No matter what have I tried it just doesn't work. I tried changing the from global searching way to a direct url of the css file, none of this seems to work.
The structure of my project looks like this:
Here is my code:
DEBUG = True
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
STATIC_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
from django.contrib import admin
from django.urls import path
from something.views import *
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', home),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Monkeys</title>
<link rel="stylesheet" type="text/css" href="{% static 'css/file.css' %}"/>
</head>
Could find the answer in any topic on the internet so I'm hoping you could help.
Go some steps back and set up static files and make sure it follows exactly django docs
Only about static files
This project has similar structure as yours
When collectstatic is run, the default STATICFILES_FINDERS value django.contrib.staticfiles.finders.FileSystemFinder will collect your static files from any paths that you have in STATICFILES_DIRS.
The other default STATICFILES_FINDERS value django.contrib.staticfiles.finders.AppDirectoriesFinder will look in the /static/ folder of any apps in your INSTALLED_APPS.
All of the static files that are found will be placed in the specified STATIC_ROOT directory.
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
You can also use python manage.py findstatic to see which directories collectstatic will look in.
manage.py findstatic
Currently you have a typo - STATIC_DIRS instead of STATICFILES_DIRS, so collectstatic is not configured at all currently.
Fix it, then run collectstatic again.

TemplateSyntaxError with static django 2

I have this code:
HTML
{% load static %}
<!--[if lte IE 8]><script src="{% static game_reviews/css/ie/html5shiv.js %}"></script><![endif]-->
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'game_reviews',]
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',]
STATIC_URL = '/static/'
i had a folder inside my app called static/game_reviews, and inside have multiple folder (js, css, etc.).
and i getting this error :
TemplateSyntaxError at /game_reviews/
Could not parse the remainder: '/css/ie/html5shiv.js' from 'game_reviews/css/ie/html5shiv.js'
Any help?. Thanks in advance.
by the django static-files, you should send path as parameter:
{% load static %}
<!--[if lte IE 8]><script src="{% static "game_reviews/css/ie/html5shiv.js" %}"></script><![endif]-->
<!-- ^^ ^^-->

Django 1.11 is not loading css files

I have the next configuration:
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^manager/', include('manager.urls')),
#Static pages
url(r'^index', TemplateView.as_view(template_name='index.html'), name='index'),
url(r'^', TemplateView.as_view(template_name='index.html'), name='index'),
url(r'^contact', TemplateView.as_view(template_name='contact.html'), name='contact'),
url(r'^services', TemplateView.as_view(template_name='services.html'), name='services'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'pf.app.pfs',
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "assets"),
#'/var/www/static/',
]
I executed the command:
python manage.py collectstatic
And the files are generated, also I added a css file with a single rule but and executed the command again.
But, in the momento to add it to my html file
<head>
{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}" />
</head>
in the html I get:
<link rel="stylesheet" href="/static/css/style.css">
And in the terminal:
"GET /static/css/style.css HTTP/1.1" 404 1766
What I have wrong in my static files configuration?

page not found when serving static files via django

so in settings.py I have
STATIC_URL = '/static/'
and also
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp.apps.MyappConfig'
]
hence as you can see django.contrib.staticfiles is there and moreover Debug = True
and in my app directory I have a file existing in this path
myapp/static/css/myapp.css
However, when I go to http://localhost/myapp/static/css/myapp.css
it ends up returning
Page not found (404)
Request Method: GET
what am I doing wrong and how can I get django to serve static files properly?
Include this in your project/urls.py
from django.conf import settings
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Use the option STATICFILES_DIRS in your settings.
An example:
# settings.py
...
STATICFILES_DIRS = [
"/path/to/your/static/files/folder",
]
...
In your templates, use the static template tag like
# some_page.html
...
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image"/>
...
or hard-code like
# some_page.html
...
<img src="/static/my_app/example.jpg" alt="My image"/>
...

Categories