关于API设计的问题
我正在研究 Web API 的最佳“设计模式”。我目前使用 Django 作为我的 Web 框架。我创建了一个非 ajax 界面,该界面会导致整个页面在每次请求时重新加载。
现在,我开始将 ajax 合并到界面中。为了便于讨论,我需要添加 API 的两个示例功能如下:
1) beta 页面:用户提供电子邮件地址。我想对服务器端进行 ajax 调用,以查看它是否已存在于我的数据库中。我对此调用的最初设计是一个类似于 2) 个人资料图片上传的查看功能
def check_email(request):
if request.method == "POST":
# check db
# return JSON true/false
新的个人资料图片会添加到您的页面,而无需重新加载整个页面
,据我所知, ,最好的方法是通过对视图函数的 POST 调用。然后,响应将返回 JSON,然后我可以将其相应地注入回 DOM 中。
有人可以让我知道我设计这个 API 的方向是否正确吗?
注意:我已经检查过 django-piston,它似乎也非常有用。
谢谢
I am in the process of researching the best "design-pattern" a web API. I am currently using Django as my web framework. I created a non-ajax interface that causes the entire page to reload at each request.
Now, I am starting incorporate ajax into the interface. For the sake of this discussion, two example pieces of functionality that I need to add an API for are the following
1) beta page: the user supplies an email address. i want to make an ajax call to the serverside to see if it already exists in my DB. my initial design for this call would be a view function similar to
def check_email(request):
if request.method == "POST":
# check db
# return JSON true/false
2) profile picture uploads, where the new profile picture is added to your page without a full page reload
as far as i can tell, the best way to do this is via a POST call to a view function. then, the response would return JSON, which I can then inject back into the DOM accordingly.
can someone please let me know if I am on the right track with designing this API?
note: i have checked out django-piston, and it seems pretty useful also.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你似乎走在正确的道路上。一个狡辩:要检查数据库中是否存在电子邮件地址,理想情况下应该使用 GET 而不是 POST,因为没有信息被更新 - 您只是询问某些内容是否存在。
返回 JSON 的一种替代方法是返回预渲染的 HTML,您可以在适当的点将其直接注入到 DOM 中。这样做的优点是您可以使用 Django 的模板机制来渲染现有的模板片段 - 唯一的区别是您单独渲染片段,而不是在整个 HTML 页面中包含/扩展它。
Yes, you seem to be on the right track. One quibble: to check if an email address exists in the db, you should ideally use a GET rather than a POST, as no information is being updated - you are simply asking if something exists.
One alternative to returning JSON is to return pre-rendered HTML which you can inject directly into the DOM at the appropriate point. The advantage of doing it this way is that you can use Django's template mechanism to render your existing template fragments - the only difference is that you render a fragment in isolation, rather than including/extending it within an entire HTML page.
你是对的,但我会使用
request.is_ajax()
来检查它是否实际上是一个 ajax 请求以相应地返回响应。来自 django 请求文档这将允许您为禁用了 javascript 的用户分隔返回值,从而使他们仍然可以正确使用您的网站。如果您不使用 javascript 库,则可以自行设置。
You're right on track, but I would use
request.is_ajax()
to check if it's actually a ajax request to return the response accordingly. from the django request docsThis will allow you to separate return values for users that have javascript disabled, allowing them to still use your site correctly. If you're not using a javascript library, you set this yourself.