在Django 1.3中如何根据基于类的视图进行重定向? [英] How to redirect on conditions with class based views in Django 1.3?

查看:154
本文介绍了在Django 1.3中如何根据基于类的视图进行重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ListView,根据标签列出视频。过滤发生在get_queryset()中。如果标签不包含任何视频,我想将用户重定向到另一个页面。

I am using a ListView that list videos according to tags. The filtering happens in get_queryset(). I'd like to redirect the user to another page if the tag doesn't contains any video.

使用功能很容易。查询,查询查询,重定向。有一个课,我没有这样做:

With a function, it would be easy. Query, check the queryset, redirect. With a class, I fail doing so:

class VideosView(generic.ListView):

    def get_queryset(self):
        """
            This work.
        """

        tag = self.kwargs.get('tag', None)

        self.videos = Video.on_site.all()

        if tag:
            self.videos = Video.tagged.with_all(tag, self.videos)

        return self.videos

    def get(self, request, *args, **kwargs):
        """
        This doesn't work because self.videos doesn't exist yet.
        """
        if not self.videos:
            return redirect('other_page')

        return super(Videos, self).get(request, *args, **kwargs)


推荐答案

找到它:

def render_to_response(self, context):

    if not self.videos:
        return redirect('other_page')

    return super(VideosView, self).render_to_response(context)

这是为所有HTTP方法

This is called for all HTTP methods

这篇关于在Django 1.3中如何根据基于类的视图进行重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆