| 1 | ===================================== |
| 2 | Writing your first Django app, part 5 |
| 3 | ===================================== |
| 4 | |
| 5 | This tutorial begins where :doc:`Tutorial 4 </intro/tutorial04>` left |
| 6 | off. We'll be turning our Web-poll into a stand-alone Python package you can |
| 7 | reuse and share. |
| 8 | |
| 9 | .. Outline: |
| 10 | |
| 11 | .. * motivation |
| 12 | .. * what is a python package? |
| 13 | .. * what is a django app? |
| 14 | .. * what is a reusable app? |
| 15 | |
| 16 | .. * preparing |
| 17 | .. * moving templates into your app |
| 18 | .. * parent directory |
| 19 | .. * adding package boilerplate |
| 20 | .. * link to packaging docs |
| 21 | .. * package builder |
| 22 | |
| 23 | .. * using the package |
| 24 | .. * how to install |
| 25 | |
| 26 | .. * publishing |
| 27 | .. * options for publishing |
| 28 | .. * link to docs on PyPI |
| 29 | |
| 30 | Python packages and reusable apps |
| 31 | ================================== |
| 32 | |
| 33 | Django really is just Python. This means that you can make use of existing |
| 34 | Python packages and Django apps in your project. |
| 35 | |
| 36 | The concept of piecing together projects out of *reusable apps* is one of |
| 37 | Django's most powerful features. Doing so, you avoid solving the same problems |
| 38 | over again. The only parts you need to write are those that make your project |
| 39 | unique. |
| 40 | |
| 41 | .. note:: |
| 42 | |
| 43 | A Python `package <http://docs.python.org/tutorial/modules.html#packages>`_ |
| 44 | is a way of grouping related Python code for easy reuse. A package contains |
| 45 | one or more files of Python code (also known as "modules"). |
| 46 | |
| 47 | A package can be imported with ``import foo.bah`` or ``from foo import |
| 48 | bar``. For a directory (like ``polls``) to be a package, it must contain a |
| 49 | special file ``__init__.py``, even if the file is empty. |
| 50 | |
| 51 | A Django *app* is just a Python package that is specifically intended for |
| 52 | use in a Django project. An app may also use common Django conventions, |
| 53 | such as having a ``models.py`` file. |
| 54 | |
| 55 | Later on we use the term *packaging* to describe the process of making a |
| 56 | Python package easy for others to install. It can be little confusing, we |
| 57 | know. |
| 58 | |
| 59 | Let's say you had written a Polls app and wanted to use it in a brand new |
| 60 | project. How do you make it easily reusable? It just so happens that you're |
| 61 | well on the way already. |
| 62 | |
| 63 | In the previous tutorials, we began making Polls reusable by decoupling it from |
| 64 | the project-level URLconf. In this tutorial, we'll take further steps to make |
| 65 | the app easily to use in new projects and ready to publish for others to |
| 66 | install and use. |
| 67 | |
| 68 | Completing your reusable app |
| 69 | ============================ |
| 70 | |
| 71 | The Polls app we've been working on is already a Python package (thanks to the |
| 72 | ``__init__.py`` inside ``polls``). That's a good start. |
| 73 | |
| 74 | We can't just pick up this package and drop it into a new project yet |
| 75 | though. The Polls templates are currently stored in the project-wide |
| 76 | ``templates`` directory. To make the app self-contained, we need to store these |
| 77 | templates within the ``polls`` directory. |
| 78 | |
| 79 | Inside ``mysite/polls``, create the directory ``templates`` and *inside* that, |
| 80 | the directory ``polls``. Move the ``index.html`` and ``detail.html`` templates |
| 81 | into ``templates/polls``. This allows the app's templates to be distributed |
| 82 | with the app. Confirm that your poll Web site still works correctly. |
| 83 | |
| 84 | Your project should now look like this:: |
| 85 | |
| 86 | mysite/ |
| 87 | polls/ |
| 88 | admin.py |
| 89 | templates/ |
| 90 | polls/ |
| 91 | detail.html |
| 92 | index.html |
| 93 | __init__.py |
| 94 | models.py |
| 95 | tests.py |
| 96 | urls.py |
| 97 | views.py |
| 98 | __init__.py |
| 99 | manage.py |
| 100 | settings.py |
| 101 | urls.py |
| 102 | |
| 103 | .. NOTE:: Why create a ``polls`` directory under templates when we're already |
| 104 | inside the Polls app? This directory is needed to avoid conflicts because of |
| 105 | how Django's ``app_directories`` template loader works. |
| 106 | |
| 107 | You now have a package that could be copied into new Django projects and |
| 108 | immediately reused. It's not yet ready to be published though. For that, we |
| 109 | need to package the app to make it easy for others to install. |
| 110 | |
| 111 | Packaging your app |
| 112 | ================== |
| 113 | |
| 114 | Python packaging isn't as hard as it sounds, especially for such a small app. |
| 115 | |
| 116 | 1. Firstly, create a parent directory for ``polls``, outside of your Django |
| 117 | project. Call this directory ``django-polls``. |
| 118 | |
| 119 | 2. Move the ``polls`` directory into the ``django-polls`` directory. |
| 120 | |
| 121 | 3. Create a file ``django-polls/README.txt`` with the following contents:: |
| 122 | |
| 123 | ===== |
| 124 | Polls |
| 125 | ===== |
| 126 | |
| 127 | Polls is a simple Django app to conduct Web-based polls. For each |
| 128 | question, visitors can choose between a fixed number of answers. |
| 129 | |
| 130 | Detailed documentation is in the "docs" directory. |
| 131 | |
| 132 | Quick start |
| 133 | ----------- |
| 134 | |
| 135 | 1. Add "polls" to your INSTALLED_APPS setting like this:: |
| 136 | |
| 137 | INSTALLED_APPS = ( |
| 138 | ... |
| 139 | 'polls', |
| 140 | ) |
| 141 | |
| 142 | 2. Include the polls URLconf in your project urls.py like this:: |
| 143 | |
| 144 | (r'^polls/', include('polls.urls')), |
| 145 | |
| 146 | 3. Run `python manage.py syncdb` to create the polls models. |
| 147 | |
| 148 | 4. Start the development server and visit http://127.0.0.1:8000/admin/ |
| 149 | to create a poll (you'll need the Admin app enabled). |
| 150 | |
| 151 | 5. Visit http://127.0.0.1:8000/polls/ to participate in the poll. |
| 152 | |
| 153 | |
| 154 | 5. Create a file ``django-polls/setup.py`` with the following contents:: |
| 155 | |
| 156 | from distutils.core import setup |
| 157 | |
| 158 | setup( |
| 159 | name='django-polls', |
| 160 | version='0.1', |
| 161 | packages=['polls',], |
| 162 | license='', |
| 163 | long_description=open('README.txt').read(), |
| 164 | url='http://www.example.com/', |
| 165 | author='Your Name', |
| 166 | author_email='yourname@example.com', |
| 167 | ) |
| 168 | |
| 169 | 6. It's optional, but recommended, to include detailed documentation with |
| 170 | your app. Create an empty directory ``django-polls/docs`` for future |
| 171 | documentation. Create a file ``django-polls/MANIFEST.in`` with the following |
| 172 | contents:: |
| 173 | |
| 174 | recursive-include docs * |
| 175 | |
| 176 | This instruction ensures that the ``docs`` directory is included in the |
| 177 | package. |
| 178 | |
| 179 | 7. Try building your package with ``python setup.py sdist`` (run from inside |
| 180 | ``django-polls``. This creates a directory called ``dist`` and builds your |
| 181 | new package, ``django-polls-0.1.tar.gz``. |
| 182 | |
| 183 | For more information on packaging, see `The Hitchhiker's Guide to Packaging |
| 184 | <http://guide.python-distribute.org/quickstart.html>`_. |
| 185 | |
| 186 | Using your own package |
| 187 | ====================== |
| 188 | |
| 189 | Since we moved the ``polls`` directory out of the project, it's no longer |
| 190 | working. We'll now fix this by installing our new ``django-polls`` package. |
| 191 | |
| 192 | .. NOTE:: The following steps install ``django-polls`` as a system library. In |
| 193 | general, it's best to avoid messing with your system libraries to avoid |
| 194 | breaking things. For this simple example though, the risk is low and it will |
| 195 | help with understanding packaging. We'll explain how to uninstall in |
| 196 | step 4. |
| 197 | |
| 198 | For experienced users, a neater way to manage your packages is to use |
| 199 | "virtualenv" (see below). |
| 200 | |
| 201 | 1. Inside ``django-polls/dist``, untar the new package |
| 202 | ``django-polls-0.1.tar.gz`` (e.g. ``tar xzvf django-polls-0.1.tar.gz``). If |
| 203 | you're using Windows, you can download the command-line tool bsdtar_ to do |
| 204 | this, or you can use a GUI-based tool such as 7-zip_. |
| 205 | |
| 206 | 2. Change into the directory created in step 1 (e.g. ``cd django-polls``). |
| 207 | |
| 208 | 3. If you're using GNU/Linux, Mac OS X or some other flavor of Unix, enter the |
| 209 | command ``sudo python setup.py install`` at the shell prompt. If you're |
| 210 | using Windows, start up a command shell with administrator privileges and |
| 211 | run the command ``setup.py install``. |
| 212 | |
| 213 | With luck, your Django project should now work correctly again. Run the |
| 214 | server again to confirm this. |
| 215 | |
| 216 | 4. To uninstall the package, delete the directory ``polls`` and the file |
| 217 | ``django_polls-0.1.egg-info`` from your system libraries. On GNU/Linux and |
| 218 | Mac OS these files live in ``/usr/local/lib/python2.X/dist-packages``, and |
| 219 | on Windows, in ``C:\Python2X\Lib\site-packages`` (where *X* is the Python |
| 220 | minor version number). You will need administrator privileges. |
| 221 | |
| 222 | .. _bsdtar: http://gnuwin32.sourceforge.net/packages/bsdtar.htm |
| 223 | .. _7-zip: http://www.7-zip.org/ |
| 224 | |
| 225 | Publishing your app |
| 226 | =================== |
| 227 | |
| 228 | Now that we've packaged and tested ``django-polls``, it's ready to share with |
| 229 | the world! If this wasn't just an example, you could now: |
| 230 | |
| 231 | * Email the package to a friend. |
| 232 | |
| 233 | * Upload the package on your Web site. |
| 234 | |
| 235 | * Post the package on a public repository, such as `The Python Package |
| 236 | Index (PyPI) |
| 237 | <http://guide.python-distribute.org/contributing.html#pypi-info>`_. |
| 238 | |
| 239 | For more information on PyPI, see the `Quickstart |
| 240 | <http://guide.python-distribute.org/quickstart.html#register-your-package-with-the-python-package-index-pypi>`_ |
| 241 | section of The Hitchhiker's Guide to Packaging. One detail this guide mentions |
| 242 | is choosing the license under which your code is distributed. |
| 243 | |
| 244 | Installing Python packages with virtualenv |
| 245 | ========================================== |
| 246 | |
| 247 | Earlier, we installed the Polls app as a system library. This has some |
| 248 | disadvantages: |
| 249 | |
| 250 | * Modifying the system libraries can affect other Python software on your |
| 251 | system. |
| 252 | |
| 253 | * You won't be able to run multiple version of this package (or others with |
| 254 | the same name). |
| 255 | |
| 256 | Typically, these situations only arise once you're maintaining several Django |
| 257 | projects. When they do, the best solution is to use `virtualenv |
| 258 | <http://www.virtualenv.org/>`_. This tool allows you to maintain multiple |
| 259 | isolated Python environments, each with its own copy of the libraries and |
| 260 | package namespace. |
| 261 | |
| 262 | Further information |
| 263 | =================== |
| 264 | |
| 265 | For more on writing reusable apps, see `What is a reusable app? |
| 266 | <http://ericholscher.com/projects/django-conventions/app/>`_ by Eric |
| 267 | Holsher. For a collection of reusable apps to use in your own projects, see the |
| 268 | `Pinax <http://pinaxproject.com/>`_ project. |
| 269 | |
| 270 | The tutorial ends here for the time being. In the meantime, you might want to |
| 271 | check out some pointers on :doc:`where to go from here </intro/whatsnext>`. |