Django

Code

Ticket #10194 (closed: fixed)

Opened 1 year ago

Last modified 1 year ago

allow model instance as argument to HttpResponseRedirect constructor

Reported by: Daniel Pope <dan@mauveinternet.co.uk> Assigned to: jacob
Milestone: 1.1 beta Component: HTTP handling
Version: 1.0 Keywords: shortcut wishlist
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 1

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

redirect.diff (1.8 kB) - added by dcwatson on 03/10/09 11:13:39.
Shortcut and documentation
redirect2.diff (1.8 kB) - added by dcwatson on 03/10/09 11:23:46.
Forgot the versionadded

Change History

02/28/09 11:52:11 changed by jacob

  • needs_better_patch changed.
  • needs_docs changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • milestone set to 1.1 beta.

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

02/28/09 11:52:16 changed by jacob

  • owner changed from nobody to jacob.
  • status changed from new to assigned.

03/10/09 11:13:39 changed by dcwatson

  • attachment redirect.diff added.

Shortcut and documentation

03/10/09 11:14:13 changed by dcwatson

  • has_patch set to 1.

03/10/09 11:23:46 changed by dcwatson

  • attachment redirect2.diff added.

Forgot the versionadded

03/12/09 07:02:32 changed by russellm

  • needs_better_patch set to 1.
  • needs_tests set to 1.

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 :-)

03/21/09 08:09:14 changed by jacob

  • status changed from assigned to closed.
  • resolution set to fixed.

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

03/21/09 10:26:57 changed by gwilson

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


Add/Change #10194 (allow model instance as argument to HttpResponseRedirect constructor)




Change Properties
Action