How To Structure Template Libraries In A Django Project?
Solution 1:
For site projects I tend to make the project module itself (as created by startproject
) an app too.
That way base templates, base models (such as a custom user model!), helper utility functions, etc. live in there, and one doesn't have to futz around with the filesystem template loaders at all - the default app-based template loader works fine.
That is, if my project is, say, grocerystore
and it has two apps, drinks
and foods
(silly example but bear with me), the structure would be approximately
manage.py
grocerystore/
__init__.py
settings/
__init__.py
models/
__init__.py
user.py
views/
__init__.py
templates/
base.html
foods/
__init__.py
apps.py
models/
__init__.py
food.py
views/
__init__.py
food/
__init__.py
food_list_view.py
__init__.py
templates/
foods/
food_list.html
drinks/
__init__.py
apps.py
models/
__init__.py
drink.py
# ... etc ...
and INSTALLED_APPS
would contain (along Django defaults) ('grocerystore', 'foods', 'drinks')
.
You can then simply {% extends "base.html" %}
in your apps.
(Note, btw, that to be able to split models.py
into a package, you have to make sure models/__init__.py
imports the modules holding each model, so they're registered by Django!)
Solution 2:
I have worked for a year on Django. I think the best way to have templates in a separate folder. It means having a template in each app folder. It will be good to have a template folder and in this, we can have our app. You have to define template folder in the settings file. Plase check my folder structure
Post a Comment for "How To Structure Template Libraries In A Django Project?"