Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep all my django applications in specific folder

I have a Django project, let's say "project1". Typical folder structure for applications is:

/project1/          /app1/          /app2/          ...          __init__.py          manage.py          settings.py          urls.py 

What should I do if I want to hold all of my applications in some separate folder, 'apps' for example? So that structure should look like the following:

/project/          apps/               app1/               app2/               ...          __init__.py          manage.py          settings.py          urls.py 
like image 652
ikostia Avatar asked Oct 16 '10 09:10

ikostia


People also ask

How do I organize my Django apps?

The way I like to organize my Django Project is – Keeps all Django apps in apps folder, static files (scripts, js, CSS) in the static folder, HTML files in templates folder and images and media content in the media folder.

Can a Django project have multiple apps?

Any Django project consists of multiple applications.

How many apps should a Django project have?

Django comes with six built-in apps that we can examine.


2 Answers

You can add your apps folder to your python path by inserting the following in your settings.py:

import os import sys  PROJECT_ROOT = os.path.dirname(__file__) sys.path.insert(0, os.path.join(PROJECT_ROOT, 'apps')) 

Then you can use all the apps in this folder just in the same way as they were in your project root!

like image 102
Bernhard Vallant Avatar answered Oct 11 '22 06:10

Bernhard Vallant


You can do this very easily, but you need to change the settings.py to look like this:

INSTALLED_APPS = (     'apps.app1',     'apps.app2',     # ... ) 

And your urls.py to look like this:

urlpatterns = patterns('',      (r'^app1/',include('apps.app1')),         (r'^app2/',include('apps.app2')),     ) 

.. and modify any imports to point to the app location

like image 25
Gabi Purcaru Avatar answered Oct 11 '22 04:10

Gabi Purcaru