Piumosso
Piumosso

Reputation: 21

How can i make a redirect in django template tag?

I have one custom template tag function.

I try raise custom exception and catch it in custom middleware, but template engine process exception and raise TemplateSyntaxError.

Upvotes: 1

Views: 1641

Answers (1)

Steve Jalim
Steve Jalim

Reputation: 12195

That's because the template engine runs before your response/post-rendering middleware, and is not tolerant of exceptions in template tags -- and nor should it be. How would it know there's middleware waiting in the response cycle that will catch it?

The simplest way forward is probably to pre-check in the view for the condition that would cause the template tag to explode (not seeing any code, I don't know whether this is really possible) and issue a suitable HttpResponse/exception handling change of flow there.

Upvotes: 1

Related Questions