如何使用格式后缀来获取djangorestframework返回xml? [英] How do you get djangorestframework to return xml using format suffix?
问题描述
我可以通过格式后缀.json获取djangorestframework返回json,但不能通过.xml后缀返回xml
I can get djangorestframework to return json via a format suffix .json, but not return xml via a .xml suffix
http://127.0.0.1:8000/chat/rooms/.json
[
{
id: 1,
timestamp: "2013-12-05T04:27:42Z",
topic: "important one"
},
{
id: 2,
timestamp: "2013-12-05T04:27:49Z",
topic: "important two"
},
{
id: 3,
timestamp: "2013-12-05T04:27:55Z",
topic: "important three"
},
{
id: 4,
timestamp: "2013-12-05T04:28:01Z",
topic: "important four"
},
{
id: 5,
timestamp: "2013-12-05T06:43:38Z",
topic: "another great stimulating topic"
}
]
http://127.0.0.1:8000/chat/rooms/.xml
{
detail: "Not found"
}
我做错了,b / c REST api显然正在工作...谢谢!
Could anyone tell me what I did wrong, b/c the REST api is clearly working...thanks!
推荐答案
你需要添加 XMLRendere r
,默认情况下未启用。
You need to add the XMLRenderer
which is not enabled by default.
要在设置中执行此操作,请执行以下操作:
To do this in settings have something like this:
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.XMLRenderer',
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
)
}
要在视图级别设置渲染器,请使用 render_classes
属性。
To set renderers at the view level use the render_classes
attribute.
查看渲染器文档。
更新:在我看来,上述只是答案的一半。您还需要添加 xml
格式后缀,如这里所述。
Update: It occurs to me the above is only half the answer. You'll also need to add the xml
format suffix, as documented here.
我希望有帮助。
这篇关于如何使用格式后缀来获取djangorestframework返回xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!