我不想序列化任何东西。我只想返回相当于HttpResponse(blah)
3 回答
听起来你想要一个字符串发射器,而不是内置的 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' }),
)
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)
在你看来:
class HttpResponsePlain(django.http.HttpResponse):
def serialize(self): return self.content
def serialize_headers(self): return ''
return HttpResponsePlain(content = 'That is plain text response!')