[Django]-Django 1.4 new project folder structure forces project prefixes?

16👍

Yes, prefix ROOT_URLCONF with your project name:

ROOT_URLCONF = 'myproject.urls'

You shouldn’t import settings directly anyway (see Using settings in Python code). Instead, use the following, which works for both the old and new project layouts.

from django.conf import settings

54👍

I would just add that it forces you to use prefixes when you accces your main myproject.urls, but it doesn’t force you either way for your apps. You can choose to store apps either in the top-level folder:

myproject
|-- manage.py
|-- myproject
|   |-- __init__.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
`-- polls
    |-- __init__.py
    |-- models.py
    |-- tests.py
    `-- views.py

This is the default when you use python manage.py startapp polls
In this case you’d use from polls.models import Whatever

Alternatively, you can:

mkdir myproject/polls
python manage.py startapp polls myproject/polls

And you’ll get this:

myproject
|-- manage.py
`-- myproject
    |-- __init__.py
    |-- polls
    |   |-- __init__.py
    |   |-- models.py
    |   |-- tests.py
    |   `-- views.py
    |-- settings.py
    |-- urls.py
    `-- wsgi.py

In which case you’ll have to from myproject.polls.models import Whatever

So the former is better for apps you think you might be able to re-use in other projects, and the latter is better for apps that are tightly coupled with other parts of your project.

👤hwjp

Leave a comment