Stephen Brown
Stephen Brown

Reputation: 574

Override Django Render Methods

With the following renderer...

from django.shortcuts import render
...
return render(request, 'template.html', {'context':context,})

Is it possible to override the render classe's methods so that I can in certain circumstances interpret template tags myself for example if I find a tag consisting of a certain format such as...

{% url 'website' page.slug %}

I could point it to...

/theme/theme-1/page.html

or

/theme/theme-2/page.html

depending on extranious settings.

Upvotes: 0

Views: 1159

Answers (1)

Alasdair
Alasdair

Reputation: 309029

The render method is just a shortcut for:

template = loader.get_template(''template.html')
context = {
    ...,
}
return HttpResponse(template.render(context, request))

Therefore, it is not the correct place to try to change the behaviour of the url tag.

For the example you have given, it looks like the website should be a variable that holds theme-1 or theme-2. You can then pass the variable to the url tag instead of the string 'website'.

{% url website page.slug %}

If that is not possible, you could create a custom template tag my_url that returns the correct url depending on your settings.

Upvotes: 1

Related Questions