Perspective Unspoken

My perspective on Git, Docker, Python, Django, PHP and other stuff

Writing a Django reusable library

The tutorial here was quite good at telling how to package your project when you’re done with it and ready to deploy it, but I left wondering, what if I want to just code my project reusable from the get go and keep it versioned just like all the other big names, and indeed Django itself. What’s the best way to do that? After some searching and reading, this is the way I’m going to run with.

Let me mention one obvious thought from the Django philosophy. You typically distribute Django apps, not projects. You’re going to distribute the result of a django-admin startapp command. When you run startproject, that creates a settings.py and a manage.py local to the project, none of which are needed for sharing your app. Typically you want your app to be usable in another Django project that has it’s own settings and includes the app in ways it chooses.

Turns out, it’s pretty straight forward. We just have to create a Django app somewhere. Where we create it actually doesn’t matter. Once the app is created though, we’ll need a Django project to test the app in, i.e. if the app has views and urls and so on.

In order to create a Django app, you just need to say manage.py startapp or django-admin.py startapp. Once you created a Django project you have access to manage.py, once you have Django installed in a virtualenv, you have access to django-admin.py.

Create a folder to store reusable apps

First step then, was to create a space for the purpose of storing all these reusable apps and associate a virtualenv with it. Don’t mind me, I went ahead and created a folder called apps 🙂 for the purpose of storing all these reusable apps.

So, we just created a virtualenv in a folder called apps, and also installed Django in the virtualenv so we have access to django-admin.py.

Create reusable app

Next, we need to create the folder structure for our app. We need a parent folder for the app to hold our setup.py and other files required for packaging our app. I’m going to call my app reusable.

Typically, the outside folder is the name of your app prefixed by “django-“. So I made that folder, came in and just created setup.py, README.rst and LICENSE files. Finally, most importantly… created a Django app using django-admin.py. Make sure you’re still in the environment in order to access django-admin.py.

You can populate your setup.py based on the sample below.

 

Test it out!

For starters you can try building the package using this python command.

If that worked it should have generated a django-reusable-0.1.tar.gz file in the dist folder.

Next, let’s create a sandbox Django project that we can test using our library in.

Next, you can install your package using pip so the Sandbox project reflects changes you make in the reusable app. While in the sandbox environment simply run the following pip command.

Also, open up settings.py for the Sandbox project and add reusable to the list of apps.

Then validate that to ensure that all is fine.

 

 

Push project to Github

Next, you can share your project with the world. Go back inside the django-reusable folder and quickly do the following.

 

And for extras, you can get a descent Pythonic .gitignore file from Github here.

Hope this helps! Happy coding!

 

Djangolibraryreusablevirtualenv

jaywhy13 • March 22, 2015


Previous Post

Next Post

www.000webhost.com