Dynelight
Dynelight

Reputation: 2162

Getting current active page in Django's Flatpages

I'm using flatpages to add my content.

I was wondering if there was a way to get the active page so I can style it properly on my navigational bar?

Thanks!

Upvotes: 0

Views: 277

Answers (3)

LionelW
LionelW

Reputation: 376

<a href="{{ flatpage.url }}" {% if flatpage.url == request.path %}class="active-page"{% endif %}>{{ flatpage.title }}</a>

If you have issues with your request context, you can check Burhan Khalid's answer.

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174624

The flatpages app uses RequestContext, which means that details from the request are pushed up through the middleware. You can access these request variables in templates if the request template context processor is enabled. Once you enable this, then all your templates will have access to the request, and from there you can get the current requested URL.

So,

  1. Enable the django.core.context_processors.request template context processor (its not enabled by default). You need to make sure you don't disable the default context processors, so add this to your settings.py:

    from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP
    
    TEMPLATE_CONTEXT_PROCESSORS = TCP + (
        'django.core.context_processors.request',
    )
    
  2. In your template, use {{ request.get_full_path }} or {{ request.path }} depending on what you need.

Upvotes: 2

Anup
Anup

Reputation: 753

If your using django flat pages, then you will already have the URL's defined.

EG:  url(r'^license/$', 'flatpage', {'url': '/license/'}, name='license'),

Using request.path_info should get you the current page. Is that what you where looking for is there of is it something else ?

Upvotes: 0

Related Questions