9

我很陌生django,并且已经学习了很多天的教程,我已经开始使用 django 构建一个小型网站,并尝试css通过在文件中安排所有必要的设置来提供settings.py文件。但不幸的是,我的代码无法提供 css 文件,我的意思是提供 css 文件的概念不起作用。我用谷歌搜索了很多,并浏览了 django 主文档教程并根据它们进行了更改,但仍然无法正常工作,所以SO我将整个代码粘贴在下面

项目文件夹结构

 personnel_blog
      |____personnel_blog
      |____manage.py  |  
                      |____media
                      |____static
                      |       |____css
                      |             |____personnel_blog_hm.css 
                      |____template 
                      |        |____home_page.html
                      |____settings.py
                      |____urls.py
                      |____views.py
                      |____wsgi.py         

我的一些 settings.py 文件设置如下

设置.py

import os
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
DEBUG = True
MEDIA_ROOT = os.path.join(PROJECT_DIR,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
                    os.path.join(PROJECT_DIR,'static'),
    # 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.
)
TEMPLATE_DIRS = (
                 os.path.join(PROJECT_DIR,'templates')
)
TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.contrib.auth.context_processors.auth',
    'django.contrib.messages.context_processors.messages',
)

网址.py

from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib import admin
admin.autodiscover()

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

)

视图.py

from django.shortcuts import render_to_response

def home_page(request):
    return render_to_response("home_page.html")

home_page.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
  <head>
    <link rel="stylesheet" href="{{ STATIC_URL }}css/personnel_blog_hm.css" type="text/css">
  </head>
  <body>
   <p>Hello !</p>
   <a href="/" target="_top">Home</a>
  </body> 
</html>  

person_blog_hm.css

body { background-color:green; }
p {color:blue;background-color:green;padding-left:20px;}

以上是我的代码,谁能告诉我 settigns.py 文件或其他 py 文件有什么问题吗?

是否需要在上面的代码中做任何其他额外的设置?

所以任何人都可以调整我的代码并进行必要的更改,以便我继续前进并迈出网页设计的第一步..... :)

4

4 回答 4

12

base.html

{% load static %}

<link rel="stylesheet" href="{% static 'css/personnel_blog_hm.css' %}" type="text/css">

设置

PROJECT_DIR  = os.path.dirname(__file__) 

MEDIA_ROOT = os.path.join(PROJECT_DIR,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'

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.
    os.path.join(PROJECT_DIR, 'staticfiles'),
)

网址

from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
     url(r'^$', 'personnel_blog.views.home_page'),
     url(r'^admin/', include(admin.site.urls)),
)+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()
于 2013-02-26T05:33:20.507 回答
1

尝试将您的STATICFILES_DIRS设置更改为

STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR,'static'),
)
于 2013-02-26T06:02:10.793 回答
0

settings.py尝试。

PROJECT_DIR  = os.path.dirname(__file__)
...
MEDIA_ROOT = os.path.join(PROJECT_DIR,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'

我的settings.py文件通常与静态目录位于同一目录中

于 2013-02-26T05:38:08.307 回答
0

您的问题与此行有关:

return render_to_response("home_page.html")

Django 的模板引擎需要两件事来正确渲染模板。

  1. 模板名称
  2. 上下文变量

context variable是一个键/值字典,列出了模板可用的所有变量。

render_to_response快捷方式实际上接受两个不同的上下文变量参数

你两个都缺。

如果没有这些变量,模板就没有任何可用的变量。所以你的{{ STATIC_URL }}模板变量可能是空白的。

要更正,请尝试以下操作:

from django.shortcuts import render_to_response
from django.template import RequestContext

def home_page(request):
    return render_to_response("home_page.html", {}, context_instance=RequestContext(request))
于 2013-02-26T06:46:48.477 回答