2015-05-06 73 views
0

我是django的新手,我開始學習Django與官方教程。css不加載我的登錄和管理頁面在Django

我使用Django的virtualenv身邊,但我有一個問題 登錄頁面和管理頁面,因爲

他們不是沒有任何風格加載CSS和顯示的登錄頁面 和管理頁面

""" 
Django settings for mysite project. 

Generated by 'django-admin startproject' using Django 1.8.1. 

For more information on this file, see 
https://docs.djangoproject.com/en/1.8/topics/settings/ 

For the full list of settings and their values, see 
https://docs.djangoproject.com/en/1.8/ref/settings/ 
""" 

# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
import os 

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 


# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = 'g8%[email protected]!hzekoho4rn7r7-t_m!sk$*nwi-4j556t=!ln3(@+' 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 

ALLOWED_HOSTS = [] 


# Application definition 

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'polls', 
) 

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
    'django.middleware.security.SecurityMiddleware', 
) 

ROOT_URLCONF = 'mysite.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'mysite.wsgi.application' 


# Database 
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', 
     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 


# Internationalization 
# https://docs.djangoproject.com/en/1.8/topics/i18n/ 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 


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

STATIC_URL = '/static/' 
+0

參考以下鏈接: http://stackoverflow.com/questions/28728912/django-admin-site-not-showing -css-style –

+0

即時通訊不調用任何地方,我只使用教程Refrence –

+0

你在談論什麼時候使用'manage.py runserver'? –

回答

0

您需要閱讀documentation for serving static files。當你在製作時,你的web服務器(nginx/apache)將負責提供靜態文件,如JS,CSS和圖像。因此,任何對圖像或JS文件的請求都將由網絡服務器立即處理,而對實際頁面的任何請求將傳遞到您的應用程序服務器(即Django)

在開發中,您需要告訴開發服務器實際上滿足您的靜態文件,所以您需要添加以下到你的根urls.py文件:

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

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

首先,創建命名爲「靜態」,那麼你需要複製所有的CSS/JS文件爲靜態文件夾文件夾(或者你創建靜態文件夾的地方)。然後在你的settings.py

聲明靜態文件的目錄路徑在HTML文件中,添加以下行 -
{% load staticfiles %}
在文件的開頭部分或頂部的頂部。
<head>部分,你可以使用鏈接的CSS/JS文件下面的代碼

<link rel="stylesheet" href="{% static 'assets/css/mystyle.css'%}"> 
</link> 
<script src="{% static 'assets/js/jquery.js'%}"></script>