如何在Django中基于类的通用视图中重定向到?next=url而不是success\u url?

2024-04-27 10:00:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道如何重定向到?next=url,而不是django中基于类的通用视图中的success\uURL

景色

class CategoryUpdateView(UpdateView):
    model = Category
    template_name = "categories/category_edit.html"
    fields = ('name', 'description', 'image')
    success_url = 'category_list'

在模板中

<a href="{% url 'category_edit' category.id %}?next={% url 'another_url' %}">Add</a>

更新类别后,我希望被重定向到链接中的nex值,而不是success\uURL。我该怎么做

谢谢


Tags: djangoname视图urlmodeledit重定向class
2条回答

从字段中删除success\u url,然后将get\u success\u url方法添加到类中,例如:

def get_success_url(self):
    URL = self.request.GET.get('next')
    return URL

这对我来说很有用,谢谢你的帮助。 正如Andrey Borzenko所说,我不得不在视图中添加这些行,并删除success_url

   def get_context_data(self, **kwargs):
        context=super(CategoryUpdateView, self).get_context_data(**kwargs)
        next_page = self.request.GET.get('next')
        if next_page:
            context["next"] = next_page
        return context

   def get_success_url(self):
        redirect_to = self.request.POST.get('next')
        return redirect_to or reverse('default_redirection')

相关问题 更多 >