[Django]-Should my web based app be a consumer of my api?

4👍

Break it into three “sections”. The first uses a Python API to manipulate the database. The second interfaces your REST API with your Python API. The third talks web and uses the Python API.

1👍

I would create the web application to serve the API to the mobile client. That is, give the web based application direct access to the database. This will simplify your XML / JSON RESTful resource access.

1👍

I would say no, don’t use the API for the HTML version. If you design your Django well, you can end up with less code than using the API for the HTML version. You also get to retain the ability to have a web designer work with the Django templates whenever the boss wants the spelling changed on something.

I’d suggest trying to define a base app for your iPhone app to interface with, and then extend that within a second app for the HTML version. App1 would have all of your models (including business logic), and a views.py for processing the data to/from the iPhone. Then create App2 which uses App1.models, but creates its own views.py. With any luck, you’ll find yourself with the ability to change nothing but the template used to render the output, so you can reuse your views by passing the template as an argument.

For example:

App1.views:

def list(request, template="list.json"):
    list = Model.objects.filter(deleted=False).filter(user=request.user)
    list.reverse()
    ## Lots of other logic to work on the list.
    return render_to_response(template, {list: list,})

App2.views:

def list(request, template="list.html"):
    return App1.views.list(request, template=template)

0👍

I think the answer to this question has changed over time. A year ago when asked it was probably still too much hassle to do this, but now I’d definitely say yes – using your API as the basis is the smart thing to do. As Web sites use more HTML5 and Mobile apps get smarter it really makes sense to have all your “UIs” read/writing from the same API layer. This will give you much more flexibility in the future.

👤steve

Leave a comment