17

I have this path in my urls.py:

archive_index_dict = {
    'queryset': News.objects.filter(show=True),
    'date_field': 'date',
    'template_object_name': 'object_list',
}

...

url(r'^$', 'django.views.generic.date_based.archive_index',
        archive_index_dict, name='news_archive_index'
    ),

Now I want to detect in template if a page is current (this is for menu styling). Neither {{ request.path }} nor {{ request.get_full_path }} work in template.

What should I use instead?

SOLUTION

To get request available in templates I had to add django.core.context_processors.request to TEMPLATE_CONTEXT_PROCESSORS. This is not set by default (since django 1.3).

Vlad T.
  • 2,568
  • 3
  • 26
  • 40
  • Do you have `django....request` context processor set up? Almost all CBV use `RequestContext` by default – ilvar Apr 16 '12 at 03:20
  • OMG, of course not! :) Thank you for the hint, I've added `'django.core.context_processors.request'` in settings and `request` is available in templates now. BTW, you could post your comment as answer and I'd vote for it. – Vlad T. Apr 16 '12 at 08:15

1 Answers1

21

Do you have 'django.core.context_processors.request' context processor set up? Almost all CBV use RequestContext by default

ilvar
  • 5,718
  • 1
  • 20
  • 17
  • Could you add a reference? Like a link to docs/source where this is mentioned? – tutuDajuju Sep 04 '18 at 17:42
  • I think the question here was actually about the template context, independent of the view used. Sorry for the confusion. – ilvar Sep 04 '18 at 20:26
  • I dug deep into the code, and it seems CBVs don't actually make the context object; they do however use the Template class, which uses make_context function that does (if it's passed a request) – tutuDajuju Sep 05 '18 at 05:02