Django文件夹结构问题 [英] Django folder structure issues

查看:42
本文介绍了Django文件夹结构问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自Apache/php世界,有些东西我想不出要寻找的django Development最佳实践:

Coming from an Apache / php world there is something i can't figure out searching for django Development best practices :

使用symfony2框架(甚至Java Play!),您始终会有一个"public"文件夹,并且Web服务器仅提供该文件夹中的文件.出于安全原因的明显策略,在开发过程中也很清楚将公用文件放在此文件夹中.

With symfony2 framework (or even java Play!) you always have a "public" folder and the webserver only serve files from this folder. Obvious Strategy for security reason it is also clear during dev process to put public files in this folder.

在django中一点都不清楚:从我的阅读来看,在根目录下有一个静态文件夹,再加上一个UGC文件的媒体文件夹和à模板文件夹,这似乎是一种好习惯.没有主公用"文件夹.

In django it is not clear at all : from my readings it appears as good practice to have a static folder at root level PLUS a Media folder for UGC files and à template folder. No main "public" folder.

有人可以帮助我在这里打消主意吗?拥有一个文件夹来容纳所有请求并保护应用程序的其余部分是否更安全?

Can someone help me to clear my mind here ? Would't it be more secure to have one folder to contain all requests and protect the rest of the app ?

坦克

推荐答案

Django中有两种不同类型的静态"文件.

There are two different types of 'static' files in django.

  1. 与应用程序捆绑在一起的资源/资产(通常是css,javascript)
  2. 您的用户上传的任何文件.

由于这是两种不同的静态文件类别,因此django提供了两种机制来处理它们.由于第一个条件比第二个条件更常见(您可能没有需要用户上传文件的应用程序),因此django内置了第一个条件.

Since these are two different categories of static files, django offers two mechanisms to deal with them. As the first is more common than the second (you may not have an application that requires users to upload files), dealing with the first condition comes built in with django.

按照标准布局,需要静态文件的应用程序会将它们包括在应用程序目录内名为 static 的目录中.Django将在 INSTALLED_APPS 中的任何应用程序内搜索此目录以查找静态文件.如果您的文件没有绑定到任何应用程序,则可以将它们放在单独的目录中.此目录应添加到 STATICFILES_DIRS (元组)中,以便django知道.

As per the standard layout, applications that require static files will include them in a directory called static within the application directory. Django will search for this directory inside any app that is in INSTALLED_APPS for static files. If you have files that are not tied to any app, you can put them in a separate directory. This directory should be added to STATICFILES_DIRS (a tuple) so django is aware of it.

完成此操作后, collectstatic 命令将收集所有静态文件(来自 INSTALLED_APPS 中所有应用程序的 static 子目录中, STATICFILES_DIRS 中的任何目录),并将它们放在 STATIC_ROOT 指向的目录中;因此, {{STATIC_URL}} 标记在模板中可以正常工作.

Once you have done this, the collectstatic command will gather all the static files (from the static subdirectories in all applications in INSTALLED_APPS and any directory in STATICFILES_DIRS) and put them in a directory pointed to by STATIC_ROOT; this is so {{ STATIC_URL }} tags work correctly in templates.

现在,您要做的就是移动/指向/链接 STATIC_ROOT 目录,以便可以从网络上访问它.Django希望 STATIC_ROOT 中的所有文件都可以在 STATIC_URL 指定的根URL上访问.

Now, all you do is move/point/link the STATIC_ROOT directory so that its accesible from the web. Django expects that all files in STATIC_ROOT are accessible at the root URL that specified by STATIC_URL.

对于用户上传的文件,django更为实用.它真正关心的是您不会将用户上传的文件放在与 collectstatic 命令将用于读取其文件的位置相同的位置-这意味着 MEDIA_ROOT 不能是 STATICFILE_DIRS 的目录或子目录(如果您在此处指定了任何目录,则通常在普通django设置中未定义此设置).

For user uploaded files, django is more hands off. All it really cares about is that you don't put your user uploaded files in the same place as the collectstatic command will be using to read its files - this means, that the MEDIA_ROOT cannot be a directory or subdirectory of STATICFILE_DIRS (if you have specified any directories here, typically this setting is undefined in vanilla django setups).

MEDIA_ROOT 是django可以通过在根目录下创建子目录来操纵的目录(请参见

MEDIA_ROOT is a directory that django can manipulate by creating subdirectories under the root (see the FileField documentation for example).

MEDIA_URL 是指向用户上传文件的根的URL前缀.这样才能为模型生成自动URL的命令正常工作.

MEDIA_URL is the URL prefix that points to the root for user-uploaded files. This is so that commands that generate automatic URLs for models work correctly.

由于这是两种不同类型的静态文件,因此有两种不同类型的安全性和部署要求.例如,您可能希望将用户上传的文件存储在S3存储桶中,但将应用程序的资产放在其他位置.

As these are two different classes of static files, there are two different types of security and deployment requirements. For example, you might want to store user uploaded files in a S3 bucket but put your application's assets somewhere else.

这就是为什么这两个相似的东西在Django中分开的原因.

This is why these two similar things are segregated in django.

这篇关于Django文件夹结构问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆