2009-11-13 83 views
1

我想在Django模板中使用css ..如果我在模板中给CSS赋值。 但我想用静态服务方式。如何在Django中提供样式表

settings.py

 DEBUG =True 
     MEDIA_ROOT = 'C:/WorkBase/Python/first/static/' 
     MEDIA_URL = '/static/' 
     ADMIN_MEDIA_PREFIX = '/media/' 

TEMPLATE_DIRS =(

 'C:/WorkBase/Python/first/templates', 
     ) 

TEMPLATE_LOADERS =(

 'django.template.loaders.filesystem.load_template_source', 
     'django.template.loaders.app_directories.load_template_source', 
     'django.template.loaders.eggs.load_template_source', 

urls.py

 from django.conf import settings 

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

我得到了上面的一行

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

回答

4

“模式意外结束”错误我相信“P”来命名的模式需要进行资本化。 r'^static/(?P<path>.*)$'

所有的例子和文档显示它大写。 Python Regex Doc

+0

谢谢......他的工作... – Beginner 2009-11-16 08:12:27

1

T. Stone已经在他的答案中击中了头部。以下是我使用的示例:

if settings.DEBUG: 
     urlpatterns += patterns('', 
       (r'^static/(?P<path>.*)$', 'django.views.static.serve', 
         { 'document_root': os.path.join(os.path.dirname(__file__), "static")}), 
     ) 
+0

+1为相对路径 – 2009-11-13 08:33:20

相关问题