24π
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.
3π
Use BASE_DIR
variable from the settings.py
. It should be already defined:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Better not use the __file__
attribute in manage.py
and wsgi.py
, as they are located in different directories.
So, just add the following to manage.py
and wsgi.py
(and to the celery.py
if you use Celery):
from django.conf import settings
sys.path.append(os.path.join(settings.BASE_DIR, "apps"))
You will end up with the following project structure:
project
βββ project
β βββ __init__.py
β βββ celery.py
β βββ settings.py
β βββ urls.py
β βββ wsgi.py
βββ apps
β βββ app1
β βββ app2
βββ manage.py
- Get_readonly_fields in a TabularInline class in Django?
- Django create new user without password
- How to make Django Password Reset Email Beautiful HTML?
1π
To keep your Django applications in a subfolder (such as apps/), first add the following to your settings.py:
import os
PROJECT_ROOT = os.path.dirname(__file__)
Then in manage.py:
Right under #!/usr/bin/env python add:
import sys
from os.path import abspath, dirname, join
from site import addsitedir
Right before if __name__ == β__main__β : add:
sys.path.insert(0, join(settings.PROJECT_ROOT, "apps"))
0π
@Radu Gheorghiuβs answer; It is not necessary to edit settings.py, and the insert path line can be condensed to 1 line of code.
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "apps"))
I sourced this answer from http://obroll.com/nested-application-inside-apps-sub-folder-in-django-1-3/
- Django Override Admin change_form.html Template β display associated model in template
- Django testing tips