#19076 closed Bug (fixed)
TemplateView does not support setting mime type, like views.generic.simple.direct_to_template did
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Generic views | Version: | dev |
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.
Change History (15)
comment:1 by , 12 years ago
Has patch: | set |
---|
comment:2 by , 12 years ago
Triage Stage: | Unreviewed → Accepted |
---|---|
Type: | Uncategorized → Bug |
comment:3 by , 12 years ago
Needs documentation: | set |
---|
comment:4 by , 12 years ago
The root cause is that TemplateView
uses two sets of keyword arguments to render itself:
**kwargs
=> they're merged in the context byget_context_data
, and they can be passed in the call toas_view()
**response_kwargs
=> they're used inrender_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.
follow-up: 10 comment:6 by , 12 years ago
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 by , 12 years ago
Cc: | added |
---|
comment:8 by , 12 years ago
Needs documentation: | unset |
---|
comment:9 by , 12 years ago
Cc: | added |
---|
comment:10 by , 12 years ago
Replying to bmispelon:
Would it make sense to also add a
get_content_type
method (that would default to just returningself.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 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:12 by , 12 years ago
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.
Patch: https://github.com/django/django/pull/426