Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

python and RESTful django - consume XML

  • 28-03-2011 6:48pm
    #1
    Registered Users, Registered Users 2 Posts: 7,893 ✭✭✭


    Hi,
    I have created a python app which runs fine on my localhost, however, it uses the bottle library. Now, I want to convert it to Django since I found some free hosting that allows for python and django.

    The app reads off an XML file, which contains coordinates, and when a user goes mysite.com/25, for example, it'll return, in XML or JSON, the coords of the element in the XML with the ID of 25.

    I've used mostly standard python libraries so maybe its my Django config thats the problem.
    See here:
    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)
    

    Theres about 15 lines of code there and I need to convert it. Anybody got any idea how I can go about doing this?

    I've never used Django before but have managed to get the basic "Congratulations! You have your server configured!" page up and running. This was in Windows but I've just loaded up and Ubuntu VM to try it in that so if someone can assist me in doing this, it'd be much appreciated!


Comments

  • Registered Users, Registered Users 2 Posts: 85 ✭✭slavigo


    Hi,

    I'd recommend reading the tutorial at "Writing your first Django app", it's not fully what your after but it'll give you a basic understanding of how django works under the hood.
    In your case, you'll be interested in views and urls.

    Once you have done that, to get it working for your example you'll need:

    1) Write a View, which takes the "request" object and the requested number as arguments.
    This view will do the working that your "index" function was doing.
    It'll need to return a HttpResponse that has the correct mime type, since your not returning html.
    http://docs.djangoproject.com/en/1.3/ref/request-response/#httpresponse-objects

    2) Once you have your View written you need to write an entry in the urls.py file to map to the View.
    In your case, you'll need a url regular expression that matches to the base of the url of your page and then it also looks for an integer.
    Something like (It might not be perfect though)
    r'^mysite/(?P<number>\d+)$'

    Once you have these, you have basically told django to send all requests to a url that matches the regular expression above to the view you have just written. It will pass the argument "number" to the view along with the request object.

    All of what I just wrote will make sense once you have worked through the tutorial I referenced above.

    On a side note, when you get that far, you might find it more convenient to store your geo information in some django models. These can be searched and manipulated easier and quicker with django's internal model orm.
    I suppose it depends on how much xml you have and if it's likely to grow.

    Anyway, I hope that helped (it's probably all useless without the tutorial thought).
    Thanks,
    Dave.


Advertisement