如何在单个站点中的不同 django 应用程序之间进行交互?

发布于 2024-12-13 23:38:56 字数 240 浏览 1 评论 0原文

我刚刚了解了 Django 应用程序。我想知道在一个网站内,我是否制作了不同的应用程序。例如,用户、个人资料、民意调查、博客、评论、工作、应用程序,那么我如何管理它们以使它们具有交互性? app的概念应该是这样的吗?我想让事情松散耦合,这就是为什么问? Rails 以 REST 方式工作,那么 Django 也支持使用应用程序吗?可能我的问题看起来有点模棱两可,因为我是 django 的新手,而且我的一些概念仍然很混乱。

请告诉我你所知道的一切。

I have just learnt about Django apps. I want to know that within one site, if I make different apps. like, users, profiles, polls, blogs,comments, jobs , applications then how can I manage them to make them intereactive? And is it how the concept of app should be? I want to have things loosely coupled that's why asking? Rails work on the way of REST, so do Django support that also with the use of apps? May be my questions seems a bit ambiguous because I am new to django and some of my concepts are still messed up.

Please tell what ever you know.

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

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

发布评论

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

评论(1

段念尘 2024-12-20 23:38:56

一般的想法是应用程序应该尽可能松散耦合。目标是拥有完全独立的功能。当然,现在这并不总是可能的,很多时候从另一个应用程序引入功能甚至是有意义的。为此,您只需导入所需的任何内容即可。例如,如果您的“博客”应用程序需要在“评论”应用程序中使用您的 Comment 模型,您只需将以下内容添加到您正在使用的 python 文件的顶部

from comments.models import Comment

:然后可以使用Comment,就好像它是在同一个文件中定义的一样。

就 REST 而言,Django 的视图更加流畅。您可以随意命名您的视图;您只需将其连接到 urls.py 中正确的 urlpattern 即可。 Django 视图可以返回任何内容类型,您只需准备响应并告诉它使用什么 mimetype(默认为 HTML)。

The general idea is that apps should be as loosely coupled as possible. The goal is to have completely self-contained functionality. Now, of course, that's not always possible and many times it even makes sense to bring in functionality from another app. To do that, you simply import whatever you need. For example, if your "blogs" app needed to work with your Comment model in your "comments" app you'd simply add the following to the top of the python file you're working in:

from comments.models import Comment

You can then use Comment as if it were defined right in the same file.

As far as REST goes, Django's views are much more fluid. You can name your view whatever you like; you need only hook it up to the right urlpattern in urls.py. Django views can return any content type, you just prepare the response and tell it what mimetype to serve it as (the default is HTML).

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