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

Jonathan Huppi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jonathan Huppi
Front End Web Development Techdegree Graduate 42,048 Points

Two Scoops of Django Project Layout

I received Two Scoops of Django over the holidays and have been trying to implement Danny and Audrey's ideas into a new project. One of those ideas is restructuring the file tree. Unfortunately they don't go into the nitty gritty details of accomplishing this. They suggest a file tree like this:

repo_folder 
|
- project_folder 
 |
-- config_folder  
  |
--- settings.py
--- urls.py
--- wsgi.py

I really like this idea because it helps create a separation of the project and the apps.

I knew by changing the name of the folder that there would be some file path errors. (This is the part I wish they went into greater depth) I updated manage.py to reflect the renamed folder, but when I run 'python manage.py runserver' I get a ModuleNotFound error. 'No module named {{ project_name }}

So it seems more than manage.py needs to be updated to reflect this change. Has anyone else tried this before?

Kenneth Love I've seen a copy of Two Scoops on your desk in your videos?

1 Answer

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

Hi Jonathan Huppi

Are you referring to this type of setup? Where settings becomes a module?

NOTE: I would not recommend modifying manage.py, as you shouldn't need to.

Example 1

example_project (root)
|   manage.py
|
|---- example_project (project core)
|    |  __init__.py
|    |  urls.py
|    |  wsgi.py
|    |---- settings
|        |   __init__.py
|        | settings.py
|
|----- app1
|    | __init__.py
|    | models.py
|    | tests.py
|    | urls.py
|    | views.py
____________________

and then its become popular to split settings.py into multiple settings files.

Example 2

example_project (root)
|   manage.py
|
|---- example_project 
|    |  __init__.py
|    |  urls.py
|    |  wsgi.py
|    |---- settings
|        |   __init__.py
|        | base.py
|        | dev.py
|        | local.py
|        | production.py
|
|----- app1
|    | __init__.py
|    | models.py
|    | tests.py
|    | urls.py
|    | views.py
____________________

The only places you should have to make modifications should be settings.py and possibly wsgi.py (if you renamed your "project core" directory noted below).

Then you ask, "well how do they get the settings to load?". Well once you have your config folder (settings.py or whatever you name it) as a module. You have an __init__.py that is auto-loaded when django is setting up.

Using Example 2

manage.py

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    # loads setting module in example_project (project core)
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example_project.settings") # <--- at the end there

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

inside the settings modules init.py lives this hidden piece.

init.py

from .dev import *

Which basically says to import all settings from dev.py

dev.py

from .base import * # import all base stuff first

# so we can overwrite below for dev

DEBUG = True

# other Dev only settings

try:
    from .local import * # Import all the local settings, local database? ...
except ImportError:  # or dont if you cant python
    pass
Jonathan Huppi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jonathan Huppi
Front End Web Development Techdegree Graduate 42,048 Points

Thanks for the response, but it's not quite what I'm looking for. I simply want to change the name of the example_project (project core) folder to configuration. I tried making the settings file as a module as you suggested, but I received the same error with the folder named as configuration. If I rename the folder back to example_project(project core) it works fine. The chapter of the book I'm referring to is Chapter 3 specifically sections 3.2 and 3.3 and there is no mention of making the settings file as a module, that sounds more like Chapter 5 to me.

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

So I think I see what you are referring to in Chapter 3.2: Our Preferred Project Layout

Instead of trying to draw this "text tree structure" I cropped a screenshot of my sublime structure out for the example I listed above.

Basically the whole Example 2 is inside a new folder acting as a generic root for the entire project. That way you can place files like requirements.txt and have let the project as a whole be tracked easily by Git.

Two Scoops Structure