2

The website I see when I run the server has not CSS in it, but just the HTML. So far I read that you keep CSS files in a static folder under the project directory. I have an Html template that is in the templates folder and it works perfectly when I load it from the views.home . In the HTML file, I have {% load staticfiles %} at the top of the document, and yes I have checked my installed apps for 'django.contrib.staticfiles' in the settings. Also, in the HTML document, in the href attribute I've added {% static 'style/style.css' %} which is the name of the folder under the static folder.

<!DOCTYPE HTML>
{% load staticfiles %}
<html>

<head>
  <title>night_sky_2</title>
  <meta name="description" content="website description" />
  <meta name="keywords" content="website keywords, website keywords" />
  <meta http-equiv="content-type" content="text/html; charset=windows-1252" 
/>
  <link rel="stylesheet" type="text/css" href="{% static 'style/style.css' 
  %}" />
</head>

enter image description here

enter image description here

3 Answers 3

1

I finally found a solution. All I had to do was add this:

STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
'DjangoProjects\Sample\website\static'
)

In the settings and also moved:

{% load staticfiles %}

To the very top of the HTML document, instead of under. I put it under at first because it wasn't giving a red line which to me seems like there isn't an error.

Sign up to request clarification or add additional context in comments.

Comments

0

it seems that you have done right in your html template do load the static file. Have you done this configuration in your settings.py ?

STATIC_URL = '/static/'
STATIC_ROOT = 'static'

I've read in this doc that the { % loadstatic %} has been done as {% load static %} just.

See: django docs about sttic files

1 Comment

I've looked in my django projects and the only different thing in my code from yours is this slash in the path beginning before the "/style" <link rel="stylesheet" type="text/css" href="{% static '/style/style.css' %}" />
0

Its easier than you think, but almost everyone gets it wrong the first time. There was a lightning talk about it at the DjangoCon Europe a couple of days ago: https://youtu.be/eEZYDDaDeCs?t=29m50s

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.