2009/5/20 Filip Gruszczyński <grusz...@gmail.com>

>
> Yeah, I did a quick check in the interpreter and this is indeed very cool.
>
> There is another thing though that worries me (but just a little).
> Often, when I write views, it's nice to be able just to return 404 or
> 302 at any moment and not proceed with a current function. For 404 it
> can be always done by just raise Http404. Inside views functions 302
> can also be easily done by return HttpResponseRedirect('location').
> But when it is done inside a method in a class, it can't be done so
> easily. For example:
>
> class SomeResponse(HttpResponse):
>
>  def __init__(self, *args, **kwargs):
>    self.some_method(*args, **kwargs)
>
>  def some_method(self, *args, **kwargs):
>    # need redirect here - this won't work
>    return HttpResponseRedirect(..
>
> Is there any elegant solution, which would allow to break, just when
> it's needed?
>
> 2009/5/20 Alex Gaynor <alex.gay...@gmail.com>:
> >
> >
> > 2009/5/20 Filip Gruszczyński <grusz...@gmail.com>
> >>
> >> > Take a look at this:
> >> > http://code.djangoproject.com/ticket/6735#comment:37
> >> > snippet which actually does let you store state on the object.
> >>
> >> The suggested solution is very cool, but I wonder, how to change
> >> status code of such responses. status_code is a class attribute, so if
> >> while serving a request I decide I need to change it's status, I will
> >> have to change this value.
> >>
> >> But wait - if at some point I decide to for example turn the response
> >> into redirect, I can just call:
> >> self.status_code = 302
> >> and I won't actually change the class attribute, but only instance
> >> attribute, right? So I can just subclass HttpResponse, add all stuff
> >> (like having function forbid or redirect) and it should work?
> >>
> >> --
> >> Filip Gruszczyński
> >>
> >>
> >
> > The way python attributes work is that if something is a class attribute,
> > but you assign to it on an instance the instance gets it's own version of
> > this attribute which "shadows" the classes one.  So if Django does
> something
> > like: status = response.status_code it will just see the objects version,
> > not it's classes.
> >
> > Alex
> >
> > --
> > "I disapprove of what you say, but I will defend to the death your right
> to
> > say it." --Voltaire
> > "The people's good is the highest law."--Cicero
> >
> > >
> >
>
>
>
> --
> Filip Gruszczyński
>
> >
>
For a 404 you should still be able to just raise the exception, for other
response you could create your own Exception like ResponseIsReady and just
raise it and have __init__ return as soon as it's done.

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to