How do I create project independent apps.

11 views
Skip to first unread message

Johan

unread,
Oct 5, 2009, 9:32:42 AM10/5/09
to Django users
Good day

I want to develop an application in django which can be used in
multiple django sites/projects. The one approach is to create one
project (mysite) and then to create the application in the site
project (myapp). The application is then added to the project via the
settings file with INSTALLED_APPS = ('mysite.myapp'). Now I create the
econd project (mysite2). I want to use exactly the same app in my new
site. The one approach is to copy the subdirectory from the first
project to the second, and the to add the application to the project
via the settings file( INSTALLED_APPS = ('mysite2.myapp')). The
second is to create the application in a common directory somewhere
NOT in the directory tree of either the projects. The issue is how do
I within the django framework set the path so that the application can
be added to the project settings file with something like
INSTALLED_APPS = ('applications.myapp').
If I add the directory of my application/s root to the TEMPLATE_DIRS
variable in the settings file I can do the above (I'm asuming this is
a side effect of the framework updating the sys library path)), but it
seems to be incorrect since my usage has nothing to do with templates.

Any help/guidance would be appreciated.

Tahnks

Johan

unread,
Oct 5, 2009, 9:45:23 AM10/5/09
to Django users
Actually the TEMPLATE_DIRS does nothing. In order to get my apps to
work outside of the project directory I edited the manage.py file and
added :

import sys
sys.path.append('..\\..\\..\\django-apps\\trunk')

This allows me to reference my applications from a central site
(django-apps\\trunk) ... Is this the correct approach ?

Thanks

Nan

unread,
Oct 5, 2009, 10:33:26 AM10/5/09
to Django users
You can move your apps outside your project to somewhere on your
python path. So for instance, move the directory from some-path/
mysite/myapp to some-path/myapp . Then install it as "myapp" instead
of "mysite.myapp" and change your imports to "from myapp.models" (etc)
instead of "from mysite.myapp.models".

Kevin Teague

unread,
Oct 5, 2009, 4:15:12 PM10/5/09
to Django users


On Oct 5, 6:45 am, Johan <djjord...@gmail.com> wrote:
> Actually the TEMPLATE_DIRS does nothing. In order to get my apps to
> work outside of the project directory I edited the manage.py file and
> added :
>
> import sys
> sys.path.append('..\\..\\..\\django-apps\\trunk')
>
> This allows me to reference my applications from a central site
> (django-apps\\trunk) ... Is this the correct approach ?
>

If scripts depend upon other Python projects you can either:

- install project globally. which I do not recommended.

- add project location to your PYTHONPATH. Lots of ways to manage
PATHs, but I like to do this by managing a 'profile.sh' file in the
root of your django site project.

- manage sys.path explicitly from within your scripts. Which means
hard-coding in your scripts, so you can run into issues with multiple
devs/instances where paths vary. However, tools such as Buildout, can
automate script generation, so you check the project out of your VCS,
run the install tool, and the hard-coded bits are auto-generated for
your install. This gives you a robust deployment, since if you later
fiddle with your environment, you can still run your code.

As for the creation of 'mysite.myapp' this pattern is introduced in
the Django tutorial. Pretty much every practice regarding installation
and project management in that tutorial is either outdated or geared
for making things as easy-as-possible. Unfortunately it really paints
you in corner and teaches a lot of bad habits. Once you want to move
beyond the simple use cases you need to take a big step back and re-
think and re-do how you're going to manage your django-based projects
and their dependencies.

Andy Mikhailenko

unread,
Oct 6, 2009, 4:36:41 AM10/6/09
to Django users
Another way is to use a settings wrapper such as django-harness[1],
which:
* helps organize apps (by name, without the notion of project) and
eliminates the need to write absolute paths for templates, sqlite
database and such stuff stored in the project directory;
* simplifies version control of project code;
* enables to extract applications to separate repositories without
changing any import statements anywhere.
The developer only needs to replace "settings" with "settings_wrapper"
in default manage.py script.

By "project" I mean the directory with manage.py and settings.py while
it may contain no apps. I think it's a good practice to only keep an
application in that directory if it is a copy of some app which code
is not publicly available or which is an experimental development
related just to this project. Anyway it must never be imported as
projectname.appname.

[1] http://pypi.python.org/pypi/django-harness

Johan

unread,
Oct 6, 2009, 4:45:14 AM10/6/09
to Django users
Thanks for all the feedback. By nature I prefer NOT to use a to deep
framework stack. So although django-harness could work I would prefer
to keep my dependencies simple. I would probably go with managing the
python path. It seems to be the simplest way and most transparent way
of handling the situation.

Torsten Bronger

unread,
Oct 6, 2009, 5:26:59 AM10/6/09
to django...@googlegroups.com
Hallöchen!

Johan writes:

I think so, too.

I did some research on this two weeks ago and my impression was that
there are no guidelines at all to create portable apps. On the
contrary, the template path in the official Django tutorial is
example.

My own guidelines are inspired from the Pinax project which seems to
be quite far in this direction.

Templates: Have them in myproject/myapp/templates/myapp/, including
the base.html. base.html extends site_base.html which resides in
myproject/myapp/templates/ but is usually overridden in
myproject/templates/. site_base.html should contain the blocks
title, css, extrahead, and content. Additionally, there should be
"title" in the context.

Static files: Have them in myproject/myapp/media/myapp/ and use
django-staticfiles for collecting them in myproject/media/. I set
STATIC_ROOT (which is used by django-staticfiles) simply to
MEDIA_ROOT because I don't think a distinction is really
necessary.

(About the additional /myapp/ in the paths: This makes it easy for
the site admin to override some or all files.)

Settings: There is no really good solution for it. Django-Harness
seems to have found one, but I think clearly documenting them in
your app's documentation should be enough.

And of course, middleware, context managers, and URLs should be in
the app directory, too. Furthermore, an app should not have
login/logout ware, and if so, it should at least be easily
overridable in the global urls.py.

Use relative imports within the application's modules. They are
available since Python 2.5. This way, I avoided any tampering with
sys.path.

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: torsten...@jabber.rwth-aachen.de
or http://bronger-jmp.appspot.com

Torsten Bronger

unread,
Oct 6, 2009, 7:44:02 AM10/6/09
to django...@googlegroups.com
Hallöchen!

Torsten Bronger writes:

> [...]


>
> I did some research on this two weeks ago and my impression was
> that there are no guidelines at all to create portable apps. On
> the contrary, the template path in the official Django tutorial is

... not a good example.

Reply all
Reply to author
Forward
0 new messages