如何在 django-piston 处理程序中为 JSON 消息设置 HTTP 标头?
在活塞处理程序中,我需要返回 django.db.models.query.QuerySet 作为正确的 JSON 消息(反映底层模型和查询),同时还添加我自己的 HttpResponse 标头。到目前为止,我可以执行其中之一,但不能同时执行两者(并获得看起来正确的 JSON 响应)。
下面生成一个正确的 JSON 格式的响应,但没有添加 HttpResponse 标头(未显示):
class PollHandlerSearch(BaseHandler):
allowed_methods = ('POST')
model = Poll
fields = ('id', 'question', 'pub_date')
KEYS = ( 'question', 'start-date', 'end-date' )
def create(self, request):
post = Poll.objects.all()
for skey in self.KEYS:
if len(post) and request.POST.has_key(skey) and len(request.POST[skey]):
if skey == self.KEYS[0]:
post = post.filter(question__icontains=request.POST[skey])
elif skey == self.KEYS[1]:
post = post.filter(pub_date__gte=request.POST[skey])
elif skey == self.KEYS[2]:
post = post.filter(pub_date__lte=request.POST[skey])
return post
生成正确格式的 JSON 消息:
[
{
"pub_date": "2010-08-23 22:15:07",
"question": "What's up?",
"id": 1
}
]
下面使用添加的标头实现 HttpResponse 并生成一个看起来像 JSON 的响应,但不是实际的响应预期或想要的,加上不反映 django 的“DateTimeAwareJSONEncoder”所做的任何事情(由活塞的 JSONEmitter 使用)。
class PollHandlerSearch(BaseHandler):
allowed_methods = ('POST')
model = Poll
fields = ('id', 'question', 'pub_date')
KEYS = ( 'question', 'start-date', 'end-date' )
def create(self, request):
resp = HttpResponse()
post = Poll.objects.all()
for skey in self.KEYS:
if len(post) and request.POST.has_key(skey) and len(request.POST[skey]):
if skey == self.KEYS[0]:
post = post.filter(question__icontains=request.POST[skey])
elif skey == self.KEYS[1]:
post = post.filter(pub_date__gte=request.POST[skey])
elif skey == self.KEYS[2]:
post = post.filter(pub_date__lte=request.POST[skey])
json_serializer = serializers.get_serializer("json")()
json_serializer.serialize(post, ensure_ascii=False, indent=4, stream=resp)
resp['MYHEADER'] = 'abc123'
return resp
导致格式不正确的 JSONish 消息:
[
{
"pk": 1,
"model": "polls.poll",
"fields": {
"pub_date": "2010-08-23 22:15:07",
"question": "What's up?"
}
}
]
这无疑是发生的,因为我正在执行自己的 JSON 序列化,绕过了活塞的 JSONEmitter,从而绕过了它正确呈现“post”的任何操作。
我一直在研究piston的emitters.py,但基本上无法理解它(我在OOP/Python/django/piston方面还很新)。如何让活塞传递格式正确的 JSON 消息,其中包含 HTTP 标头并补充了我提供的标头?
In a piston handler, I need to return a django.db.models.query.QuerySet as a proper JSON message (reflective of the underly model and query), while also adding an HttpResponse header of my own. So far, I can do one or the other, but not both (and get a proper looking JSON response).
The below generates a proper JSON formatted response, but with no added HttpResponse header (not shown):
class PollHandlerSearch(BaseHandler):
allowed_methods = ('POST')
model = Poll
fields = ('id', 'question', 'pub_date')
KEYS = ( 'question', 'start-date', 'end-date' )
def create(self, request):
post = Poll.objects.all()
for skey in self.KEYS:
if len(post) and request.POST.has_key(skey) and len(request.POST[skey]):
if skey == self.KEYS[0]:
post = post.filter(question__icontains=request.POST[skey])
elif skey == self.KEYS[1]:
post = post.filter(pub_date__gte=request.POST[skey])
elif skey == self.KEYS[2]:
post = post.filter(pub_date__lte=request.POST[skey])
return post
Resulting correctly formatted JSON message:
[
{
"pub_date": "2010-08-23 22:15:07",
"question": "What's up?",
"id": 1
}
]
The below implements an HttpResponse with the added header and generates a JSONish looking response, but one that is not what is being expected or wanted, plus not reflecting whatever django's 'DateTimeAwareJSONEncoder' does (used by piston's JSONEmitter).
class PollHandlerSearch(BaseHandler):
allowed_methods = ('POST')
model = Poll
fields = ('id', 'question', 'pub_date')
KEYS = ( 'question', 'start-date', 'end-date' )
def create(self, request):
resp = HttpResponse()
post = Poll.objects.all()
for skey in self.KEYS:
if len(post) and request.POST.has_key(skey) and len(request.POST[skey]):
if skey == self.KEYS[0]:
post = post.filter(question__icontains=request.POST[skey])
elif skey == self.KEYS[1]:
post = post.filter(pub_date__gte=request.POST[skey])
elif skey == self.KEYS[2]:
post = post.filter(pub_date__lte=request.POST[skey])
json_serializer = serializers.get_serializer("json")()
json_serializer.serialize(post, ensure_ascii=False, indent=4, stream=resp)
resp['MYHEADER'] = 'abc123'
return resp
Resulting incorrectly formatted JSONish message:
[
{
"pk": 1,
"model": "polls.poll",
"fields": {
"pub_date": "2010-08-23 22:15:07",
"question": "What's up?"
}
}
]
This is no doubt happening since I am doing my own JSON serialization, bypassing piston's JSONEmitter and thus whatever it does to properly render 'post'.
I have been pouring over piston's emitters.py, and largely can't follow it (I am pretty new at OOP / Python / django / piston). How can I get piston to deliver a properly formatted JSON message with an HTTP header supplemented with headers I provide?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以子类化
piston.resource.Resource
,并在__call__
方法中添加您想要的任何标头。frompiston.resource import Resource
然后,在你的 urls.py 中:
You could subclass
piston.resource.Resource
, and add whatever header you want in the__call__
method.from piston.resource import Resource
Then, in your urls.py: