django - 在基础项目中声明的变量不会出现在应用程序中
我有一个名为 STATIC_URL 的变量,在我的基础项目的 settings.py 中声明:
STATIC_URL = '/site_media/static/'
例如,在我的 site_base.html 中使用它,它链接到 CSS 文件,如下所示:
<link rel="stylesheet" href="{{ STATIC_URL }}css/site_tabs.css" />
我有一堆与不同应用程序相关的模板,这些应用程序扩展了site_base.html,当我在浏览器中查看它们时,CSS 已正确链接为
<link rel="stylesheet" href="/site_media/static/css/site_tabs.css" />
(这些带有默认的 pinax 发行版。)我创建了一个名为“courses”的新应用程序,它位于 ...../apps/课程文件夹。我有一个名为 courseinstance.html 的课程页面之一的视图,它像其他页面一样扩展了 site_base.html 。
然而,当这个在我的浏览器中呈现时,
<link rel="stylesheet" href="css/site_tabs.css" />
对于这个应用程序来说,STATIC_URL 似乎等于“”。我是否必须做出某种声明才能让我的应用程序采用与项目相同的变量值?我没有该应用程序的 settings.py 文件。顺便说一句,该应用程序列在我的 INSTALLED_APPS 列表中,并且运行良好,只是没有指向 CSS 文件的链接(因此该页面看起来很有趣)。
预先感谢您的帮助。
I have a variable called STATIC_URL, declared in settings.py in my base project:
STATIC_URL = '/site_media/static/'
This is used, for example, in my site_base.html, which links to CSS files as follows:
<link rel="stylesheet" href="{{ STATIC_URL }}css/site_tabs.css" />
I have a bunch of templates related to different apps which extend site_base.html, and when I look at them in my browser the CSS is linked correctly as
<link rel="stylesheet" href="/site_media/static/css/site_tabs.css" />
(These came with a default pinax distribution.) I created a new app called 'courses' which lives in the ...../apps/courses folder. I have a view for one of the pages in courses called courseinstance.html which extends site_base.html just like the other ones.
However, when this one renders in my browser it comes out as
<link rel="stylesheet" href="css/site_tabs.css" />
as if STATIC_URL were equal to "" for this app. Do I have to make some sort of declaration to get my app to take on the same variable values as the project? I don't have a settings.py file for the app. by the way, the app is listed in my list of INSTALLED_APPS and it gets served up fine, just without the link to the CSS file (so the page looks funny).
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
settings.py 中的变量不可用于模板。模板可用的内容由呈现它的视图决定。渲染模板时,您会传入一个字典,它是模板的“上下文”。上下文是变量名称及其值的字典。
要将设置中的值传递到模板,通常必须执行以下操作:
您的 STATIC_URL 设置似乎与 MEDIA_URL 设置。
通过默认上下文处理器。您可以通过 编写来执行类似的操作您自己的上下文处理器。您可以查看默认上下文处理器是如何实现的< /a> 在 django 源代码中得到一个想法。
Variables in settings.py are not available to the templates. What is available to a template is determined by the view that renders it. When the template is rendered you pass in a dictionary which is the "context" for the template. The context is a dictionary of names of variables and their values.
To pass a value from the settings onto the template, you usually have to something like this:
Your STATIC_URL settings seems to be very similar to the MEDIA_URL setting.
MEDIA_URL is made available to all templates via a default context processor. You can do something similar by writing your own context processor. You can take a look at how the default context processors are implemented in the django source to get an idea.