urls.py
url(r'^(?i)(?P<slug>[a-zA-Z0-9_]+)$', views_search.index, name='articles'),
context_processor.py
def get_username(request, **kwargs):
print kwargs
slug = kwargs.get('slug')
return {
'slug': slug
}
But when i am running it, its printing empty dict and nothing is returned to the template. I have added this in template context processors in setting. How can I access kwargs here ?
If an url is resolved, the ResolverMatch
object is set as an attribute on the request:
def get_username(request):
if hasattr(request, 'resolver_match'):
slug = request.resolver_match.kwargs.get('slug')
return {'slug': slug}
return {}
Actually, for class based views, the view
is already available in the context, so you can directly access kwargs
in the template. In the template, just do the following:
{{ view.kwargs.slug }}
Also, see this SO answer
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