Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.4 new project folder structure forces project prefixes?

Tags:

Using django 1.4 and have seen that when you use startproject it now creates a folder within your project with the same name.

-myproject/            manage.py            myproject/                     settings.py                     urls.py 

Documented change here

Previously for my urls I could input

ROOT_URLCONF = 'urls' 

But that no longer works. Am I now supposed to prefix this with project name? i.e.

ROOT_URLCONF = 'myproject.urls' 

-- In my urls.py I'd import settings but now I have to prefix it with from myproject import settings.

I thought prefixing variables with the project name was against django standards as it breaks reuseability?

like image 990
darren Avatar asked Feb 09 '12 14:02

darren


1 Answers

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.

like image 144
hwjp Avatar answered Oct 08 '22 15:10

hwjp