Django

Code

Ticket #3526 (closed: fixed)

Opened 2 years ago

Last modified 1 year ago

HttpResponse object should have 'content_type' argument (in additon to / instead of 'mimetype')

Reported by: Simon Willison Assigned to: adrian
Milestone: Component: Core framework
Version: SVN Keywords: httpresponse
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 1
Needs tests: 0 Patch needs improvement: 0

Description

I can never remember the 'mimetype' argument to HttpResponse; I always have to look it up. The corresponding HTTP header is 'Content-Type'; I think that content_type would be a more memorable and more accurate argument.

For example:

return HttpResponse(simplejson.dumps({
    'airports': airports
}), mimetype='application/json; charset=utf-8')

Should be:

return HttpResponse(simplejson.dumps({
    'airports': airports
}), content_type='application/json; charset=utf-8')

The charset=utf-8 bit further demonstrates that mimetype should be renamed - charset=utf-8 is a parameter, not part of the actual mime media type.

I suggest adding content_type as an alternative to mimetype; I see no particular need to deprecate mimetype and break existing code, although it may be something that should be removed in the future.

Attachments

content_type.diff (1.3 kB) - added by Simon Willison on 02/19/07 07:15:57.
Patch to add content_type argument without breaking mimetype

Change History

02/19/07 07:15:57 changed by Simon Willison

  • attachment content_type.diff added.

Patch to add content_type argument without breaking mimetype

02/19/07 07:16:34 changed by Simon Willison

  • needs_better_patch changed.
  • has_patch set to 1.
  • needs_tests changed.
  • needs_docs set to 1.

02/19/07 07:17:01 changed by Simon Willison

  • needs_tests set to 1.

02/19/07 13:41:04 changed by SmileyChris

  • stage changed from Unreviewed to Design decision needed.

03/09/07 06:44:14 changed by mtredinnick

  • stage changed from Design decision needed to Accepted.

This looks reasonable. However, isn't the patch broken? If you pass in mimetype, but not content_type, then lines 162 and 163 will stomp all over line 161 in the new version. Looks like 163 should be "elif". Otherwise, looks like the right idea.

03/09/07 07:12:45 changed by mtredinnick

OK, I'm an idiot. The patch is not broken. Nothing to see here.

07/19/07 07:58:06 changed by Simon G. <dev@simon.net.nz>

  • stage changed from Accepted to Ready for checkin.

07/19/07 07:58:48 changed by Simon G. <dev@simon.net.nz>

  • needs_tests deleted.

Want to write a few lines for the docs too?

08/11/07 04:37:08 changed by mtredinnick

The only change I made in the version I'm about to commit is to move the content_type parameter to the end of the constructor list to preserve backwards compatibility for people using positional arguments.

08/11/07 04:37:43 changed by mtredinnick

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

(In [5844]) Fixed #3526 -- Added content_type as an alias for mimetype to the HttpResponse constructor. It's a slightly more accurate name. Based on a patch from Simon Willison. Fully backwards compatible.


Add/Change #3526 (HttpResponse object should have 'content_type' argument (in additon to / instead of 'mimetype'))




Change Properties
Action