如何避免在与 django-cms 集成的 django 应用程序中硬编码模板名称?
我有一个自定义联系表单应用程序,我想将其与 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 页面获取模板,或者我是否必须对模板进行硬编码被使用?
I have a custom Contact Form App, and I want to integrate it with django-cms. From what I understand I have to register the app with django-cms, and then in my views I have to return a RequestContext instance instead of a regular context instance. So in my views.py
, I have
return render_to_response('my_template.html',
{'form': form},
context_instance=RequestContext(request))
I don't know if I'm missing something here, but my issue here is that I don't want to hardcode the template name my_template.html
. Instead, I want the template to be the same one I put when adding a new page, so my question is, is there a way to get the template from the django-cms page thats hosting the app or do I have to hardcode the template to be used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的应用挂钩是安装在
/
上的单个视图,则可以仅使用{% extends request.current_page.get_template %}
,但这不适用于您应用中的子页面。为此,您需要反转应用的根视图,使用
cms.utils.page_resolver.get_page_from_request
和use_path
参数来获取页面,然后调用get_template
在页面上并扩展它。If your apphook is a single view mounted on
/
, you can just use{% extends request.current_page.get_template %}
, however this does not work on subpages in your app.For that you would need to reverse the root view of your app, use
cms.utils.page_resolver.get_page_from_request
with theuse_path
argument to get the page, then callget_template
on the page and extend that.