2012-10-16 183 views
0

可能重复:
Django and Serving Static FilesDjango的静态文件 - CSS不工作

我有一个问题,在base.html文件加载CSS。我把所有的css文件放在/ static目录下。

urls.py我把这个代码:

if settings.DEBUG: 
    urlpatterns += patterns('', 
     (r'^static/(?P<path>.*)$', 'django.views.static.serve', 
      { 'document_root': '/home/bkcherry/botstore/botstore/static' }), 
    ) 

而在base.html文件,我把下列:

<link rel="Stylesheet" type="text/css" href="/static/css.css" /> 

当我去main.html中,CSS样式不工作。我需要配置settings.py MEDIA_ROOT,MEDIA_URLSTATIC_ROOT

+1

如果你只是将浏览器指向http://whatever.com/static/css.css,会发生什么? –

+0

这也有帮助。另外不要错过通过RequestContext。 http://stackoverflow.com/questions/12819395/how-to-make-my-css-files-to-work-in-django/12821074#12821074 – Thomas

回答

0

我认为你需要在你的路径的结尾斜线,即'/home/bkcherry/botstore/botstore/static/'

+0

我把斜杠在MEDIA_ROOT结束,现在它的工作! ! ;) – user1750551

0

如果您检查的官方文件

from django.conf import settings 

# ... the rest of your URLconf goes here ... 

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

MEDIA_ROOT应该有/上月底(https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-MEDIA_ROOT

+0

Thx所有,它现在工作! ) 我修复代码在urls.py: 如果settings.DEBUG: urlpatterns的+ =模式( '', URL(R '^静态/(P *)$',“django的?。 .views.static.serve '{ 'DOCUMENT_ROOT':settings.MEDIA_ROOT, }), ) 而且我配置settings.py: MEDIA_ROOT ='/家/ bkcherry/botstore/botstore /静态/ ' MEDIA_URL ='/ static /' STATIC_ROOT ='' STATIC_URL ='ht tp:// localhost:8000/static /' 现在GET方法找到了该目录。 – user1750551

1

你必须而不是使用MEDIA_ROOT或MEDIA_URL这是上传的媒体,而不是您的静态内容,并且您不需要设置URL模式,因为这仅适用于django 1.2或“如果您使用g下地方发展“的一些其他的服务器:https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-development

,你需要在你的静态文件: botstore/botstore /静态/ botstore/css.css

然后使用:

HOME_ROOT = os.path.dirname(__file__) 

# Absolute path to the directory static files should be collected to. 
# Don't put anything in this directory yourself; store your static files 
# in apps' "static/" subdirectories and in STATICFILES_DIRS. 
# Example: "/home/media/media.lawrence.com/static/" 

STATIC_ROOT = os.path.join(HOME_ROOT, 'staticfiles') 

# URL prefix for static files. 
# Example: "http://media.lawrence.com/static/" 
STATIC_URL = '/static/' 

# URL prefix for admin static files -- CSS, JavaScript and images. 
# Make sure to use a trailing slash. 
# Examples: "http://foo.com/static/admin/", "/static/admin/". 
ADMIN_MEDIA_PREFIX = '/static/admin/' 

# 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', 
) 

然后在你的HTML中,你可以参考你的静态文件:

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}botstore/css.css" />