This article is part 10 of 14 in the series Python Django Tutorial

Last Updated: Thursday 12th December 2013

Save Time by Writing and Using Reusable Python Django Apps

It's not trivial to design, develop and maintain a web application. Lots of features and aspects have to be handled properly in order for a web application to succeed. To name a few, the features that are common to almost every web application are user management, third-party oauth sign in / sign up and admin site. Since so many common problems have to be solved in any web application over and over again, it makes sense to make them reusable components / packages so that a new web application can simply leverage the existing code to save time during development.

Fortunately, the Python Package Index (pypi) provides numerous packages you can use in your own application. More specifically, the Django Packages list all the reusables Django apps you can integrate into your own project. Finding and using a proper Django package is often better and more time-efficient than writing one yourself.

In this article, we are going to learn how to make our current myblog Django application become a reusable Django package so that you or someone else can use it in his or her own project.

Package and App

Before we start, we should clarify a critical point about packages and apps. A Python package is a logical group of Python code that's easy to re-use. A package often contains multiple Python files that are called modules.

Usually, we use a module or a package by importing it like import myblog.views or from myblog import views. To make a Python directory such as myblog become a package, we put a special file __init__.py into it even if the directory is empty.

A Django app is just a Python package designed to be used inside a Django project. Usually, a Django app follows common Django conventions such as including models.py, urls.py and views.py.

The term packaging means wrapping the Django app into a deployable Python package so that others can easily integrate it into their own project.

Extract the App Code

After our previous tutorial, the current structure of our application myblog should look like this:

First, let's create a parent directory for myblog, outside of the root myblog directory. Let's call it django-myblog:

Second, let's move the myblog directory into django-myblog:

Third, create a file django-myblog/README.rst with the following content:

=====
Myblog
=====

Myblog is a simple demo of Django's basic usage.

Quick start
-----------

1. Add "myblog" to INSTALLED_APPS:
  INSTALLED_APPS = {
    ...
    'myblog'
  }

2. Include the myblog URLconf in urls.py:
  url(r'^myblog/', include('myblog.urls'))

3. Run `python manage.py syncdb` to create myblog's models.

4. Run the development server and access http://127.0.0.1:8000/admin/ to
    manage blog posts.

5. Access http://127.0.0.1:8000/myblog/ to view a list of most recent posts.

Fourth, create a license file django-myblog/LICENSE for your reusable app. Usually Django apps are distributed under the BSD license but you're free to choose any one.

Fifth, create django-myblog/setup.py to specify instruction about how to install the app which is used by Distribute.

Sixth, create django-myblog/MANIFEST.in to include the text files and static files in our package:

include LICENSE
include README.rst
recursive-include myblog/static *
recursive-include myblog/templates *
recursive-include docs *

Notice we also included a directory docs in MANIFEST.in. This directory will contain documentation for our reusable app in the future. For now, let's create an empty directory django-myblog/docs.

Finally, we build our Python package:

Use django-myblog in a New Django Web Application

Suppose you are going to start a new Django project and it's going to use the functionality in myblog. You can simply reuse django-myblog we just built in your new project.

First, let's create a new Django project:

Second, let's modify mysite/settings.py:

Third, let's modify mysite/urls.py to put myblog's URLconf under /blog:

Fourth, we run `python manage.py syncdb` to create models for myblog and run `python manage.py runserver` to start the server:

Finally, we can access http://127.0.0.1:8000/blog/ to take a look at our blog's home page:

Django Blog Screenshot

Summary and Tips

In this article, we learned how to package our myblog app into a reusable component and how to use it in a new django project. It's always a good idea to write reusable Django apps since you can reuse the same code in new projects, thus saving a significant amount of time. Since Python Package Index (pypi) and Django Packages provide comprehensive lists of reusable Python and Django apps, you should check them out before starting any new project.

About The Author