Sharing code between two projects

1,297 views
Skip to first unread message

Eugene Morozov

unread,
Jul 4, 2007, 7:34:39 AM7/4/07
to Django users
Hello,
I have two projects, they were developed independently. They will be
run on the same server and have many things in common (notably some
models and templatetags).

While I can import models from another project if it is on PYTHONPATH,
I cannot find a way to share templatetags. I don't want to copy them,
because maintaining costs would become higher (need to remember to
apply bug fixes in both places, for example).

Is there a way to import templatetags from another project?

Django is really great framework, but I have a gut feeling that Django
support for code reuse is significantly worse, than, say, in Plone/
Zope 3. Though it might be because of my relative inexpirience in
Django.

I would be very grateful if someone will point me to pages/blog
entries about how to write Django apps with reusability in mind.
Eugene

Malcolm Tredinnick

unread,
Jul 4, 2007, 7:56:16 AM7/4/07
to django...@googlegroups.com
On Wed, 2007-07-04 at 04:34 -0700, Eugene Morozov wrote:
> Hello,
> I have two projects, they were developed independently. They will be
> run on the same server and have many things in common (notably some
> models and templatetags).
>
> While I can import models from another project if it is on PYTHONPATH,
> I cannot find a way to share templatetags. I don't want to copy them,
> because maintaining costs would become higher (need to remember to
> apply bug fixes in both places, for example).
>
> Is there a way to import templatetags from another project?

Reuse is much easier once you think in terms of applications, rather
than projects. A project is just a collection of applications (really,
it's just a settings file and maybe a root URLConf). So you can easily
share an application in two projects, since applications can be imported
from anywhere on your Python path, as you've noted.

So, if you *only* want to share the template tags and nothing else from
their current application, make a new application that only contains
those template tags. Then you can install that application into both
projects (by including the import path in INSTALLED_APPS in each
project's settings file) and the code will be shared as you would
expect. As mentioned in a thread earlier today on this list, loading a
template tag in a template will cause all the installed apps to be
searched for that template tag file, so you don't need to worry about
keeping the tags right next to the templates they are used in.

> Django is really great framework, but I have a gut feeling that Django
> support for code reuse is significantly worse, than, say, in Plone/
> Zope 3. Though it might be because of my relative inexpirience in
> Django.

It's due to your inexperience, I suspect. Almost everything in Django
operates through normal Python imports, so you can share any code that
is on your Python path. The only real constraints are that template tags
are loaded from a directory called templatetags/ that must live in an
application directory and models must live in a module called "models".
Everything else is free-form and entirely under your control.

Regards,
Malcolm

--
Depression is merely anger without enthusiasm.
http://www.pointy-stick.com/blog/

Ben Ford

unread,
Jul 4, 2007, 8:27:24 AM7/4/07
to django...@googlegroups.com
Hi Eugene,
The way to do this is to make another project (just a module with an __init__.py inside) and then have a templatetags module inside that... Your directory structure looks like this:
top_module/
  -> __init__.py
  -> templatetags/
         -> __init__.py
         -> some_file.py

Put all of your templatetags in some_file.py and include top_module in your installed apps. In your templates you just
    {% load some_file %}
AFAIK that's all you need.
Cheers
Ben
--
Regards,
Ben Ford
ben.f...@gmail.com
+628111880346

Eugene Morozov

unread,
Jul 4, 2007, 8:38:42 AM7/4/07
to Django users
Hello,
Thank you, must have thought about it... Probably need some rest, now
it seems so obvious a solution.
Eugene

Ben Ford

unread,
Jul 4, 2007, 8:44:10 AM7/4/07
to django...@googlegroups.com
Ooops, typo from my earlier message s/project/app/ you want a new application structure as Malcolm said, not a whole project. Sorry :-)

On 04/07/07, Eugene Morozov <eugene....@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages