Opened 16 years ago

Closed 16 years ago

Last modified 13 years ago

#10194 closed (fixed)

allow model instance as argument to HttpResponseRedirect constructor

Reported by: Daniel Pope <dan@…> Owned by: Jacob
Component: HTTP handling Version: 1.0
Severity: Keywords: shortcut wishlist
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

Something I do fairly often is

return HttpResponseRedirect(model_inst.get_absolute_url())

It just occurred to me that

return HttpResponseRedirect(model_inst)

would be much more concise, and in the long run, more legible.

Obviously this would constitute an association between django.http and django.db.model which I believe we try to avoid in general. Perhaps a more appropriate for such a shortcut would be

from django.shortcuts import redirect
return redirect(model_inst)

Attachments (2)

redirect.diff (1.8 KB ) - added by Dan Watson 16 years ago.
Shortcut and documentation
redirect2.diff (1.8 KB ) - added by Dan Watson 16 years ago.
Forgot the versionadded

Download all attachments as: .zip

Change History (9)

comment:1 by Jacob, 16 years ago

milestone: 1.1 beta
Triage Stage: UnreviewedAccepted

I think django.shortcuts.redirect is a good idea.

comment:2 by Jacob, 16 years ago

Owner: changed from nobody to Jacob
Status: newassigned

by Dan Watson, 16 years ago

Attachment: redirect.diff added

Shortcut and documentation

comment:3 by Dan Watson, 16 years ago

Has patch: set

by Dan Watson, 16 years ago

Attachment: redirect2.diff added

Forgot the versionadded

comment:4 by Russell Keith-Magee, 16 years ago

Needs tests: set
Patch needs improvement: set

A few quick comments:

  1. The patch needs tests. This sort of thing is easily testable using the builtin test client.
  2. I'm not entirely clear why line 69 (the hasattr(obj, 'get_absolute_url') and obj.get_absolute_url() or obj bit) is required. Is there some condition I'm missing where returning just the object would make sense? The only case I can think of is so you can use redirect() as a shortcut for HttpResponseRedirect(), but frankly, I'd rather see the separation of concerns maintained - HttpResposeRedirect handling a literal URL, and redirect handling the get_absolute_url case.
  3. There are a couple of minor PEP8 issues - most notably, extra spaces around function arguments. This is easy for us to fix prior to commit, but it would be even nicer if we didn't have to fix them :-)

comment:5 by Jacob, 16 years ago

Resolution: fixed
Status: assignedclosed

(In [10108]) Fixed #10194: added django.shortcuts.redirect, a do-what-I-mean redirect shortcut. See the docs at topics/http/shortcuts for details.

comment:6 by Gary Wilson, 16 years ago

(In [10111]) Added a versionadded directive to new redirect shortcut (refs #10194).

comment:7 by Jacob, 13 years ago

milestone: 1.1 beta

Milestone 1.1 beta deleted

Note: See TracTickets for help on using tickets.
Back to Top