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

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

问题描述

我使用Django的基于类的DetailView通用视图来查找要显示的对象。在某些情况下,我不想显示对象,而是希望退出并发出HTTP rediect。我看不到我是如何去做这件事的。这是为了当用户在我的应用程序中击中对象时,但不使用规范URL。因此,例如,在StackOverflow网址上采用以下格式:

  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。



我希望在我的DetailView中执行相同的操作。检索该对象,检查它是否是规范URL,是否重定向到该项目的get_absolute_url URL。

我无法在get_object中返回HttpResponseRedirect,因为它期望抬头看物体。我似乎无法从get_context_data中返回它,因为它只是期待上下文数据。



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



谢谢!



Ludo。

解决方案

这不适合DetailView。为此,您需要重写BaseDetailView的get方法,如下所示:

  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检查。例如:

  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)
返回self.render_to_response(上下文)

你最终会覆盖如此多的功能,它是否值得实际使用通用视图,但是你知道它是值得怀疑的。


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>

eg:

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

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.

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.

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?

Thanks!

Ludo.

解决方案

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)

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天全站免登陆