如何使用 django-piston 读取 lxml.objectify.ObjectifiedElement 对象?
我正在尝试创建一个 django-piston API,它读取另一个 API 并以不同的格式呈现它(读取仅 XML API 并以 JSON 呈现它)。但我遇到了麻烦,因为我这样做的方式只是一个空字符串。
这是我的处理程序:
class MyHandler(BaseHandler):
methods_allowed = ('GET')
def read(self, request, my_argument):
my_node = get_node(my_argument)
return my_node
get_node 函数只是一个从其他 API 获取对象的函数,这是一个 lxml 对象:
In [27]: type(my_node)
Out[27]: <type 'lxml.objectify.ObjectifiedElement'>
在我的 urls.py 中,我有:
from django.conf.urls.defaults import *
from piston.resource import Resource
from api.handlers import MyHandler
class CsrfExemptResource( Resource ):
def __init__( self, handler, authentication = None ):
super( CsrfExemptResource, self ).__init__( handler, authentication )
self.csrf_exempt = getattr( self.handler, 'csrf_exempt', True )
my_resource = CsrfExemptResource( MyHandler )
urlpatterns = patterns( '',
url( r'^api/(?P<my_argument>.*)/$', my_resource ),
)
当我访问该 URL 时(使用正确的参数,给定直接 get_node 给出了正确的对象),我只得到一个空字符串。
如何让它发挥作用?
I am trying to make a django-piston API which reads another API and presents it in a different format (reads an XML-only API and presents it in JSON). But I am having trouble, because the way I am doing it I just an empty string.
Here is my handler:
class MyHandler(BaseHandler):
methods_allowed = ('GET')
def read(self, request, my_argument):
my_node = get_node(my_argument)
return my_node
The get_node function is just a function which gets the object from the other API, and that is an lxml object:
In [27]: type(my_node)
Out[27]: <type 'lxml.objectify.ObjectifiedElement'>
In my urls.py I have:
from django.conf.urls.defaults import *
from piston.resource import Resource
from api.handlers import MyHandler
class CsrfExemptResource( Resource ):
def __init__( self, handler, authentication = None ):
super( CsrfExemptResource, self ).__init__( handler, authentication )
self.csrf_exempt = getattr( self.handler, 'csrf_exempt', True )
my_resource = CsrfExemptResource( MyHandler )
urlpatterns = patterns( '',
url( r'^api/(?P<my_argument>.*)/
And when I visit that URL (with the right argument, which given directly to get_node gives a correct object), I just get an empty string.
How to make it work?
, my_resource ),
)
And when I visit that URL (with the right argument, which given directly to get_node gives a correct object), I just get an empty string.
How to make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您需要从 lxml.objectify.ObjectifiedElement 中获得什么。如果您想从该元素获取文本内容,您可以通过以下方式访问它
如果您想要属性值,您可以通过以下方式访问它:
It depends on what you need from that lxml.objectify.ObjectifiedElement. If you want to get the text content from that element you can access it via
If you want an attribute value you can access it via: