如何使用 Django 活塞以文本/纯文本形式返回响应?

发布于 2024-10-12 19:49:16 字数 62 浏览 2 评论 0原文

我不想序列化任何东西。我只想返回相当于 HttpResponse(blah) 的内容

I want to NOT serialize anything. I just want to return what is equivalent to HttpResponse(blah)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

递刀给你 2024-10-19 19:49:16

听起来你想要一个字符串发射器,而不是内置的 JSONEmitter、XMLEmitter 等之一。

请查看发射器的文档: https://bitbucket.org/jespern/django-piston/wiki/Documentation

以及此处现有的发射器定义:
https://bitbucket.org/jespern/django-piston/src /c4b2d21db51a/piston/emitters.py

纯文本发射器的定义可能如下所示:

from piston.emitters import Emitter
from piston.utils import Mimer    

class TextEmitter(Emitter):
    def render(self, request):
        return self.construct()
Emitter.register('text', TextEmitter)
Mimer.register('text', None, ('text/plain',))

您可以让资源在 urls.py 中使用此发射器,如下所示:

urlpatterns = patterns('',
   url(r'^blogposts
, resource_here, { 'emitter_format': 'text' }),
)

Sounds like you want a string emitter, and not one of the built-in JSONEmitter, XMLEmitter, etc.

Have a look at the docs for emitters: https://bitbucket.org/jespern/django-piston/wiki/Documentation

And the existing emitter definitions here:
https://bitbucket.org/jespern/django-piston/src/c4b2d21db51a/piston/emitters.py

A definition of a plain text emitter might look like this:

from piston.emitters import Emitter
from piston.utils import Mimer    

class TextEmitter(Emitter):
    def render(self, request):
        return self.construct()
Emitter.register('text', TextEmitter)
Mimer.register('text', None, ('text/plain',))

You'd get your resource to use this emitter in your urls.py like so:

urlpatterns = patterns('',
   url(r'^blogposts
, resource_here, { 'emitter_format': 'text' }),
)
自演自醉 2024-10-19 19:49:16

我发现的最简单的方法,来自 Django 文档

HttpResponse("Text only, please.", content_type="text/plain")

Simplest way I found, from the Django docs:

HttpResponse("Text only, please.", content_type="text/plain")
缱绻入梦 2024-10-19 19:49:16

为了补充 user85461 的答案,当您制作文本发射器时,您还需要制作一个文本 Mimer。我使用 Piston 0.2.2 编写了以下代码,

from piston.emitters import Emitter
from piston.utils import Mimer

class TextEmitter(Emitter):
    def render(self, request):
        return self.construct()
Emitter.register('text', TextEmitter, ('text/plain',))
Mimer.register(lambda x: QueryDict(x), ('text/plain',))

将此代码段添加到将在处理程序之前运行的位置。我将其放在上面的 API urls.py 中,我在其中创建了 Resources

resource_handler = Resource(handler=SomeHandler)

To add on to user85461's answer, when you make a text emitter, you'll want to also make a text Mimer. I wrote the following code with works with Piston 0.2.2

from piston.emitters import Emitter
from piston.utils import Mimer

class TextEmitter(Emitter):
    def render(self, request):
        return self.construct()
Emitter.register('text', TextEmitter, ('text/plain',))
Mimer.register(lambda x: QueryDict(x), ('text/plain',))

Add this snippet somewhere that will get run before your handlers. I put it in my API urls.py above where I created my Resources with

resource_handler = Resource(handler=SomeHandler)
我爱人 2024-10-19 19:49:16

在你看来:

class HttpResponsePlain(django.http.HttpResponse):

    def serialize(self):            return self.content
    def serialize_headers(self):    return ''

return HttpResponsePlain(content = 'That is plain text response!')

in your view:

class HttpResponsePlain(django.http.HttpResponse):

    def serialize(self):            return self.content
    def serialize_headers(self):    return ''

return HttpResponsePlain(content = 'That is plain text response!')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文