Настройка Mimetype при использовании TemplateView в Django

Кто-нибудь знает, как мне установить желаемый тип mimetype при использовании TemplateView, например:

urlpatterns = patterns('',
    url(r'^test\.txt$', TemplateView.as_view(template_name='staticpages/test.html')),

В этом случае я хочу установить mimtype как «text / plain»

30
задан Henrik Aasted Sørensen 10 September 2012 в 10:46
поделиться

2 ответа

Простой пример того, как изменить тип содержимого TemplateView:

#views.py
from django.views.generic import TemplateView

class HomeView(TemplateView):
    template_name = "home/index.html"
    content_type = 'text/html'

# urls.py
url(r'^home/, HomeView.as_view(), name='home_page'),
0
ответ дан 27 November 2019 в 23:33
поделиться

Я знаю, что это решено для 1.5, но приложение, в котором я работаю, - 1.4.

У меня была проблема с двумя шаблонами URL-адресов подряд, используя ответ sacabuche:

url(r'^playlist1\.m3u 

Я обнаружил, что playlist1 вернет правильный шаблон, но с типом контента playlist2! Playlist2 был в порядке. Добавление шаблона 3-го URL с типом содержимого 'foo' приведет к тому, что все представления списка воспроизведения будут возвращены с типом содержимого 'foo'.

В итоге я использовал метод рендеринга с хорошими результатами:

URL:

url(r'^playlist1\.m3u 

просмотров:

from django.shortcuts import render

def content_type_to_template(request, template_name='', content_type='text/plain'):
    return render(request, template_name, content_type=content_type)
, 'content_type_to_template', {'template_name': 'playlist1.m3u', 'content_type': 'audio/x-mpegurl'}), url(r'^playlist2\.pls

просмотров:

from django.shortcuts import render

def content_type_to_template(request, template_name='', content_type='text/plain'):
    return render(request, template_name, content_type=content_type)
, 'content_type_to_template', {'template_name': 'playlist2.pls', 'content_type':'audio/x-scpls'})

просмотров:

from django.shortcuts import render

def content_type_to_template(request, template_name='', content_type='text/plain'):
    return render(request, template_name, content_type=content_type)
, ContentTypeTemplateView.as_view(template_name='playlist1.m3u', content_type='audio/x-mpegurl')), url(r'^playlist2\.pls

Я обнаружил, что playlist1 вернет правильный шаблон, но с типом контента playlist2! Playlist2 был в порядке. Добавление шаблона 3-го URL с типом содержимого 'foo' приведет к тому, что все представления списка воспроизведения будут возвращены с типом содержимого 'foo'.

В итоге я использовал метод рендеринга с хорошими результатами:

URL:

url(r'^playlist1\.m3u 

просмотров:

from django.shortcuts import render

def content_type_to_template(request, template_name='', content_type='text/plain'):
    return render(request, template_name, content_type=content_type)
, 'content_type_to_template', {'template_name': 'playlist1.m3u', 'content_type': 'audio/x-mpegurl'}), url(r'^playlist2\.pls

просмотров:

from django.shortcuts import render

def content_type_to_template(request, template_name='', content_type='text/plain'):
    return render(request, template_name, content_type=content_type)
, 'content_type_to_template', {'template_name': 'playlist2.pls', 'content_type':'audio/x-scpls'})

просмотров:

from django.shortcuts import render

def content_type_to_template(request, template_name='', content_type='text/plain'):
    return render(request, template_name, content_type=content_type)
, ContentTypeTemplateView.as_view(template_name='playlist2.pls', content_type='audio/x-scpls'))

Я обнаружил, что playlist1 вернет правильный шаблон, но с типом контента playlist2! Playlist2 был в порядке. Добавление шаблона 3-го URL с типом содержимого 'foo' приведет к тому, что все представления списка воспроизведения будут возвращены с типом содержимого 'foo'.

В итоге я использовал метод рендеринга с хорошими результатами:

URL:

url(r'^playlist1\.m3u 

просмотров:

from django.shortcuts import render

def content_type_to_template(request, template_name='', content_type='text/plain'):
    return render(request, template_name, content_type=content_type)
, 'content_type_to_template', {'template_name': 'playlist1.m3u', 'content_type': 'audio/x-mpegurl'}), url(r'^playlist2\.pls

просмотров:

from django.shortcuts import render

def content_type_to_template(request, template_name='', content_type='text/plain'):
    return render(request, template_name, content_type=content_type)
, 'content_type_to_template', {'template_name': 'playlist2.pls', 'content_type':'audio/x-scpls'})

просмотров:

from django.shortcuts import render

def content_type_to_template(request, template_name='', content_type='text/plain'):
    return render(request, template_name, content_type=content_type)
1
ответ дан 27 November 2019 в 23:33
поделиться
Другие вопросы по тегам:

Похожие вопросы: