2017-07-06 6 views
1

Ich erstelle eine Website mit Django und aus irgendeinem Grund hat meine CSS-Datei keinen Einfluss auf die Seite. Ich habe überprüft, um sicherzustellen, dass meine STATIC_URL definiert ist, aber immer noch kein Glück.Warum lädt Django mein CSS nicht?

Mein settings.py:

# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.11/howto/static-files/ 

STATIC_URL = '/static/' 
STATIC_ROOT = os.path.join(BASE_DIR, 'static') 

Innerhalb von meinem Blog app habe ich ein statisches Verzeichnis

blog 
    | 
    static 
    | 
    css 
     | 
     blog.css 

Mein HTML doc:

{% load staticfiles %} 
<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <title>Medicare Supplemental info</title> 
     <meta charset="utf-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> 
     <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css"> 
     <!-- This is where I'm loading the CSS file --> 
     <link rel="stylesheet" href="{% static 'css/blog.css' %}"> 
    </head> 

ich sicherstellen, dass geprüft Ich habe die erforderliche App im settings.py installiert:

INSTALLED_APPS = [ 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'blog', 
] 

Ich habe auch versucht, die Art, wie ich lade statische Dateien ändert:

{% load staticfiles %} 

zu:

{% load static %} 

noch kein Glück. Was mache ich falsch?

Antwort

1

Ich glaube, Sie verpassen in urls.py:

from django.conf import settings 
from django.conf.urls.static import static 

urlpatterns = [ 
    # ... the rest of your URLconf goes here ... 
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

Diese Arbeit in dev, in der Produktion Sie mit manage.py und dienen Statik mit nginx (oder Apache) collectstatic müssen.

+1

Yup, dass mein Problem behoben. Ist das etwas Neues? Ich glaube nicht, dass ich dies in der letzten Django-Site, die ich erstellt habe, machen musste. –

+0

Dies ist nicht neu, wenn Sie Version 1.7 sehen. Es ist dort. https://docs.djangoproject.com/de/1.7/howto/static-files/#serving-static-files-during-development –