Search code examples
pythondjangodjango-cms

How to avoid hardcoded template name in a django app, integrated with 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?


Solution

  • 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 the use_path argument to get the page, then call get_template on the page and extend that.