Opened 6 years ago

Closed 6 years ago

Last modified 4 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: UI/UX:

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 dcwatson 6 years ago.
Shortcut and documentation
redirect2.diff (1.8 KB) - added by dcwatson 6 years ago.
Forgot the versionadded

Download all attachments as: .zip

Change History (9)

comment:1 Changed 6 years ago by jacob

  • milestone set to 1.1 beta
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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

comment:2 Changed 6 years ago by jacob

  • Owner changed from nobody to jacob
  • Status changed from new to assigned

Changed 6 years ago by dcwatson

Shortcut and documentation

comment:3 Changed 6 years ago by dcwatson

  • Has patch set

Changed 6 years ago by dcwatson

Forgot the versionadded

comment:4 Changed 6 years ago by russellm

  • 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 Changed 6 years ago by jacob

  • Resolution set to fixed
  • Status changed from assigned to closed

(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 Changed 6 years ago by gwilson

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

comment:7 Changed 4 years ago by jacob

  • milestone 1.1 beta deleted

Milestone 1.1 beta deleted

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