【问题标题】:How to avoid hardcoded template name in a django app, integrated with django-cms?如何避免在与 django-cms 集成的 django 应用程序中硬编码模板名称?
【发布时间】:2011-07-27 08:25:42
【问题描述】:

我有一个自定义的联系表单应用程序,我想将它与 django-cms 集成。据我了解,我必须使用 django-cms 注册应用程序,然后在我看来,我必须返回一个 RequestContext 实例而不是常规上下文实例。所以在我的views.py,我有

return render_to_response('my_template.html',
                          {'form': form},
                          context_instance=RequestContext(request))

我不知道我是否在这里遗漏了什么,但我的问题是我不想硬编码模板名称my_template.html。相反,我希望模板与添加新页面时放置的模板相同,所以我的问题是,有没有办法从托管应用程序的 django-cms 页面获取模板,或者我是否必须对模板进行硬编码要使用吗?

【问题讨论】:

    标签: python django django-cms


    【解决方案1】:

    如果您的 apphook 是安装在 / 上的单个视图,您可以只使用 {% extends request.current_page.get_template %},但这不适用于您应用中的子页面。

    为此,您需要反转应用程序的根视图,使用 cms.utils.page_resolver.get_page_from_requestuse_path 参数来获取页面,然后在页面上调用 get_template 并扩展它。

    【讨论】:

      猜你喜欢
      • 2011-07-07
      • 2010-10-22
      • 1970-01-01
      • 2015-05-31
      • 2018-10-29
      • 2014-09-19
      • 2019-01-25
      • 1970-01-01
      • 2020-11-19
      相关资源
      最近更新 更多