Reputation: 17392
I'm trying to write a "standard" business web site. By "standard", I mean this site runs the usual HTML5, CSS and Javascript for the front-end, a back-end (to process stuff), and runs MySQL for the database. It's a basic CRUD site: the front-end just makes pretty whatever the database has in store; the backend writes to the database whatever the user enters and does some processing. Just like most sites out there.
So, I want to make it as a MTV structure (model-template-view) in Django. I want to ask that when a user makes an call to the server from the front-end (ie the template), should it be via an API call or a direct call to a particular page through AJAX?
Performing via API will be modular and clean way, but then I guess relatively slow (not sure though).
What would be the correct way of approaching this, keeping in mind the information to be displayed will be provided by the API
Thanks
Upvotes: 2
Views: 736
Reputation: 3120
I had a similar question. First of all, I think it depends on whether your business has plans to create native mobile applications, etc. and whether it will need to fetch the same data as the website. In that case, a REST API would be optimal. In that case, it'd be simpler to maintain a single endpoint to interact with your data than multiple, so I'd make the website interact through the API as well. This diagram from tuts exemplifies this.
I think that you see the simplicity and modularity of the API, so at the end your question is about efficiency and whether it's a waste to have your website make an HTTP request to its own API instead of interacting with the models directly. Regarding this point, I don't think it's something to worry about (or shouldn't be unless your API calls are enormous and bottleneck your application).
The approach of building your frontend around your own API seems to work for Twitter (read "The Tech Behind the New Twitter.com") and TripAdvisor ("TripAdvisor Architecture - 40M Visitors, 200M Dynamic Page Views, 30TB Data") among others. At the end of the day, a single page load today might take tens to hundreds of HTTP requests so a few more for the actual data should not cause a problem.
Upvotes: 2