django - 在基础项目中声明的变量不会出现在应用程序中

发布于 2024-09-03 07:33:32 字数 876 浏览 6 评论 0原文

我有一个名为 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 技术交流群。

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

发布评论

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

评论(2

梦晓ヶ微光ヅ倾城 2024-09-10 07:33:32

settings.py 中的变量不可用于模板。模板可用的内容由呈现它的视图决定。渲染模板时,您会传入一个字典,它是模板的“上下文”。上下文是变量名称及其值的字典。

要将设置中的值传递到模板,通常必须执行以下操作:

from django.conf import settings
def my_view(request):
    # view logic
    context = {
            'STATIC_URL': settings.STATIC_URL,
            # other template variables here
    }
    # render the template and produce a response

您的 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:

from django.conf import settings
def my_view(request):
    # view logic
    context = {
            'STATIC_URL': settings.STATIC_URL,
            # other template variables here
    }
    # render the template and produce a response

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.

踏雪无痕 2024-09-10 07:33:32
def courseinstance(request, courseinstance_id):
    p = get_object_or_404(CourseInstance, pk=courseinstance_id)
    return render_to_response('courses/courseinstance.html', {'courseinstance': p},
        context_instance=RequestContext(request)) #added this part to fix problem
def courseinstance(request, courseinstance_id):
    p = get_object_or_404(CourseInstance, pk=courseinstance_id)
    return render_to_response('courses/courseinstance.html', {'courseinstance': p},
        context_instance=RequestContext(request)) #added this part to fix problem
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文