7

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?

4

2 に答える 2

9

静的ファイルは、特定のアプリに関連するテンプレートがアプリケーション ディレクトリに配置されることが多いのと同じ方法で、関連するアプリに配置できます。

時々、それは理にかなっています。そうでない場合もあります - それはあなた次第です。

たとえば、静的メディアをsite_mediaディレクトリ (グローバル css、グローバル イメージなど) に配置し、アプリ固有のメディアをapp/static. たとえば、Pollアプリを持っている場合、メディアが Poll アプリ テンプレートにのみ必要で、サイト インデックスには必要ない可能性が十分にあります。

テンプレートについても同様です。グローバル テンプレート (base.html) をグローバル テンプレート ディレクトリに配置しましたが、アプリ固有のテンプレートはmyapp/templates/myapp/foo.html.

最後に、これは特にプラグイン可能なアプリにとって理にかなっています。たとえば、django の静的ファイルはアプリに保存されますが、アプリが Python パスのどこかにある場合でも、静的ファイル ディレクトリにアクセスできます。以前は、メディア ディレクトリまたはシンボリック リンクをコピーする必要がありました。

staticfiles アプリは、アプリケーションに関連するすべてのファイルを 1 つの場所 (アプリケーション フォルダー) に整理できるため、非常に優れています。collectstatic残りの処理はすべて 1 か所の Web サーバーで利用できるようにします。

于 2012-02-03T00:03:28.150 に答える
5

実際に各アプリのディレクトリにテンプレートを配置しましたが、それは理にかなっていると思います。他のすべては Python ファイルで行われているため、ロジックの分離はまだ残っています。

ただし、テンプレート ディレクトリをアプリに配置するということは、アプリが他のプロジェクトに役立つことが判明した場合は、アプリを新しいプロジェクトに直接コピーするだけでよいことを意味します。また、ルート テンプレート フォルダーに別のテンプレートを作成することで、これらのテンプレートをいつでもオーバーライドできます。

このため、静的ディレクトリアプリ ディレクトリに格納する必要があります。これにより、特定のアプリに必要なものに基づいてリソースを非常に明確に整理できます。これが、最初に staticfiles アプリが作成された理由です。

于 2012-02-03T14:04:10.513 に答える