How to serve stylesheet in Django
i want to use css in Django templates..If i give CSS with in the templates it gets working. But i want to use in static serve manner.
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开发者_StackOverflow中文版.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}),
)
I got 'unexpected end of pattern' error for above line
<link rel="stylesheet" type="text/css" href="/static/css/style.css"/>
I believe the 'P' to name the pattern needs to be capitalized. r'^static/(?P<path>.*)$'
All of the examples and doc show it capitalized. Python Regex Doc
T. Stone has hit the nail on the head with his answer. Here's what I use, for an example:
if settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{ 'document_root': os.path.join(os.path.dirname(__file__), "static")}),
)
精彩评论