Can I redirect to another url in a django TemplateView?

后端 未结 4 2074
盖世英雄少女心
盖世英雄少女心 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:46

    This worked for me using an UpdateView class in Django 3.1:

    def get(self, request, *args, **kwargs):
       
        if 1 == 1:
            return HttpResponseRedirect(reverse_lazy("view_name_here"))
        else:
            return super().get(request, *args, **kwargs)
    

    To determine this, I analyzed its base class (Cmd+Click in PyCharm), where I found the base method:

    def get(self, request, *args, **kwargs):
        self.object = self.get_object()
        return super().get(request, *args, **kwargs)
    

    You can find this and other methods in the Django source code: django/views/generic/edit.py

提交回复
热议问题