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).
A view is a callable which takes a request and returns a response. This can be more than just a function, and Django provides an example of some classes which can be used as views. These allow you to structure your views and reuse code by harnessing inheritance and mixins.
The intention of Generic Views is to reduce boilerplate code when you repeatedly use similar code in several views. You should really use it just for that. Basically, just because django allows something you are doing generically you shouldn't do it, particularly not when your code becomes not to your like.
Generic views come in handy when you want to display lists, or detail views, for instance. They will manage querying the database, and messaging the user, among other. So generic views are high level functions to help you creating a response from a view.
Do you have 'django.core.context_processors.request'
context processor set up? Almost all CBV use RequestContext
by default
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With