如何在 django 开发服务器中提供静态文件

发布于 2025-01-03 21:35:49 字数 954 浏览 1 评论 0原文

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

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

在 urls.py 中我有这个代码:

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

我做错了什么? 我应该做什么?比如应该添加任何东西 静态文件目录?或者在设置中的 INSTALLED_APPS 中? 请假设我是一个绝对的初学者。 我真的需要一个答案。 谢谢。

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/

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.

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

i use this in the template


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.

,'serve'), )

i use this in the template

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的黑色迷你裙 2025-01-10 21:35:49

您仍然需要将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文