Skip to main content

Django: Better way to install django apps into apps. (Handling django projects dependencies.)

Let's talk about common programming practice. You love to code yourself. Everyday you write tons of code. Making lots of common tasks is interesting and challenging. No?
A-ha! You love plugins too! :) I'm making a startup. Everyday there are some ideas coming to my head. Some are easy to implement, some are not, but I often download tons of plugins.
There are lots of ways to use plugins without installing them to main system. Python virtualenv is easy and common to use for this purposes. Soon I'll write an article about it too. But the main problem of Python Virtualenv is it's complexity. You basically need to install something, make an environment and so on... What if you just need to implement nice and easy "Django way".
Here direct copy comes in mind. It's the easiest way, as for me.
Basic concept is:
   - download plugin (be it in egg, zip, tar.gz or any other format)
   - unpack it 
   - copy to django app dir (simple drag and copy files to eclipse in my case)
   - settings.py modification (and/or any App post setup procedures)
Thats ALL!

Lets go into this way more deeply. Lets install django-pagination into my app.
First wee need to check plugin manual. Let's download it from google code HERE.
Unpack It. If you've done it in a period near to writing this article you'll probably see something like this.

We see a list of folders, containing one, like "docs" and a bunch of setup scripts like "setup.py", "setup.cfg" and so on. "Docs/install.txt" says about different install methods.
But you don't have to install it in order things to work properly!
The simplest way is to copy this main app "pagination" (it is usually a first dir in a tree containing "__init__.py" file) into your project or app dir, containing "settings.py".  This way you can see apps or plugins, like they where installed on a python path. 
Setup script usually does this for you, but installing this directory to your system (or virtualenv) python path. And what if you'd like any other version of this django app installed? 
There some other thoughts here also. You may create dir, like "plugins" and copy installed plugins there. But i would not recommend this. You usually need up to 10 plugins per project. Why bother separating them? Bad side here is that we need to do some inside plugin modifications because you're renaming main plugin directory path. So it's possible, but unlikely simpler...

Now we must do plugin setup inside our App. You unfortunately can not escape this. :)
So let's make steps 1 and 2 from "docs/usage.txt" in our plugin dir. Let's add 'pagination', app to INSTALLED_APPS in our settings.py and add a middleware 'pagination.middleware.PaginationMiddleware', string to our MIDDLEWARE_CLASSES.

Let's add request to our TEMPLATE_CONTEXT_PROCESSORS too (if it isn't already there).
It will look something like so:

And thats pretty much all. Now we can add {% load pagination_tags %} somewhere in our template and use  {% autopaginate object_list %} and then {% paginate %} father. (see "docs/usage.txt" of this app for more info)

Now your app is really installed. You can use common from my_app import my_function like in "standard" installation way. Nothing more to do. 

We worked out most common situation with django plugins or redistributable apps. Maybe to some complex python apps, like lets say PIL, this method would be unsuitable, BUT for most common "django app installation" cases it will work like a charm.
Main plus of this way, that you can have all code related to you App/project etc. stored in one place.
This code would not affect main system also. So if you decide to use older version of plugin (for older ver of django for e.g.), maybe you'll remember yourself reading this article :)...

Bad thing here, that if your app has some more dependencies, you have to install them somehow too. Maybe you have to change your new app code, if you choose to copy them to your main dir way. But it's in not so common "complex" cases.

Comments? Suggestions? Please drop me a comment about what you think here...

Comments

  1. How is this simpler than just using pip/virtualenv?

    Any sane developer should have those installed anyway which means the process for adding an app is:
    1. Activate your virtualenv
    2. Add the app name to requirements.txt
    3. pip install -r requirements.txts

    ReplyDelete
  2. Well, virtualenv is cool. But key thing here that you must install something in your system (Virtualenv in this case). But I don't use virtualenv in my Eclipse projects. It's possible of course.
    Anyway.
    I'm having all code installed in one place. It's IMHO major advantage. When I'll have system crash I don't have to remember what to recover. (Backup of only one Developer dir for e.g.)

    I can have many versions of plugins installed without virtualenv. (It's not the best thing, because virtualenv is cool :) see higher). But I'll need it for my deployment in near future.
    I had a number of conflicts, while installing OSQA.

    You can access and change plugin code however you like. And you can easily access it in Eclipse...

    BTW I'll publish an article about virtualenv very soon. I'm actually writing it right now. So check back later on. It's the first method I tried in my "programmers life"...

    ReplyDelete
  3. Virtualenv is pretty nice, and you can use it with virtualenvwrapper that is amazing!!

    ReplyDelete

Post a Comment

Popular posts from this blog

Pretty git Log

SO you dislike git log output in console like me and do not use it... Because it looks like so: How about this one? It's quite easy... Just type: git log - - graph - - pretty = format : '%Cred%h%Creset -%C ( yellow ) %d%Creset %s %Cgreen ( %cr) %C ( bold blue ) <%an>%Creset' - - abbrev - commit - - It may be hard to enter such an easy command every time. Let's make an alias instead... Copypaste this to your terminal: git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --" And use simple command to see this pretty log instead: git lg Now in case you want to see lines that changed use: git lg - p In order for this command to work remove  the -- from the end of the alias. May the code be with you! NOTE: this article is a rewritten copy of  http://coderwall.com/p/euwpig?i=3&p=1&t=git   and have b

Django: Resetting Passwords (with internal tools)

I have had a task recently. It was about adding a forms/mechanism for resetting a password in our Django based project. We have had our own registration system ongoing... It's a corporate sector project. So you can not go and register yourself. Admins (probably via LDAP sync) will register your email/login in system. So you have to go there and only set yourself a password. For security reasons you can not register. One word. First I've tried to find standart decision. From reviewed by me were: django-registration and django password-reset . These are nice tools to install and give it a go. But I've needed a more complex decision. And the idea was that own bicycle is always better. So I've thought of django admin and that it has all the things you need to do this yourself in no time. (Actually it's django.contrib.auth part of django, but used out of the box in Admin UI) You can find views you need for this in there. they are: password_reset password_reset_

Time Capsule for $25

The real article name might be something like:  Configuring Raspbery Pi to serve like a Time Capsule with Netatalk 3.0 for Mountain Lion.  But it's too long ;) Here I will describe the process of using Raspberry Pi like a Time Machine in my network. To be able to backup your MAC's remotely (Like it would be NAS of some kind). It assumes you have a Raspberry Pi and have installed a Raspbian there and have a ssh connection, or somehow having access to it's console. Refer to my previous article for details . Now that we have a Pi that is ready for action let's animate it. So to make it suit you as a Time Capsule (NAS) for your MAC's you need to do those basic steps: - connect and configure USB hard drive(s) - install support of HFS+ filesystem to be able to use MAC's native filesystem - make mount (auto-mount on boot) of your hard drive - install Avahi and Netatalk demons - configure Netatalk daemon to make it all serve as a Time Machine - configure