【问题标题】:Managing Static Files and Templates Django管理静态文件和模板 Django
【发布时间】:2014-12-10 06:22:45
【问题描述】:

我已经阅读了一些关于在 Django 中创建 Web 应用程序的教程。根据目前为止不同的教程,每个人在存储静态文件和模板的方式上似乎略有不同。

在一个特定的教程中,作者创建了一个与项目文件夹处于同一级别的静态文件夹。在静态文件夹中,他又创建了四个文件夹。它们是:

  1. 媒体
  2. 静态
  3. 仅静态
  4. 模板

在静态中,他创建了:

  1. CSS
  2. js

在模板中,他存储了所有的 web 应用模板,例如 base.html、aboutus.html 等。

然而,在官方 django 文档中,它说

https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-during-development

将您的静态文件存储在应用中名为 static 的文件夹中。例如 my_app/static/my_app/myimage.jpg。

请问最常用的方法是什么?非常感谢!

【问题讨论】:

标签: django django-templates django-staticfiles


【解决方案1】:

这取决于。由于我的应用程序共享同一组静态和模板,因此我将它们存储在根目录中:

# Settings
STATIC_URL = '/static/'
STATIC_ROOT = abspath('static')
STATICFILES_DIRS = (abspath('styles'),)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    ...
)

TEMPLATE_DIRS = (abspath('templates'),)
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

# All styles are in one place
├── styles
│   ├── less
│   ├── js

# App's templates are stored in privite dirs so I can move them in app dir any time
# without changing my code: flatpages/flatpage_detail.html Also it works well with
# class based views.
├── templates
│   ├── base.html
│   ├── flaptages/
│   │   └── flatpage_detail.html
│   ├── another_app/
│   │   └── another_app_detail.html

然后我做manage.py collecstatic 并在root/static 目录中拥有所有静态。 它适合我。但是,如果我要制作一个与社区共享的应用程序,我会将静态和模板放在它自己的目录中,如文档中所述。

【讨论】:

    猜你喜欢
    • 2013-06-18
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 2012-02-25
    • 2020-11-21
    • 2014-10-31
    相关资源
    最近更新 更多