0

I am using Django flatpages and would like to implement some logic in a template based on a user session variable.

eg.

{% if session.my_var %}
    YES
{% else %}
    NO
{% endif %}

Problem is that session object is not defined in flatpage context.

Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
Alan Harper
  • 793
  • 1
  • 10
  • 23

1 Answers1

1

Create a TEMPLATE_CONTEXT_PROCESSOR which is then used by the RequestContext (see docs).

def session(request):
    return { 'session': request.session }
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • Great, that exactly what I am looking for (as I don't see session if flatpage module sources) – Alan Harper Feb 04 '13 at 21:31
  • 1
    @AlanHarper: I just updated my answer as the template tag was a more elaborate solution. Django's flatpages uses a RequestContext so you can create a template context processor to add variables to the context. – Simeon Visser Feb 04 '13 at 21:32
  • tnx, btw - may be you remember if it's possible to include one flatpage into another? (like a placeholder common for several pages)? tnx – Alan Harper Feb 04 '13 at 21:38
  • @I don't think that's possible as one page could then consist of multiple `FlatPage` model objects. But you can still include the templates as normal Django templates using `include`. – Simeon Visser Feb 04 '13 at 21:49