Can I redirect to another url in a django TemplateView?

后端 未结 4 2140
盖世英雄少女心
盖世英雄少女心 2021-02-02 09:13

I have a url mapping that looks like this:

url(r\'^(?P[a-z][a-z])/$\', MyTemplateView.as_view()),

There are only a few values that

4条回答
  •  误落风尘
    2021-02-02 09:44

    I know this question is old, but I've just done this myself. A reason you may think you want to do it in get_context_data is due to business logic, but you should place it in dispatch.

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated():
            return redirect('home')
    
        return super(MyTemplateView, self).dispatch(request, *args, **kwargs)
    

    Keep your business logic in your dispatch and you should be golden.

提交回复
热议问题