Code

Opened 19 months ago

Closed 15 months ago

Last modified 15 months ago

#19076 closed Bug (fixed)

TemplateView does not support setting mime type, like views.generic.simple.direct_to_template did

Reported by: gwahl@… Owned by: nobody
Component: Generic views Version: master
Severity: Normal Keywords:
Cc: bmispelon@…, mail@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I use direct_to_template to serve my robots.txt, like this:

urlpatterns = patterns('',
    url('^robots.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
...

In Django 1.5 this is no longer possible, because the function-based generic views are removed, and TemplateView does not have a mime/content type argument.

I think that a content_type argument should be added to TemplateView to add the same functionality that was in the function-based view. Patch to come.

Attachments (0)

Change History (15)

comment:1 Changed 19 months ago by gwahl@…

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 17 months ago by aaugustin

  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to Bug

comment:4 Changed 17 months ago by aaugustin

The root cause is that TemplateView uses two sets of keyword arguments to render itself:

  • **kwargs => they're merged in the context by get_context_data, and they can be passed in the call to as_view()
  • **response_kwargs => they're used in render_to_response to instantiate the response class, but currently they can't be passed without defining a subclass, hence the problem.

HttpResponseBase takes two arguments: content_type and status (plus mimetype, a deprecated alias for content_type).

TemplateView implies status = 200: otherwise one would use a different class like RedirectView.

So, the only remaining argument accepted by all HttpResponseBase subclasses is content_type, and it makes some sense to special-case it.

comment:5 Changed 17 months ago by Gavin Wahl <gwahl@…>

I added docs to the pull request.

comment:6 follow-up: Changed 17 months ago by bmispelon

Would it make sense to also add a get_content_type method (that would default to just returning self.content_type)?

A lot of the other generic views implement this pattern and it's useful in some cases.

comment:7 Changed 17 months ago by bmispelon

  • Cc bmispelon@… added

comment:8 Changed 17 months ago by dlams

  • Needs documentation unset

comment:9 Changed 16 months ago by mail@…

  • Cc mail@… added

comment:10 in reply to: ↑ 6 Changed 15 months ago by aaugustin

Replying to bmispelon:

Would it make sense to also add a get_content_type method (that would default to just returning self.content_type)?
A lot of the other generic views implement this pattern and it's useful in some cases.


The accessor pattern is useful when views may have a good reason to incrementally modify the return value -- eg. the template context.

I don't see an use case for get_content_type, and it can be added later on in a backwards-compatible way in the need arises.

comment:11 Changed 15 months ago by Aymeric Augustin <aymeric.augustin@…>

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

In 23e319d7298c8b778181e85dd9b6cbed095f8147:

Fixed #19076 -- Added content_type attribute to TemplateView.

Thanks Gavin Wahl.

comment:12 Changed 15 months ago by aaugustin

This is a "new feature" for the class-based generic views, but it's also a "fix for a regression" as 1.5 removes the function-based generic views, and the class-based generic views don't provide this feature (yet).

For this reason, I'm going to backport to 1.5, even though we've already released a RC1. A RC2 is planned. Jacob and Florian agreed on IRC.

comment:13 Changed 15 months ago by Aymeric Augustin <aymeric.augustin@…>

In d5ad9d3dfe729f8b605965e131c787fae9f84377:

[1.5.x] Fixed #19076 -- Added content_type attribute to TemplateView.

Thanks Gavin Wahl.

Backport of 23e319d.

comment:14 Changed 15 months ago by Aymeric Augustin <aymeric.augustin@…>

In c8c7cdc8f8f81b2f162db7acc8faaef0ece3b6ae:

Changed "versionadded" after the decision to backport.

Refs #19076.

comment:15 Changed 15 months ago by Aymeric Augustin <aymeric.augustin@…>

In d5ad9d3dfe729f8b605965e131c787fae9f84377:

[1.5.x] Fixed #19076 -- Added content_type attribute to TemplateView.

Thanks Gavin Wahl.

Backport of 23e319d.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.