Django 应用程序到底是什么

发布于 2024-12-02 16:32:50 字数 371 浏览 2 评论 0原文

我想从 Rails 转向 Django,以拓宽我的视野,一个问题在我的脑海中浮现。

我的 Rails 应用程序相当混乱,因为我基于爱好的开发方法是修补和粘合的方法。我很早就看到 Django 分为项目和应用程序。根据他们的网站,一个项目由许多应用程序组成,一个应用程序可以用于多个项目。

这引起了我的兴趣,因为这将使我的网站区域之间的界限更加清晰。我试图找到更多关于这方面的示例和信息,但我无法回答我的问题,即:

这样的应用程序有多大/小?他们能够/应该密切互动吗?

例如,让一个应用程序来处理用户的个人资料,而另一个应用程序来处理这些用户的博客文章和评论,这是否明智? (在我的网站中,一个用户可以拥有多个具有不同配置文件的博客)。或者它们是否应该用于其他用途?

I want to switch from Rails to Django, to broaden my mind, and a question has bobbed up in my mind.

My Rails app is quite a mess, since my hobby-based development approach is a patch-and-glue one. I have seen very early that Django divied between a project and an app. According to their site, a project is made of many apps, and one app can be used for many projects.

This intrigued me, since that would made the lines between my site's areas clearer. I tried to find some more examples and information on that, but I couldn't answer my question, which is:

How big/small is such an app? Are they able/supposed to interact closely?

It is, for example smart to have one app to deal with user's profiles, and another app to deal with blog-posts and comments, from those users? (In my site, a user can have several blogs, with different profiles). Or are they meant to be used otherwise?

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

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

发布评论

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

评论(4

很糊涂小朋友 2024-12-09 16:32:50

django 应用程序是一个 python 包的奇特名称。真的,就是这样。 django 应用程序与其他 python 包的唯一区别是,它出现在 settings.pyINSTALLED_APPS 列表中是有意义的,因为它包含以下内容:模板、模型或其他 django 功能可以自动发现的功能。

一个好的 django 应用程序只会做一件事,并且做得很好,并且不会与可能使用它的任何其他应用程序紧密耦合。 django 在 contrib 命名空间中提供了遵循此约定的各种应用程序。

在您的示例中,设计应用程序的一种好方法是拥有一个用于用户个人资料的应用程序(或使用许多现有的个人资料应用程序之一),一个用于博客文章的应用程序(或许多已经这样做的应用程序之一),一个用于评论的应用程序,与博客文章分开(同样,您可以为此使用现有的应用程序),最后,一个非常小的应用程序将这三个应用程序联系在一起,因为它们不也不应该直接相互依赖。

A django App is a fancy name for a python package. Really, that's it. The only thing that would distinguish a django app from other python packages is that it makes sense for it to appear in the INSTALLED_APPS list in settings.py, because it contains things like templates, models, or other features that can be auto-discovered by other django features.

A good django app will do just one thing, do it well, and not be tightly coupled to any other app that might use it. A wide variety of apps are provided with django in the contrib namespace that follow this convention.

In your example, a good way to devise apps is to have one for user profiles (or use one of the many existing profile apps), one app for blog posts (or one of the many that already do this), one app for comments, separate from blog posts (again, you can use an existing app for this), and finally, a very tiny app that ties the three together, since they don't and shouldn't depend on each other directly.

不爱素颜 2024-12-09 16:32:50

使用应用程序的目的是使它们可重用。 Django 喜欢DRY 原则 DRY 代表不要重复自己

应用程序应该尽可能小,并且松散耦合。因此,一个应用程序不应该需要另一个应用程序才能正常工作。

Django 建议为每个表编写一个应用程序(当然,并非总是如此,但是一旦您想要扩展您的应用程序,您肯定需要划分表来配对应用程序。否则您将很难维护代码。

)例如,可以为用户创建一个应用程序、一个用于销售的应用程序、一个用于评论的应用程序、一个用于文章的应用程序。维护您的代码会更容易,如果您做得正确,您可以在其他项目中使用该应用程序,并在应用程序中进行一些(如果有)修改。

Projectapp 的编译。用户应用程序、文章应用程序、评论应用程序可以组合在一起制作一个项目,或者换句话说,一个网站。

如果你想学习django,我建议你查看:

http://www.djangobook.com/

http://docs.djangoproject.com/


一句话建议,不要 ,无论如何,复制/粘贴。不仅你的代码很有可能失败,而且你也不知道代码在做什么。如果您打算在项目中使用其他人的代码,至少键入它们,这将使您了解代码的用途,或者至少会给您一个想法。

编写自己的代码总是更便于维护,但这并不意味着您应该重新发明世界,您可以使用库,查看它们的文档以正确使用它。

文档、教程是你最好的朋友。

祝你好运。

The purpose of using app's is to make them reusable. Django likes DRY principle DRY stands for DO NOT repeat yourself

An app should be as small as it can, and loosely coupled. So, for an app should not need another app to work properly.

Django recommends writing an app for each table (well, not always, but as soon as you want to grow your app, you will definitely need to divide tables to pair apps. Or else you will have hard time for maintaining your code.)

You can, for example, create an app for users, an app for sales, an app for comments, an app for articles. It will be easier to maintain your code and if you have done it right, you can use the app in other project with a little (if any) modification in the app.

Project's are, compilation of app's. Users app, articles app, comments app can come together an make a project, or in other words, a website.

If you want to learn django, I suggest you to check out:

http://www.djangobook.com/

http://docs.djangoproject.com/


One word of advice, do not, in any case, copy/paste. Not only your code has great chance to fail, but you will not know what the code is doing. If you are going to use someone elses code in your project, at least type them, this will make you understand what the code is doing, or at least, it will give an idea.

Writing your own code is always better for maintance, but this does not mean that you should reinvent the world, you can use libraries, look at their documentation to use it properly.

Documentations, tutorials are you best friend.

Good luck.

落墨 2024-12-09 16:32:50

项目基本上是项目所在的地方...在项目中,您可以设置 url、项目设置等。

应用程序定义要在项目中使用的自己的数据模型和视图。如果您愿意,您可以在项目之间移动它们。

我强烈建议您浏览 Django 站点的教程,因为它们会向您展示项目和应用程序是什么,如何管理两者,如何使用管理面板,如何使应用程序可在多个项目中使用等。

A project is basically a place where your project lives...in your project you setup your url's, project settings, etc.

An app defines its own data models and views to be used within a project. You can move these between projects if you like.

I highly recommend running through the tutorials from the Django site as they will show you what a project and app are, how to manage both, how to use the admin panel, how to make the app usable in multiple projects, etc.

最舍不得你 2024-12-09 16:32:50

门户 = django 项目

广告系统、图库照片、产品目录 = 应用程序

A portal = A django project

A ads system, gallery photos, catalog of products = Apps

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