2009-11-13 80 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

相關問題