我已经完成了一些关于在Django中创建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。
请问哪种方法最常用?非常感谢!
答案 0 :(得分: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
目录中拥有所有静态内容。
它适合我。但是,如果我要创建一个与社区分享的应用程序,我会将静态和模板放在它自己的目录中,如文档中所述。