#10194 closed (fixed)
allow model instance as argument to HttpResponseRedirect constructor
Reported by: | 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)
Change History (9)
comment:1 by , 16 years ago
milestone: | → 1.1 beta |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 16 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 16 years ago
Has patch: | set |
---|
comment:4 by , 16 years ago
Needs tests: | set |
---|---|
Patch needs improvement: | set |
A few quick comments:
- The patch needs tests. This sort of thing is easily testable using the builtin test client.
- 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.
- 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 , 16 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:6 by , 16 years ago
Note:
See TracTickets
for help on using tickets.
I think
django.shortcuts.redirect
is a good idea.