Django - 使用 XML - RESTful
我有一个 python 脚本在我的本地主机上运行良好。它不是一个企业应用程序或任何东西,只是我正在玩的东西。它使用“bottle”库。该应用程序基本上使用一个 XML 文件(本地存储或在线存储),其中包含具有自己唯一 ID 的元素以及一些坐标,例如 mysite.com/23 将带回元素 23 的纬度/经度。我确信无论如何,在这个阶段你们都已经熟悉了 REST。
现在,我想把它放到网上,但找不到支持“bottle”的主机。然而,我找到了一个安装了 django 的主机。
所以,我的问题是,将以下代码从 Bottle 转换为 django 有多难?有人可以给我一些指点吗?我尝试过使用常见的 python 库。
谢谢。
from xml.dom.minidom import parseString
from bottle import route, run
import xml
import urllib
file = open('myfile.xml','r')
data = file.read()
dom = parseString(data)
@route('/:number')
def index(number="1"):
rows = dom.getElementsByTagName("card")[0].getElementsByTagName("markers")[0].getElementsByTagName("marker")
for row in rows:
if row.getAttribute("number") == str(number):
return str(xml.dumps({'long': row.getAttribute("lng"), 'lat': row.getAttribute("lat")}, sort_keys=True, indent=4))
return "Not Found"
run(host='localhost', port=8080)
I have a python script running fine on my localhost. Its not an enterprise app or anything, just something I'm playing around with. It uses the "bottle" library. The app basically consumes an XML file (stored either locally or online) which contains elements with their own unique IDs, as well as some coordinates, eg mysite.com/23 will bring back the lat/long of element 23. I'm sure you're all familiar with REST at this stage anyway.
Now, I want to put this online, but have had trouble finding a host that supports "bottle". I have, however, found a host that has django installed.
So, my question is, how hard would it be to convert the following code from bottle to django? And can someone give me some pointers? I've tried to use common python libraries.
thanks.
from xml.dom.minidom import parseString
from bottle import route, run
import xml
import urllib
file = open('myfile.xml','r')
data = file.read()
dom = parseString(data)
@route('/:number')
def index(number="1"):
rows = dom.getElementsByTagName("card")[0].getElementsByTagName("markers")[0].getElementsByTagName("marker")
for row in rows:
if row.getAttribute("number") == str(number):
return str(xml.dumps({'long': row.getAttribute("lng"), 'lat': row.getAttribute("lat")}, sort_keys=True, indent=4))
return "Not Found"
run(host='localhost', port=8080)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我把你的问题作为进一步了解 Django 的机会。我使用 The Django Book 作为参考。
从一个空的 Django 站点 (
django-admin.py startproject testsite
) 开始,我将urls.py
更改为:和
views.py
> 对此:警告:我没有测试 XML 代码,只测试了与 Django 相关的代码,我已经通过
python manage.py runserver
对其进行了测试。Django Book 包含大量信息,包括如何在生产服务器上部署它。
I took your question as an opportunity to learn a bit more about Django. I used The Django Book as a reference.
Starting with an empty Django site (
django-admin.py startproject testsite
), I've changedurls.py
to this:And
views.py
to this:Caveat: I've not tested the XML code, only Django-related one, which I've tested via
python manage.py runserver
.The Django Book contains a lot of information, including how to deploy this on a production server.