如何在django的templateview中重定向url

dja*_*ina 1 django redirect

我有一个templateview,代码在这里是怎么做的

class MyTemplateView(TemplateView):
    def get_context_data(self, **kwargs):
        context = super(UBaseTemplateView, self).get_context_data(**kwargs)
        # i want to redirect another url in here
        # how to do it

        return context
Run Code Online (Sandbox Code Playgroud)

Gam*_*iac 5

好吧,你会这样的:

class MyTemplateView(TemplateView):
    def get(self, request, *args, **kwargs):
        return HttpResponseRedirect('/<your path here>/')
Run Code Online (Sandbox Code Playgroud)

您可以详细了解一下这里,并更详细地在这里.

如果你想传递帖子数据,那么你所要做的就是:

class MyTemplateView(TemplateView):
    def get_context_data(self, **kwargs):
        return HttpResponseRedirect(reverse('/<your url here>/', [params]))
Run Code Online (Sandbox Code Playgroud)

您也可以使用该post功能执行此操作.

class MyTemplateView(TemplateView):
    def post(self, request, *args, **kwargs):
        # do what you want with post data
        # you can get access via request.POST.get()
        return HttpResponseRedirect('/<your url>/')
        # Or use the above example's return statement, if you want to pass along parameters
Run Code Online (Sandbox Code Playgroud)