Django 中模板和静态文件的位置

发布于 2025-01-01 16:59:09 字数 427 浏览 0 评论 0原文

我正在计划一个新的 Django 项目,希望一切顺利。我偶然发现了如何组织项目目录布局的问题。幸运的是,网络上有很多优秀的项目模板示例。尽管如此,有一件事我很难理解:

建议将模板文件放入项目根目录下的单独目录中,该目录按应用程序分为子目录。因此,模板不位于应用程序目录中。这对我来说似乎是合乎逻辑的,因为我们希望将应用程序逻辑与表示逻辑分开。但是静态文件呢?在这里,常见的做法似乎是在应用程序目录中找到静态文件,并在开发时将它们加载到项目根目录下的“静态”目录中(collectstatic)。这个逻辑我不明白。由于静态文件(即 js、css、图像)通常在模板内访问,而不是在应用程序代码内访问,因此我将它们计入表示逻辑。那么为什么它们不像模板一样存储 - 项目根目录下的目录,以及单个应用程序的子目录?

我知道我可以将这些文件存储在任何我想要的地方,但我想人们这样做可能是有充分理由的。这个原因可能是什么?

I am planning a new Django project and want to get everything right and stuff. I stumbled over the question of how to organize the project directory layout. Luckily, there are quite a few examples of good project templates out there in the web. Still, there is one thing I struggle to get in my head:

It is the recommended way to put template files into a separate directory under the project root that is divided into subdirectories by apps. So, templates are not located within the app directories. That seems logical to me since we want to separate application logic from representation logic. But what about static files? Here, the common practice seems to be to locate the static files within the app dirs and load them into a 'static' directory under the project root at development time (collectstatic). And this logic I do not understand. Since static files (i.e. js, css, images) are usually accessed within templates, not within application code, I would count them to presentation logic. Then why aren't they stored just like templates are - a directory under the project root, with subdirectories for the single apps?

I know I can store these files wherever I want but I guess there might be a good reason why folks are doing it this way. What could this reason be?

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

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

发布评论

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

评论(2

意中人 2025-01-08 16:59:09

静态文件可以放入相关应用程序中,就像通常将与特定应用程序相关的模板放入应用程序目录中一样。

有时,这是有道理的。有时,情况并非如此——这是你的决定。

例如,我将静态媒体放在 site_media 目录中(全局 css、全局图像等),但将应用程序特定媒体放在 app/static 中。例如,如果我有一个投票应用程序,则很可能我的媒体仅用于投票应用程序模板,而不是我的网站索引。

模板也是如此:我将全局模板 (base.html) 放在全局模板目录中,但应用程序特定的模板放在 myapp/templates/myapp/foo.html 中。

最后,它对于可插拔应用程序特别有意义。例如,django 的静态文件存储在应用程序中,但可以在静态文件目录中访问,即使应用程序位于 python 路径上的某个位置。以前,您必须将媒体目录或符号链接复制到其中。

staticfiles 应用程序确实很出色,因为它可以让您将与应用程序相关的所有文件组织到一个位置:应用程序文件夹。 collectstatic 负责剩下的工作,并使其在一个位置可供 Web 服务器使用。

Static files can be put in the related app in the same way that templates related to a specific app are often put in the application directory.

Sometimes, it makes sense. Sometimes, it doesn't - it's your call.

For example, I put my static media in a site_media directory (global css, global images, etc) but put app specific media in app/static. For example, if I have a Poll app, there's a good chance that my media is only needed for the Poll app templates, and not my site index.

The same goes for templates: I put my global templates (base.html) in a global template directory, but app specific templates go in myapp/templates/myapp/foo.html.

Finally, it particularly makes sense for pluggable apps. For example, django's static files are stored in the app but becomes accessible in your static files directory even though the app lives somewhere on your python path. Previously, you would have had to copy the media directory or symlink to it.

The staticfiles app really shines because it lets you organize all files related to an application in one place: the application folder. collectstatic takes care of the rest, and makes it all available at one location for a web server.

浴红衣 2025-01-08 16:59:09

我实际上将模板放在每个应用程序的目录中,我认为这是有道理的。由于其他一切都发生在 Python 文件中,因此逻辑分离仍然存在。

但是,将模板目录放在应用程序中意味着如果该应用程序对其他项目有用,您可以直接将应用程序复制到新项目中。您始终可以通过在根模板文件夹中创建备用模板来覆盖这些模板。

正是由于这个原因,静态目录也应该存储在应用程序目录中;它允许您根据特定应用程序的需要非常清晰地组织资源,这就是首先创建静态文件应用程序的原因。

I actually put templates in the directory for each app and I think it makes sense. You still have separation of logic separation since everything else is happening in Python files.

However, putting the templates directory in the app means that if it turns out the app is useful for other projects, you can just copy the app over directly to the new project. And you can always override those templates by creating alternate ones in your root templates folder.

It's for this reason that the static directory should also be stored in the app directory; it allows you to very clearly organize resources based on what is needed for a specific app and it's why the staticfiles app was created in the first place.

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