Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Django Templates Templates

Sahar Nasiri
Sahar Nasiri
7,454 Points

Templates directory

Why did Kenneth say that we cannot add templates directory for an app? There is a view inside the inner learning_site folder! Why did we build templates folder for courses but we cannot do the same for the homepage (learning_site)? And what was that DIR thing?

Jeff Muday
Jeff Muday
Treehouse Moderator 28,722 Points

I'm definitely only a beginner at this, but what I understand is the following:

It's all about namespacing. You can do all kinds of crazy things with Django; it is really flexible. But Kenneth's approach is to teach us a standard Django convention because when projects get big and complex, we will avoid naming conflicts.

Suppose I have a project with the structure shown below, three different base.html files.

project1 -> templates -> base.html
          -> appname1 -> templates -> appname1 -> base.html
          -> appname2 -> templates -> appname2 -> base.html

If an app is portable...

Templates in appname1 should extend or use

{% extends  'appname1/base.html' %}

Templates in appname2 should extend by

{% extends  'appname2/base.html' %}

but...

a template in either appname1 or appname2 can still reference or extend the project1 base.html with

{% extends 'base.html' %}

I have used this approach to pull in the "Bootstrap" framework once for the project, yet use in several apps-- the DRY concept. It works fine, but then the templates in appname1 or appname2 will no longer be portable and depend upon the project1 templates.

Then later you will learn about "namespacing" which will allow you to remove direct URL references and replace these with namespaced references.

Sahar Nasiri
Sahar Nasiri
7,454 Points

Jeff Muday, thanks for the answer, but I'm really curious, why did Kenneth say its view doesn't live inside of an app!! Why did he say this? We have view.py!

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,722 Points

Django doesn't automatically create a views.py file in the inner project folder, this was a design choice by the developers suggesting all views should be in apps. But... Kenneth demonstrates the flexibility of Django showing that we can create a view for a "landing page" or "homepage", not in an app folder, but in the inner project folder "learning_site". He creates a file views.py

learning_site/learning_site/views.py

from django.http import HttpResponse
from django.shortcuts import render

def homepage(request):
    # return HttpResponse("This is the learning_site project home page") # original, stub for testing
    return render(request, template_name = 'home.html')

learning_site/learning_site/urls.py

from django.conf.urls import url, include
from django.contrib import admin

from . import views # importing the homepage view

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.homepage, name="homepage"),
    url(r'^courses/', include('courses.urls', namespace="courses")),
]

One could have just as easily implemented a homepage view function in the project's urls.py file, by putting homepage() function above the urlpatterns, but Kenneth is stressing a modular approach to development will keep things straight in our minds as we learn Django.