如何在Django开发服务器中提供静态文件 [英] how to serve static files in django development server

查看:56
本文介绍了如何在Django开发服务器中提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经被回答了好几次了,我几乎看到了所有相关的帖子,但是无法加载CSS文件.我的项目中有以下结构:我的网站/模板/settings.py/urls.py/myapp/静止的/css/test.css在settings.py中,我有以下代码:

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/'

在urls.py中,我有以下代码:

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

我在模板中使用它

{{STATIC_URL}}css/test.css

我在做什么错?我应该做些什么吗?例如应该添加任何内容STATICFILES_DIRS?或在INSTALLED_APPS的设置中?请假设我是一个绝对的初学者.我真的需要一个答案.谢谢你.

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.

推荐答案

,您仍然需要将 STATIC_URL 传递到视图中的模板.最简单的方法是使用 RequestContext ,只要'TEMPLATE_CONTEXT_PROCESSORS'包含 static 条目即可.

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.

这篇关于如何在Django开发服务器中提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆