Django模板添加图片、CSS、JavaScript等静态文件

https://docs.djangoproject.com/en/3.1/howto/static-files/ 

Managing static files (e.g. images, JavaScript, CSS)

Websites generally need to serve additional files such as images, JavaScript, or CSS. In Django, we refer to these files as “static files”. Django provides django.contrib.staticfiles to help you manage them.

网页通常需要提供额外的文件比如图像,JavaScript,或CSS。在Django中,我们称这些文件为“静态文件”。Django提供了django.contrib.staticfiles来帮助你管理它们。

This page describes how you can serve these static files.

本页描述了如何提供这些静态文件。

Configuring static files

1、Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.

确保”django.contrib.staticfiles”被包含在你的myproj/settings.py的INSTALLED_APPS设置项。

INSTALLED_APPS = [

'blogapp.apps.BlogappConfig', #20200922 test values

'testapp.apps.TestappConfig', #20200922 test aggregation

'myapp.apps.MyappConfig', #20200911

'django.contrib.admin',

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.messages',

'django.contrib.staticfiles',

]

 

2、In your settings file, define STATIC_URL, for example:

在你的myproj/settings.py文件中,定义STATIC_URL,例如:

STATIC_URL = '/static/' #表示在应用程序目录的static子目录作为静态文件搜索路径

STATIC_URL = '/static/'

3、In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE.

在你的模板中,使用static模板标签、使用已配置的STATICFILES_STORAGE来为给定的相对路径建立URL

#~~~~~~~

{% load static %}

<img src="{% static "my_app/example.jpg" %}" alt="My image">

#~~~~~~~

{% load static %}

<img src={% static "myapp/波尔多葡萄酒.jpg" %} alt="logo">

4、Store your static files in a folder called static in your app. For example my_app/static/my_app/example.jpg.

在你的app的名为static的文件夹中存储你的静态文件。例如:myapp/static/myapp/example.jpg:

 

提供文件Serving the files

 

In addition to these configuration steps, you’ll also need to actually serve the static files.

    除了这些配置步骤,你也需要实际提供静态文件。

During development, if you use django.contrib.staticfiles, this will be done automatically by runserver when DEBUG is set to True (see django.contrib.staticfiles.views.serve()).

    在开发中,如果你使用django.contrib.staticfiles,当DEBUG设置为True时通过runserver这将被自动完成(参见django.contrib.staticfiles.views.serve())。

This method is grossly inefficient and probably insecure, so it is unsuitable for production.

    该方法效率极低并且可能不安全,因此不适合生产。

See Deploying static files for proper strategies to serve static files in production environments.

    有关在生产环境中提供静态文件的适当步骤参见部署静态文件Deploying static files https://docs.djangoproject.com/en/3.1/howto/static-files/deployment/ 。

 

Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files. For example:

你的项目也可能具有与特定app无关的静态资产。除了在你的应用程序目录中使用static/目录,你可以在你的settings.py文件中定义一个目录列表(STATICFILES_DIRS),Django也将在那里查找静态文件。例如:

#~~~~~~

STATICFILES_DIRS = [

    BASE_DIR / "static", #项目根目录poll0907/static/

    '/var/www/static/', #绝对路径

]

#~~~~~~

See the documentation for the STATICFILES_FINDERS setting for details on how staticfiles finds your files.

关于staticfiles如何找到你的文件的细节参见STATICFILES_FINDERS设置项的文档。

静态文件命名空间Static file namespacing

 

Now we might be able to get away with putting our static files directly in my_app/static/ (rather than creating another my_app subdirectory), but it would actually be a bad idea. Django will use the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the best way to ensure this is by namespacing them. That is, by putting those static files inside another directory named for the application itself.

    现在我们也许可以侥幸放置我们的静态文件直接到myapp/static/(而不是创建另外一个myapp子目录,即myapp/static/myapp/),但这其实是个坏主意。Django将使用它找到的第一个名字匹配的静态文件,如果你在不同应用程序中有相同名字的静态文件,Django将不能区分它们。我们需要能够为Django指名正确的那个,确保这一点的最好的方式就是对它们进行名字空间管理。也就是,将这些静态文件放进应用程序命名的另一个子目录中。

You can namespace static assets in STATICFILES_DIRS by specifying prefixes.

    你可以在STATICFILES_DIRS中通过指定前缀对静态资产添加命名空间。

 

Serving static files during development

 

Serving files uploaded by a user during development

Testing

Deployment

Learn more

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值