how to serve static files in django development server

雨燕双飞 提交于 2019-12-01 10:46:30

问题


this question has been answered several times and i have seen almost all related posts,but couldn't get css files load. i have this structure in my project: mysite /templates /settings.py /urls.py /myapp /static /css /test.css in settings.py i have this code:

import os
PROJECT_PATH=os.path.realpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH,'static',)
STATIC_URL = '/static/'

in urls.py i have this code:

from django.conf.urls.defaults import*
from mysite.myapp.views import test
urlpatterns = patterns('',(r'^home/$',test),)

from django.conf import settings
if settings.DEBUG:
    urlpatterns += patterns('django.contrib.staticfiles.views',
                        url(r'^static/(?P<path>.*)$','serve'),
                        )

i use this in the template

{{STATIC_URL}}css/test.css

what wrong am i doing? is there anything should i be doing?like should add anything in STATICFILES_DIRS? or in INSTALLED_APPS in settings? kindly assume that i am an absolute beginner. i really need an answer. thank you.


回答1:


you still need to pass STATIC_URL to the template in your view. The easiest way to do this is using RequestContext as long as the 'TEMPLATE_CONTEXT_PROCESSORS' include the static entry.



来源:https://stackoverflow.com/questions/9197599/how-to-serve-static-files-in-django-development-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!