2014-02-17 106 views
1

我試過了很多東西,但只是不能提供css和圖像。我試圖通過CSS呈現背景圖像。以下是代碼的網站Django 1.5.4無法在開發服務器上服務css

simple_emp_hr/

|->hr_base/ 
|  |-> views.py 
|  |-> templates/hr_base/homepage.html 
|  |-> static 
|   |->css/base.css 
|   |->images/foto-camion-transformers.jpg 
| 
|->simple_emp_hr/ 
     |->urls.py 
     |->settings.py 

以下爲觀點:

@csrf_protect 
def index(request): 
    print(request.GET) 
    print(request.POST) 
    template = loader.get_template('hr_base/homepage.html') 
    c = Context() 
    c.update(csrf(request)) 
    c.update({'user':request.user.username}) 
    str = template.render(c) 
    response = str 
    print(str) 
    hres = HttpResponse() 
    hres.write(response) 
    return hres 

以下是我的urls.py:

from django.conf.urls import patterns, include, url 
from django.conf import settings 
# Uncomment the next two lines to enable the admin: 
from django.contrib import admin 
admin.autodiscover() 

urlpatterns = patterns('', 
    url(r'^hr_base/index$','hr_base.views.index'), 
) 
urlpatterns += patterns('', 
    (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root':   settings.STATIC_ROOT, 'show_indices':True}), 
) 

以下是我的settings.py:

# Django settings for simple_emp_hr project. 

DEBUG = True 
TEMPLATE_DEBUG = DEBUG 

MANAGERS = ADMINS 


ALLOWED_HOSTS = [] 

SITE_ID = 1 

MEDIA_ROOT = '' 

MEDIA_URL = '' 

STATIC_ROOT = 'static_root/' 

STATIC_URL = '/static/' 

# Additional locations of static files 
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
"hr_base/static/hr_base/", 
) 

# List of finder classes that know how to find static files in 
# various locations. 
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
# 'django.contrib.staticfiles.finders.DefaultStorageFinder', 
) 

# List of callables that know how to import templates from various sources. 
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader', 
    'django.template.loaders.app_directories.Loader', 
#  'django.template.loaders.eggs.Loader', 
) 

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    # Uncomment the next line for simple clickjacking protection: 
'django.middleware.clickjacking.XFrameOptionsMiddleware', 
) 

ROOT_URLCONF = 'simple_emp_hr.urls' 

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
"../simple_emp_hr/hr_base/templates/", 
"../simple_emp_hr/emp_users/templates/", 
) 

INSTALLED_APPS = (
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    # Uncomment the next line to enable the admin: 
    'django.contrib.admin', 
    # Uncomment the next line to enable admin documentation: 
    # 'django.contrib.admindocs', 
    'hr_base', 
) 

我的CSS文件有:

body { 
    background-image: url ("images/foto-camion-transformers.jpg"); } 
title { 
    background-color: rgb(238,62,128); 
    color: white; } 

以下是homepage.html:

<!DOCTYPE html> 
<html> 
<head> 
    <title> Simple Employee HR : Solutions for the companies which are <em>simple</em></title> 
    <link href="{{ STATIC_URL }}css/base.css" type="text/css" rel="stylesheet" /> 
</head> 
<body> 
    <h1>Simple Employee HR : Solutions for the companies which are <em>simple</em></h1> 
    <hr /> 
    <h2 id="top">Who we are</h2> 
    <hr /> 
    <a href="#top">top</a> 
</body> 
</html> 

在查看打印可以讓我打印呈現的HTML:

<!DOCTYPE html> 
<html> 
<head> 
    <title> Simple Employee HR : Solutions for the companies which are <em>simple</em></title> 
    <link href="css/base.css" type="text/css" rel="stylesheet" /> 
</head> 
<body> 
    <h1>Simple Employee HR : Solutions for the companies which are <em>simple</em></h1> 
    <hr /> 
    <h2 id="top">Who we are</h2> 
    <hr /> 
</body> 
</html> 
[17/Feb/2014 14:40:17] "GET /hr_base/index HTTP/1.1" 200 1455 
[17/Feb/2014 14:40:17] "GET /hr_base/css/base.css HTTP/1.1" 404 3640 

我從來沒有得到的CSS呈現。瀏覽器顯示純HTML文本。什麼可能是錯的? 我是django新手,想學習。我在這裏無能爲力,無法理解django的文檔。

編輯: 我能夠得到css工作後的pythonvile的答案,但它仍然奇怪。 我能看到的

h1, h2 { 
    color: #ee3e80;} 

的影響,但我不能看到

h1 { 
    background-color: rgb(238,62,128); 
    color: white; } 

效果
body { 
    background-image: url ("images/foto-camion-transformers.jpg"); } 
title { 
    background-color: rgb(238,62,128); 
    color: white; } 

回答

2

您在定義MEDIA_ROOT,STATIC_ROOT,STATICFILES_DIRS和TEMPLATE_DIRS時需要使用絕對路徑。良好做法是開始設置模塊(它沒有成爲一個單一的文件!)像這樣的東西:

import os 
PROJECT_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..') 

是啊是啊,我知道它的外觀,最可怕的一件Python中的我至今遇到過。但這是一條路。讓我們來看一個外殼:

In [1]: from django.conf import settings 

In [2]: settings.PROJECT_DIR 
Out[2]: '/home/yourname/Projects/simple_emp_hr/simple_emp_hr/..' 

這相當於/home/yourname/Projects/simple_emp_hr對不對? 現在我們可以再次使用os.path.join膠水迪爾斯在一起:

In [3]: import os 

In [4]: os.path.join(settings.PROJECT_DIR, 'foo', 'bar') 
Out[4]: '/home/yourname/Projects/simple_emp_hr/simple_emp_hr/../foo/bar' 

在這裏,我們走在實際設置:

STATIC_ROOT = os.path.join(PROJECT_DIR, 'static') # you will have something different, I guess 
STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR, 'hr_base', 'static'), 
) 

同樣的系統訓練的MEDIA_ROOT。

編輯:我的網址爲靜態片段。我一天無恥地從一些博客上拿回來。

from django.conf import settings 
from django.conf.urls import patterns, include, url 
from django.contrib import admin 
admin.autodiscover() 

urlpatterns = patterns('', 
    # normal patterns 
) 

if settings.DEBUG: 
    urlpatterns += patterns('', 
     url(r'^static/(?P<path>.*)$', 'django.views.static.serve', 
      {'document_root': settings.STATIC_ROOT, 'show_indexes': True}), 
     url(r'^media/(?P<path>.*)$', 'django.views.static.serve', 
      {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}), 
    ) 
+0

檢查我的控制檯上:[18/Feb/2014 12:01:04]「GET /hr_base/css/base.css HTTP/1.1」404 3640。它沒有意義 – preetam

+0

它應該從靜態開始。它很容易混淆哪個目錄被哪個url訪問。你能檢查我之前發佈的url文件,並讓我知道如果那可以嗎? – preetam

+0

這變得更糟了,我將html轉儲到控制檯進行調試。 {{STATIC_URL}}在那裏變得沒有任何東西。它的字面上沒有。 – preetam

-2

你應該考慮使用Django的壓縮機, http://django-compressor.readthedocs.org/en/latest/ 。 它完全值得學習曲線。解決了很多問題

+0

沒有。在查看備選方案之前,OP需要學習如何使用基礎知識。無論如何,這不是一個答案,它應該是一個評論 – yuvi

相關問題