Django API: how to construct URLs, and handle queries?
Forgive this newbie (and possibly subjective - I don't know) question.
I want to add a REST API to my site. For example, I have a URL that is /post/ that shows all posts, and I'd like to give users a way to get all posts back in JSON.
Is is better to:
- define 开发者_运维百科a new API URL structure (e.g.
/api/rest/post/to return all posts in JSON) - use the existing URL structure, and allow users simply to append
/json/on the end of each URL to get JSON objects back? (e.g./post/json/to return all posts in JSON)
If the latter, then is there a standard way to implement it, in terms of views? Should I simply add an optional json parameter to all my views?
Thank you for your advice.
Take a look at Piston, which is a Django plugin to handle REST APIs.
Listen to the previous commenter's advise. But in particular that's probably better to use new API URL structure (/api/rest/post/ as you've said). Separating totally different kinds of functionality is always good for your project. In other words, you can place your API documentation at /api/docs/, and it will look natural. If you use same URL structure, it will be not so obvious where to place your docs.
The answer is of course also subjective.
加载中,请稍侯......
精彩评论