目前在Django中提供静态文件的最佳做法是什么? [英] What is the best practice for serving static files in Django currently

查看:103
本文介绍了目前在Django中提供静态文件的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django 1.x中我已经找到了很多关于如何处理静态文件的建议。有没有一个最佳的做法方法去做这样做?

解决方案

可能有方法在Django中提供静态文件,但Django 1.3引入了一个新的选项来处理它们。基本上,您可以在项目级别或包含静态媒体的应用程序级别定义特定目录。然后通过管理命令collectstatic,您可以将项目目录中的所有静态文件复制到由Web服务器提供的单独目录(可能位于项目外部)。这解决了以前方法的许多并发症。



1)它允许第三方应用程序以标准方式轻松包含静态文件。不再将您的Web服务器目录中的符号链接创建到单独的python / django模块中的位置。



2)它可以更好地控制您的网络服务器托管其静态文件的位置。例如,您可以在版本控制下的项目中定义所有静态媒体,但是将其全部复制到文件系统的任何位置的外部位置。这样就无法将您的网络服务器指向项目中的某个位置。 3)分割静态媒体,永远不会从用户使用FileField上传的静态媒体变化。这是很好的,因为您可能希望将站点级静态媒体保留在版本控制中(要安装在您的开发服务器上),但用户提交的内容将上传到不同的目录(与1.3之前的django版本不同)。



我认为这个功能以前是被合并成核心的第三方模块。



以下是文档:
https://docs.djangoproject.com/en/dev/howto/static-files/



基本上有几个新的settings.py变量:



STATIC_ROOT - 这是您的网络服务器提供的文件系统上的目录。当您运行./manage.py collectstatic时,项目中的所有静态文件及其应用程序将被复制到您在此处指定的目录中。



STATIC_URL - 这是您将设置为代表您的内容将基于什么URL的URL。如果将此值设置为/ static /,则/ static /将会包含所有静态媒体网址。您还将在模板中使用此变量。例如,在您的模板中,您可以指定以下内容:

 
< img src ={{STATIC_URL}} logo.png >

STATIC_ROOT设置变量指定静态文件复制到的位置,但您还需要提供静态文件从哪里复制的位置。这可以通过在您的个人Django应用程序中创建一个名为static //的目录,类似于应用程序中的模板。在运行collectstatic命令时,Django会将所有应用程序中所有static /目录的所有静态文件复制到STATIC_ROOT目录中。您也可以使用STATICFILES_DIRS设置变量来定义项目级别的目录来复制静态媒体。尽管有许多方式可以提供静态媒体,但是Django认为这是一个很好的api,这将有助于更好地集成第三方模块。这个特征现在被包含在核心中的事实可以表明这种方法可能已经获得了一些动力。



希望这有帮助,
Joe


I've found plenty of advice for how to tackle static files in Django 1.x. Is there a best practices way to go about doing so?

解决方案

There are may approaches to serving static files in Django, but Django 1.3 introduced a new option to handle them. Basically, you can define specific directories at a project level or at an app level that contain static media. Then via a management command, 'collectstatic', you can copy all of the static files in your project directory to a separate directory (likely external to your project) that is served by your webserver. This solves a lot of complications of the previous approaches.

1) It allows 3rd party apps to easily include static files in a standard way. No more creating symlinks from your webserver directory to locations inside of individual python/django modules.

2) It gives you more control over where your webserver can host its static files. For example, you can define all of your static media inside of you project under version control, but then copy it all to an external location anywhere on the filesystem. This prevents having to point your webserver to a location inside of your project.

3) Is splits out static media that that will never change from static media that is uploaded by a user using something like a FileField. This is good because you likely want to keep site-level static media in version control (to be installed on your dev servers), but user-submitted content will upload to a different directory (unlike versions of django prior to 1.3).

I think this functionality used to be a 3rd party module that was merged into core.

Here are the docs: https://docs.djangoproject.com/en/dev/howto/static-files/

Basically there are a few new settings.py variables:

STATIC_ROOT - This is a directory on your filesystem that will be served by your webserver. When you run ./manage.py collectstatic, all of the static files from your project and it's apps will be copied into the directory that you specify here.

STATIC_URL - This is the url that you will set to represent what url base your content will be based from. If you set this value to "/static/", then "/static/" will prefix all urls of static media that included. You will also use this variable in your templates. For example, in your template, you could specify something like:

    <img src="{{ STATIC_URL }}logo.png">

The STATIC_ROOT settings variable specifies where static files are copied to, but you also need to provide a location of where static files are copied from. This can be done by creating a directory in your individual Django apps called "static//", similar to what you would do for templates within an app. Upon running the collectstatic command, Django will copy all of the static files from all of the "static/" directories in all of your apps to the STATIC_ROOT directory. You can also use the STATICFILES_DIRS settings variable to define project-level directories to copy static media from.

Though there are many ways to serve static media, I think this is a nice api set in place by Django that will help with better integrating 3rd party modules. The fact that this feature is now included in core could give some indication that this approach may have gained some momentum.

Hope this helps, Joe

这篇关于目前在Django中提供静态文件的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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