0

I have gone through a few tutorials on creating a web application in Django. Based on the different tutorials so far, everyone seems to have a slightly different ways of storing their static files and templates.

In one particular tutorial, the author created a static folder at the same level as the project folder. Within the static folder, he created four more folders.They are :

  1. media
  2. static
  3. static-only
  4. templates

Within static, he created:

  1. css
  2. js

within templates, he stores all the web app templates such as base.html, aboutus.html, etc.

However, in the official django documentation, it says

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

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

May I ask which is the most common way of doing it? Many thanks!

codemax
  • 1,322
  • 10
  • 19
  • I follow this blog: http://www.hasnath.net/blog/serving-static-files-css-js-images-in-django-the-best-way – ruddra Oct 15 '14 at 09:27

1 Answers1

1

It depends. Since my apps share the same set of static and templates I store them in root dirs:

# 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

Then I do manage.py collecstatic and have all static in root/static dir. It fits me. But if I would make an app to share with community I will put static and templates in it's own dir as described in docs.

byashimov
  • 454
  • 2
  • 6