2
votes

I know there's a lot of questions regarding best practices of how to structure an entire Django project but i cannot find a concrete explanation on how to structure templates for a Django project.

Obviously, I've just started learning Python/Django and i'm quite confused why most of the tutorials/examples i found, the directory name under templates directory is always the same as the applications directory name.

For example:

gazette/
    __init__.py
    models.py
    views.py
    static/
        ...
    templates/
        gazette/
            __base.html
            __l_single_col.html
            __l_right_sidebar.html
            __l_left_sidebar.html
            _article_full.html
            _article_summary.html
            _author_list.html
            _author_name.html
            _category_list.html
            _navigation.html
            _tag_list.html
            article_detail.html
            article_list.html
            author_list.html
            category_list.html
            tag_list.html

this actually came from a blog: https://oncampus.oberlin.edu/webteam/2012/09/architecture-django-templates

In the application structure above, the application gazette/ have templates/ directory under it. In turn, under templates/, there's a single directory called gazette/, which is the same name as the application directory where it's contained. Under this folder are the actual html template files.

Question: Why can't we just directly put all the html template files under templates/ directory? I would understand the logic of the structure if there are several directories under templates folder. However, i never found a single example from any tutorials in YouTube or blogs that shows a project structure where there are several directories under templates directory. Instead, they have to name their one and only directory (under templates) same as the the application directory.

If you could explain the principle behind it and direct me to any reference/link, i would greatly appreciate it.

Thanks in advance

2

2 Answers

1
votes

Your Django Project Template at the end of the day is a personal choice, yes there are known templates that optimize your workflow for most projects, but at the end of the day every project is different, for example this is the template i use when i work for someone who expect me to follow the guidelines

Project
    app1
    app2
    ..etc
    static
        app1
            images
            css
            js
        app2
            ...etc
    templates
        app1
            file.html
            ...etc
        app2
        ..etc
    log_files
        app1_info.log
        app2_info.log
        app1_erros.log
        ..etc

this is because the Django philosophy is based on separation, Django is built around encouraging modularity

when i'm working on personal projects, i like to mix everything because even if use different apps for different things, i'll still have the same style and same templates across most of them.

1
votes

One alternative for storing templates is to store each app's templates inside the templates directory of the app.

So the project structure would look something like this

 Project  
     app1  
         templates  
             app1  
                 template1.html  
                 template2.html  
     app2 
         templates  
             app2  
                 template1.html

This way you can render the template from the view by passing 'app1/templates1.html'