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_...

Vagrant error: * Unknown configuration section 'hostmanager'.

Sometimes you get a vagrant environment or boilerplate with a Vagrantfile config in there and do a vagrant up command. And see some errors. like this: There are errors in the configuration of this machine . Please fix the following errors and try again : Vagrant: * Unknown configuration section 'hostmanager'. To fix this one needs: $ vagrant plugin install vagrant - hostmanager Installing the ' vagrant-hostmanager ' plugin . This can take a few minutes . . . Fetching : vagrant - hostmanager - 1.8 .6 . gem ( 100 % ) Installed the plugin ' vagrant-hostmanager (1.8.6) ' ! So command to fix this as follows: vagrant plugin install vagrant-hostmanager