How to path CSS file in Django? - python

When I access to this static file by just writing path after 'localserver:8000' is works but not working on html file. Even html loads image from image folder in static but CSS is in not applying on that html file. I am stuck for approximately 3 hours please help....
SETTINGS.PY>INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'multiselectfield',
'shop',
'colorfield',
]
STATC CODE
STATIC_URL = '/static/'
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'static')
]
MEDIA_URL = ''
MEDIA_ROOT = BASE_DIR
HTML
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/base-style.css' %}">

It solved by just restarting all apps.

Related

Python Django can't get static files to work

I'm working on a pyhton django project and now I'm trying to add some style to it with a styles.css file but I just can't get it to work.
My project is named commerce and my app is called auctions and I have the static file under
commerce/auctions/static/auctions/styles.css
My settings.py file include
INSTALLED_APPS = [
'auctions',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
My auctions/urls.py
from django.urls import path
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views
urlpatterns = [
path("", views.index, name="index"),
]
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns()
My styles.css file
h1 {
color: red;
}
Then in my template file, I have this
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}Auctions{% endblock %}</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="{% static 'auctions/styles.css' %}">
</head>
<body>
<h1>Auctions</h1>
</body>
</html>
It feels like I'm missing something obvious.
EDIT:
So I managed to fix my issue, and it was the STATIC_URL in my settings.py that was wrong, it looked in the wrong place. I updated it to:
STATIC_URL = 'auctions/static/'
And now it works. Thanks for your replies and help.
Edit your settings.py file and add WhiteNoise (check here the lib) to the MIDDLEWARE list, above all other middleware apart from Django’s SecurityMiddleware:
MIDDLEWARE = [
# ...
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
# ...
]
You should also add this to the settings.py:
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
For more details,check Using WhiteNoise with Django guide.
Addding this in your settings.py should solve the issue:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]

HTML is not applying CSS

I am very new to HTML and CSS. I am trying to link my CSS and JS file to my HTML file, but it is not working. I have looked at many other Stack Overflow questions & answers on this problem, and none of the solutions seemed to have worked. Is there some sort of typo/problem that I am overlooking?
website
|home
|_static
|_home
|_styles.css
|_main.js
|_templates
|_home
|_base.html
This is the top of my base.html file
<head>
{% load static %}
<link rel="stylesheet" href="{% static '../home/styles.css' %}">
<script src="{% static 'home/main.js' %}"></script>
<link href="https://fonts.googleapis.com/css?family=Poppins&display=swap" rel="stylesheet">
<meta charset="UTF-8">
<title>Title</title>
</head>
Here is the relevant code from settings:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
and later
STATIC_URL = '/static/'
Here are the error messages in Command Prompt:
[05/Jun/2019 13:58:15] "GET /home/ HTTP/1.1" 200 1014
Not Found: /home/styles.css
[05/Jun/2019 13:58:15] "GET /static/home/main.js HTTP/1.1" 404 1660
[05/Jun/2019 13:58:15] "GET /home/styles.css HTTP/1.1" 404 2304
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
If this is your entire installed apps, it looks like you haven't installed your own app, and that means staticfiles apps doesn't check that directory since it is not an installed app.Adding your app here would add it to the searched paths and django-dev-server would serve the statics.
change your ../ like this:
<link rel="stylesheet" href="{% static 'home/styles.css' %}">

Django Invalid block tag: 'static' means what exactly?

(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" />

Can't load .css and image files in django

I can't load images and css files in django, although everything seems to be fine in settings.py and in the home.html file itself... what can be a problem here?
In the static folder there are template, css and image folders.
HTML:
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'css/main.css' %}">
<title>Main Page</title>
settings.py:
# Static files (CSS, JavaScript, Images)
MEDIA_ROOT = os.path.join(BASE_DIR, "/media")
#MEDIA_URL = ''
#STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "/static")
]
STATIC_URL = "/static/"
Unfortunately, it looks like this:
django.contrib.staticfiles is not included in your INSTALLED_APPS.
Your settings.py should look like this (in INSTALLED APPS)
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sites',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
without that static files won't work even if everything else is setup properly because to django it's not installed even though it is (Technically).

Django 1.8 cannot locate static css file

On my development env, I get this error when trying to access static css file:
Page not found (404)
Request Method: GET Request URL:
http://127.0.0.1:8000/static/styles.css
Some lines from the settings.py:
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/'
Path to my styles.css file (where eshop is a root folder of my project):
/eshop/static/styles.css
Relevant lines from my html file:
{% load staticfiles %}
<head>
<link rel="stylesheet" type="text/css" href="{% static 'styles.css' %}" />
...
Could you please advice on what i'm doing wrong?
Add the following to your settings.py file:
# define global static
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Or just put the static files inside each app:
app_folder/static/app_name

Categories

Resources