从 Django 视图使用 django-piston REST API 的最佳方法是什么?

发布于 2024-09-27 16:43:10 字数 169 浏览 1 评论 0原文

我已经开始使用 django-piston 来创建 API,但我无法找到有关如何使用 python 从 django 内部使用 API 的文档。我见过使用 javascript 的例子。

到目前为止,我正在使用 urllib 来访问 API,但我想知道这是否是最好的方法。

感谢您对此的意见!

I have started using django-piston for creating APIS but I am having trouble finding documentation on how to consume the API from inside django with python. I have seen examples using javascript.

So far I am using urllib to access the API but I wonder if this is the best approach.

I appreciate your input on this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心清如水 2024-10-04 16:43:10

如果这是一个内部 API——也就是说,您/您的视图和 API 对资源具有相同的访问权限——为什么您要使用 API,而不是正常获取其结果(通过模型操作等)?如果您想避免代码重复,请将公共代码分解为可供两个进程使用的单独函数。

如果这是一个外部 API(例如,您的站点正在与 BitBucket 进行通信),则 HTTP 客户端大约是使用它的唯一(合理)方式。虽然我个人会选择 httplib 而不是 urllib。

If this is an internal API -- that is, you/your views and the API have the same access to resources -- why are you consuming the API rather than getting its results normally (through model manipulation, etc)? If you want to avoid code duplication, break out common code into separate functions that can be used by both processes.

If this is an external API -- for example, your site is communicating with BitBucket -- an HTTP client is about the only (sane) way to go about consuming it. Though I personally would choose httplib over urllib.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文