I am pretty new to Django and I am trying to link CSS file with my HTML document and I have tried using all possible ways posted on StackOverflow and I am unable to locate what is the issue here.
Here is the directory and file structure:
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
index.html
{% load static %}
<link rel="stylesheet" type="css/text" href="{% static 'css/style.css' % }">
Thanks in advance.
Ops. Silly mistake. It was all about indentation and minor type tag changes. Here is what worked out for me.
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">
Related
I just got started in Django a few days ago from Ruby and have only run into one really annoying problem that I just can't seem to figure out on my own. I've tried everything I can think of to no avail.
I am trying to serve up two static files, a custom CSS file (style.css) and a bootstrap.min.css file.
While this should be very easy as everybody keeps telling me, I must be staring at the outside of the box because I can't fix it. I would like to note, it does not work in both live (which I don't expect it too because I don't have a root set) or local environments. It currently will only serve up bootstrap.min.css
EDIT:
When collectstatic is run, it passes through. I have set STATIC_ROOT to 'STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
my settings file:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
My Main Template:
<head>
<meta charset="utf-8">
<title>{% block title %}Django Boards{% endblock %}</title>
<link href="https://fonts.googleapis.com/css?family=Peralta" rel="stylesheet">
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
My File Structure:
Main Project
boards
project1
static
css
bootstrap.min.css (loads first files)
style.css (wont load second file)
templates
manage.py
db.sqlite3
{% load staticfiles %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="{% static 'search/style.css' %}">
This is the code and I am getting error in loading static file in this part
href="{% static 'search/style.css' %}
error cannot resolve directory "{%static search
Are your settings pointing to that (search/style.css)directory? You'll need something like the following in your app's settings.py:
...
STATIC_URL = '/search/'
STATIC_ROOT = os.path.join(BASE_DIR, 'search')
Ok I am new to Python, I am using Django here.
I cannot get my Python to see the CSS file and use it. I have tried hardcoding the path, server restarts, nothing works. I can get it to read a Bootstrap CDN however, I am not sure what is wrong.
My File structure is like so:
-migrations
-static
--music
---images
---style.css (the file that i'm trying to get)
-templates
--music
---index.html (where my link attribute is)
I am trying to load the static CSS file in index.html like so:
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href= "{% static 'music/style.css' %}"/>
Here is the CSS:
body {
background-color: red;
}
Thanks in advance!
Add following code in setting.py file
STATIC_ROOT = os.path.join(BASE_DIR,"deploy_to_server")
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
And use in your template
<link rel="stylesheet" type="text/css" href= "{% static 'music/style.css' %}"/>
Create static folder in your project
==> static ==> music ==> style.css
==> manage.py
Hope this helps you
I have a little problem with Django , it is not my static files.
I have read the documentation , I tried putting the path directly , use {{ STATIC_URL }} and most recommended method is to leave the details. Any idea how to fix it ?
template.html:
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'css/normalize.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'css/demo.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'css/component.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'css/cs-select.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'css/cs-skin-boxes.css' %}" />
local.py (settings)
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR2.child('static')]
my file folder in the right direction
The first folder is the virtualenv, the second folder is for Django Project where is the manage.py (image)
Views, models, urls and forms working properly, and display the chosen template
[The python code working properly (image)][2]
Update
I try change STATICFILES_DIRS for this:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), 'static', ),
)
And for and printed the path to make sure it is correct, but the the error continues.
The result of print
Thanks in advance
I've resolved for Django 1.9 is: STATICFILES_DIRS = (BASE_DIR, 'static')
To start, I would make STATICFILES_DIRS a tuple.
Next, I would try a format like this:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), 'static', 'static_dirs'),
)
To make sure it is going to the right directory, print the path and adjust accordingly:
print os.path.join(os.path.dirname(BASE_DIR), 'static', 'static_dirs')
Hope that helps!
I have solve that problem on mac by using:
STATICFILES_DIRS = [os.path.join(os.path.dirname(__file__), 'static').replace('\\','/'),]
I am using django-compressor to compress css files.
I did as it was explained in http://django-compressor.readthedocs.org/en/latest/quickstart/
I changed my template file as follows:
{% load staticfiles %}
{% load compress %}
<!DOCTYPE html>
<html lang="en">
<head>
{% compress css %}
<link href="{% static "crsq/css/zippednewsapp/bootstrap.readable.min.css" %}" rel="stylesheet">
<link href="{% static "crsq/css/zippednewsapp/zippednewsapp.css" %}" rel="stylesheet">
<link rel="stylesheet" href="{% static "crsq/css/zippednewsapp/typeahead.css" %}"/>
{% endcompress %}
</head>
.....
I do not see any change what so ever. No error. The files are not compressed. How do I change this?
In my settings file:
DEBUG = False
TEMPLATE_DEBUG = False
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
)
Appreciate any help. Thanks
If html didn't change, I'd check server restart after the changes.
If not, you can increase logging level and see what does the compressor module print into the logs.
Also,the compressor should run under a user with enough privileges to create files and folders under COMPRESS_ROOT (which defaults to STATIC_ROOT)
Regards