Moving django apps into subfolder and url.py error

django, django-apps, django-settings

Solution

I don't know why the previous answer got -1 aside from maybe a few redundant lines that can be corrected. Anyway, I found a slightly different method that doesn't involve adding anything to the python path.

This is my final directory structure, I will explain in a moment:

mysite
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── apps
│   ├── __init__.py
│   └── myfirstapp
│       ├── __init__.py
│       ├── admin.py
│       ├── models.py
│       ├── tests.py
│       └── views.py
└── manage.py

No matter if you have just created your project or if you want to move your apps, create the `apps` subdirectory that should contain your apps. The trick is to add an `__init__.py` to that directory.

mkdir apps
touch apps/__init__.py

Now you can move your existing apps into the `apps` subdirectory. If you would like to create a new one instead here are the commands:

python manage.py mysecondapp
mv mysecondapp apps/

Warning: Don't be tempted to call `python manage.py ./apps/mysecondapp`. For some reason this deletes all other apps in that directory. I just lost a day of work this way.

Next, you will need to fix a few imports. Your `settings.py` should be prefixed with `apps`:

INSTALLED_APPS = (
    ...
    'apps.myfirstapp',
    'apps.mysecondapp'
)

Lastly, fix your project's `urls.py` to prefix `apps`:

urlpatterns = patterns('', 
  url(r'^myfirstapp', include('apps.myfirstapp.urls')),
  ...
)

Depending on how you wrote them, you might also have to fix a few imports inside your app. Either just use `from models import MyFirstModel` or also prefix it using `from apps.myfirstapp.models import MyFirstModel`.

In short, if you make your `apps` directory a python package (by adding `__init__.py`), you can use it as part of the import path. This should work regardless of the deployment method with no extra configuration.

Problem

I have a question about putting django apps into "apps" subdirectory. I have the app called “faktura” in a project_root. I didn’t like the fact it lies there and I want to store all my apps under “apps” subdirectory. So, I found out that I could extend python path to “apps” subdir, so after looking in the internet, I added this string to settings.py: sys.path.insert(0, os.path.join(PROJECT_PATH, "apps")). Then I added the app to INSTALLED_APPS like “faktura”. Everything worked smooth until I added url(r'^faktura/', include('faktura.urls')) to urls.py in the root. Since that, Django throws the error message “No module named faktura” full taceback is here: http://dpaste.com/737380/ What can be wrong here, why only urls.py can’t find the app? And does it can’t find this app if I added it to the PATH? I spent a morning trying to figure out what’s wrong and now I need your help.

Original source