从 Django 中的通用视图 DetailView 重定向 [英] Redirect from Generic View DetailView in Django

查看:19
本文介绍了从 Django 中的通用视图 DetailView 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用基于 Django 类的 DetailView 通用视图来查找要显示的对象.在某些情况下,我不想显示对象,而是希望退出并发出 HTTP 重定向.我看不出我是怎么做这件事的.这是当用户点击我的应用程序中的对象但不使用规范 URL 时.因此,例如,在 StackOverflow 上,URL 采用以下形式:

I'm using Django's class based DetailView generic view to look up an object for display. Under certain circumstances, rather than displaying the object, I wish to back out and issue a HTTP rediect instead. I can't see how I go about doing this. It's for when a user hits an object in my app, but without using the canonical URL. So, for example, on StackOverflow URLs take the form:

http://stackoverflow.com/<content_type>/<pk>/<seo_friendly_slug>

例如:

http://stackoverflow.com/questions/5661806/django-debug-toolbar-with-django-cms-and-django-1-3

您实际上可以在 seo_friendly_slug 部分输入任何内容,它会将您重定向到通过 PK 查找的对象的正确规范 URL.

You can actually type anything as the seo_friendly_slug part and it will redirect you to the correct canonical URL for the object looked up via the PK.

我希望在我的 DetailView 中做同样的事情.检索对象,检查它是否是规范 URL,如果不是,则重定向到该项目的 get_absolute_url URL.

I wish to do the same in my DetailView. Retrieve the object, check that it's the canonical URL, and if not redirect to the item's get_absolute_url URL.

我无法在 get_object 中返回 HttpResponseRedirect,因为它期待查找的对象.我似乎无法从 get_context_data 返回它,因为它只是期待上下文数据.

I can't return an HttpResponseRedirect in get_object, as it's expecting the looked up object. I can't seem to return it from get_context_data, as it's just expecting context data.

也许我只需要写一个手动视图,但我想知道是否有人知道这是否可能?

Maybe I just need to write a manual view, but I wondered if anyone knew if it was possible?

谢谢!

卢多.

推荐答案

这不适合 DetailView.为此,您需要覆盖 BaseDetailView 的 get 方法,如下所示:

This isn't a natural fit for DetailView. To do this you need to override the get method of BaseDetailView, which looks like:

class BaseDetailView(SingleObjectMixin, View):
    def get(self, request, **kwargs):
        self.object = self.get_object()
        context = self.get_context_data(object=self.object)
        return self.render_to_response(context)

因此,在您的类中,您需要提供一个新的 get 方法,该方法在获取对象和设置上下文之间进行 URL 检查.类似的东西:

So in your class you'd need to provide a new get method which did the URL check between fetching the object and setting up the context. Something like:

def get(self, request, **kwargs):
    self.object = self.get_object()
    if self.request.path != self.object.get_absolute_url():
        return HttpResponseRedirect(self.object.get_absolute_url())
    else:
        context = self.get_context_data(object=self.object)
        return self.render_to_response(context)

当您最终覆盖了如此多的功能时,实际上是否值得为此使用通用视图变得值得怀疑,但您知道.

As you end up overriding so much of the functionality it becomes questionable whether it's worth actually using a generic view for this, but youknow.

这篇关于从 Django 中的通用视图 DetailView 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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