如何在项目中实际使用可重用的django应用程序?

时间:2022-03-26 18:14:45

This question has been troubling me for some days now and I've tried asking in many places for advice, but it seems that nobody can answer it clearly or even provide a reference to an answer.

这个问题已经困扰了我好几天了,我试着在很多地方征求意见,但似乎没有人能清楚地回答它,甚至没有人能提供答案的参考。

I've also tried searching for tutorials, but I just cannot find any type of tutorial that explains how you would use a reusable third-party django app (most tutorials explain how to write them, none explain how to use them).

我也尝试过搜索教程,但是我找不到任何类型的教程来解释如何使用可重用的第三方django应用程序(大多数教程都解释了如何编写它们,没有解释如何使用它们)。

Also, I've taken a look here:

另外,我在这里看了一下:

How to re-use a reusable app in Django - it doesn't explain how to actually use it IN a project itself

如何在Django中重用一个可重用的应用程序——它没有说明如何在项目本身中实际使用它

and here:

在这里:

How to bind multiple reusable Django apps together? - the answer by aquaplanet kind of makes sense, but I thought I would ask this question to solve the mental block I am facing in trying to understand this.

如何将多个可重用的Django应用程序绑定在一起?aquaplanet的答案是有道理的,但是我想我可以问这个问题来解决我在试图理解这个问题时所面临的心理障碍。


In order to best explain this, let me do so by example (note, it is not something I am actually building).

为了更好地解释这个问题,让我举个例子(注意,这不是我真正构建的东西)。

I am creating a project that acts like Reddit. I will have users, links and voting/points. Based on this crude example, I will want to reuse 3 (arbitrary) third-party apps: user, voting/points and links.

我正在创建一个类似Reddit的项目。我将有用户,链接和投票/点。基于这个粗略的示例,我将重用3个(任意的)第三方应用程序:用户、投票/点和链接。

I decide to use each of them as any other python package (meaning that they will be treated as a package and none of their code should be touched) [would this method actually work? Or do you have to be able to edit third-party apps to build a project??)

我决定将它们作为任何其他python包使用(这意味着它们将被视为一个包,它们的任何代码都不应该被修改)[这个方法实际上会工作吗?或者你必须能够编辑第三方应用程序来构建一个项目?

With these apps now within my project, I will use a main app to handle all the template stuff (meaning everything I see on the frontend will be in a single app).

现在在我的项目中有了这些应用程序,我将使用一个主应用程序来处理所有的模板(这意味着我在前端看到的所有东西都将在一个应用程序中)。

I will then either use that same main app for custom logic (in views.py) or I will break up that logic among different apps (but will still use a single frontend-only app).

然后,我要么使用相同的主应用程序进行自定义逻辑(在views.py中),要么在不同的应用程序之间分解这个逻辑(但仍将使用单一的前端应用程序)。

From the 3 paragraphs above, is this structure applicable (or can it work) ?

从上面的3个段落来看,这个结构是否适用(或者它可以工作)?


Now lets say that this structure is applicable and I am using a single main app for the frontend and custom logic.

现在我们说这个结构是适用的,我正在使用一个单一的主应用程序的前端和自定义逻辑。

What would I write in models.py? How would I integrate things from the 3 reusable apps into the main models.py file?

我用model .py写什么?我将如何将3个可重用应用程序集成到主模型中。py文件?

How would I reference the reusable apps in views.py? Lets take the example of contrib.auth

我如何在views.py中引用可重用应用程序?让我们以发明为例

With this built-in app, for logging out I would write:

有了这个内置的应用,我就可以登出:

from django.contrib.auth import authenticate, login, logout
@login_required
def user_logout(request):
logout(request)

return HttpResponseRedirect('/home/')

Although the above code is simple, is that basically how things would be done with any reusable app?

虽然上面的代码很简单,但这就是如何使用任何可重用的应用程序来完成的吗?

My question is very long, but I think that this reusable app issue is something a lot of developers aren't quite clear about themselves and maybe this answer will help a lot of others who have heard about the promises of reusable apps, but fail to understand how to actually use them.

我的问题是很长,但我认为,这是可重用的应用程序的问题很多开发人员不太清楚自己,也许这个答案将帮助很多人听说过的承诺可重用的应用程序,但实际上无法理解如何使用它们。

2 个解决方案

#1


3  

TL;DR:

Nope & it depends...

不与这取决于……

Some (Very) Common Reusable Apps

... those are all reusable Django apps, that happen to be shipped with Django (most of them were not, at some point in time)

…这些都是可重复使用的Django应用程序,这些应用程序都是用Django进行的(在某些时候,大多数都不是)

Ok, some other reusable apps that don't ship with Django:

好的,还有一些可重用的应用程序没有Django提供:

Those are all truly reusable apps, and nothing less. There are very many more apps like that.

这些都是真正可重用的应用程序,而且都是如此。还有很多这样的应用。

How do they do it?

To me your question looks more like "how do I build reusable apps", then "how to use them". Actually using them is very different from app to app, because they do very different things. There is only one rule: RTFM No way around that either.

对我来说,你的问题更像是“如何构建可重用的应用”,然后是“如何使用它们”。实际上,不同的应用程序使用它们是非常不同的,因为它们做的事情非常不同。只有一条规则:RTFM也不可能绕开这一点。

Often, they rely on one or more of the following:

他们通常依赖以下一种或几种:

  • additional value(s) in settings.py
  • 在settings.py附加价值(s)
  • addition (usually one include statement) to urls.py
  • 向urls.py添加(通常是一个include语句)
  • subclassing and/or mixins for Models, Forms, Fields, Views etc.
  • 用于模型、表单、字段、视图等的子类化和/或混合。
  • template tags and/or filters
  • 模板标签和/或过滤器
  • management commands
  • 管理命令
  • ...

Those are all powerful ways though which your app can provide functionality to other apps. There is no recipe (AFAIK) to make a reusable app, because there are so many different scenarios to consider. It all depends on what exactly your app should do.

这些都是你的应用可以为其他应用提供功能的强大方式。制作一个可重用的应用程序没有秘诀(AFAIK),因为有太多不同的场景需要考虑。这完全取决于你的应用应该做什么。

Reusable apps provide functionalities

I'd argue that it's important to not think of reusable apps as "working together" with other app, but instead recognize that that they "provide functionality." The details of the functionality provided should dictate the way the target developer is supposed to use your library.

我认为重要的是不要将可重用应用程序视为与其他应用程序“协同工作”,而是要认识到它们“提供了功能”。提供的功能的细节应该规定目标开发人员使用库的方式。

Not everything should be reusable

Obviously enough, even though many apps can "in principle" be reusable, it often makes little sense to do so, because it is way faster to clump things together (and make them just "work together").

很明显,即使很多应用程序可以“原则上”是可重用的,但这样做也没什么意义,因为把东西聚集在一起(并让它们“一起工作”)的速度更快。

#2


0  

I'm not sure why you think you need a main app for the "frontend" stuff. The point of a reusable app is that it takes care of everything, you just add (usually) a single URL to include the urls.py of the app, plus your own templates and styling as required.

我不知道为什么你认为你需要一个“前端”的应用程序。一个可重用应用程序的要点是,它负责所有的事情,您只需(通常)添加一个URL来包含URL。应用的py,加上您自己的模板和样式。

And you certainly don't need to wrap the app's views in your own views, unless you specifically want to override some functionality.

当然,你也不需要将应用程序的视图封装到你自己的视图中,除非你特别想要覆盖某些功能。

I don't understand at all your question about models. There's no such thing as a "main" models file, and using a reusable app's models is just the same as using models from any of your own apps.

我一点也不理解你关于模特的问题。不存在“主”模型文件,使用可重用应用程序的模型与使用来自自己应用程序的模型是一样的。

Normally you would not edit a third-party app, that would make it very hard to integrate updates. Just install the app in your virtualenv (you are using virtualenv, of course!) with pip, which will put it in the lib directory, and you can reference it just like any other app. Make sure you add it to INSTALLED_APPS.

通常情况下,你不会编辑第三方的应用程序,这将使整合更新变得非常困难。在你的virtualenv(当然是使用virtualenv,当然是使用virtualenv)安装这个应用程序,它会把它放到lib目录中,你可以像任何其他的应用一样引用它,确保你将它添加到INSTALLED_APPS中。

#1


3  

TL;DR:

Nope & it depends...

不与这取决于……

Some (Very) Common Reusable Apps

... those are all reusable Django apps, that happen to be shipped with Django (most of them were not, at some point in time)

…这些都是可重复使用的Django应用程序,这些应用程序都是用Django进行的(在某些时候,大多数都不是)

Ok, some other reusable apps that don't ship with Django:

好的,还有一些可重用的应用程序没有Django提供:

Those are all truly reusable apps, and nothing less. There are very many more apps like that.

这些都是真正可重用的应用程序,而且都是如此。还有很多这样的应用。

How do they do it?

To me your question looks more like "how do I build reusable apps", then "how to use them". Actually using them is very different from app to app, because they do very different things. There is only one rule: RTFM No way around that either.

对我来说,你的问题更像是“如何构建可重用的应用”,然后是“如何使用它们”。实际上,不同的应用程序使用它们是非常不同的,因为它们做的事情非常不同。只有一条规则:RTFM也不可能绕开这一点。

Often, they rely on one or more of the following:

他们通常依赖以下一种或几种:

  • additional value(s) in settings.py
  • 在settings.py附加价值(s)
  • addition (usually one include statement) to urls.py
  • 向urls.py添加(通常是一个include语句)
  • subclassing and/or mixins for Models, Forms, Fields, Views etc.
  • 用于模型、表单、字段、视图等的子类化和/或混合。
  • template tags and/or filters
  • 模板标签和/或过滤器
  • management commands
  • 管理命令
  • ...

Those are all powerful ways though which your app can provide functionality to other apps. There is no recipe (AFAIK) to make a reusable app, because there are so many different scenarios to consider. It all depends on what exactly your app should do.

这些都是你的应用可以为其他应用提供功能的强大方式。制作一个可重用的应用程序没有秘诀(AFAIK),因为有太多不同的场景需要考虑。这完全取决于你的应用应该做什么。

Reusable apps provide functionalities

I'd argue that it's important to not think of reusable apps as "working together" with other app, but instead recognize that that they "provide functionality." The details of the functionality provided should dictate the way the target developer is supposed to use your library.

我认为重要的是不要将可重用应用程序视为与其他应用程序“协同工作”,而是要认识到它们“提供了功能”。提供的功能的细节应该规定目标开发人员使用库的方式。

Not everything should be reusable

Obviously enough, even though many apps can "in principle" be reusable, it often makes little sense to do so, because it is way faster to clump things together (and make them just "work together").

很明显,即使很多应用程序可以“原则上”是可重用的,但这样做也没什么意义,因为把东西聚集在一起(并让它们“一起工作”)的速度更快。

#2


0  

I'm not sure why you think you need a main app for the "frontend" stuff. The point of a reusable app is that it takes care of everything, you just add (usually) a single URL to include the urls.py of the app, plus your own templates and styling as required.

我不知道为什么你认为你需要一个“前端”的应用程序。一个可重用应用程序的要点是,它负责所有的事情,您只需(通常)添加一个URL来包含URL。应用的py,加上您自己的模板和样式。

And you certainly don't need to wrap the app's views in your own views, unless you specifically want to override some functionality.

当然,你也不需要将应用程序的视图封装到你自己的视图中,除非你特别想要覆盖某些功能。

I don't understand at all your question about models. There's no such thing as a "main" models file, and using a reusable app's models is just the same as using models from any of your own apps.

我一点也不理解你关于模特的问题。不存在“主”模型文件,使用可重用应用程序的模型与使用来自自己应用程序的模型是一样的。

Normally you would not edit a third-party app, that would make it very hard to integrate updates. Just install the app in your virtualenv (you are using virtualenv, of course!) with pip, which will put it in the lib directory, and you can reference it just like any other app. Make sure you add it to INSTALLED_APPS.

通常情况下,你不会编辑第三方的应用程序,这将使整合更新变得非常困难。在你的virtualenv(当然是使用virtualenv,当然是使用virtualenv)安装这个应用程序,它会把它放到lib目录中,你可以像任何其他的应用一样引用它,确保你将它添加到INSTALLED_APPS中。