D
D
dstdfx2015-07-30 15:32:31
css
dstdfx, 2015-07-30 15:32:31

Why doesn't CSS connect to Django?

Good day, the second day I can not figure it out. The organization of the project is as follows:

mysite ---
       \  static ---
       		   | my_css.css

       \  templates ---
       	    |base.html

settings in settings.py file:
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, "mysite","media")
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, "mysite", "static")

base.html file:
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>{% block title %} {% endblock %}</title>
  <link rel="stylesheet" href="{{ STATIC_URL }}my_css.css"> 
</head>
<body>
  <h1>My site current time</h1>
  {% block content %} {% endblock content %}
  {% block footer %}
  <hr>
  <p>Thank you for your come.</p>
  {% endblock %}
</body>
</html>

my_css.css :
h1 { 
    font-size: 120%; 
    font-family: Verdana, Arial, Helvetica, sans-serif; 
    color: red; 
   }

Browser console : Failed to load resource: the server responded with a status of 404 (NOT FOUND)
System console :
[30/Jul/2015 15:11:11]"GET /time/ HTTP/1.1" 200 292
[30 /Jul/2015 15:11:11]"GET /time/static/my_css.css HTTP/1.1" 404 2961
[30/Jul/2015 15:13:24]"GET /time/static/my_css.css HTTP/ 1.1" 404 2961

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Abdulla Mursalov, 2015-07-30
@dstdfx

for example, the code from settings.py, below in the picture is the directory structure and its location relative to the project root. put scripts and stuff in static_files

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static_root')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static', 'static_files'),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,  'static', 'media')

in the urls.py file at the end write
if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

All this is described in detail in the documentation.

U
un1t, 2015-07-30
@un1t

Well first compare
STATIC_URL = '/static/'
and
GET /time/static/my_css.css
Secondly, deal with STATIC_ROOT and STATICFILES_DIRS

R
Roman Kitaev, 2015-07-30
@deliro

Well, read
the docs https://docs.djangoproject.com/en/1.8/howto/static...

Z
zelsky, 2015-07-31
@zelsky

Where is the slash in the html code?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question