| 1 | ============================================= |
| 2 | Advanced tutorial: How to write reusable apps |
| 3 | ============================================= |
| 4 | |
| 5 | This advanced tutorial begins where :doc:`Tutorial 4 </intro/tutorial04>` left |
| 6 | off. We'll be turning our Web-poll into a standalone Python package you can |
| 7 | reuse in new projects and share with other people. |
| 8 | |
| 9 | If you haven't recently completed Tutorials 1–4, we encourage you to review |
| 10 | these so that your example project matches the one described below. |
| 11 | |
| 12 | .. Outline: |
| 13 | |
| 14 | .. * motivation |
| 15 | .. * what is a python package? |
| 16 | .. * what is a django app? |
| 17 | .. * what is a reusable app? |
| 18 | |
| 19 | .. * preparing |
| 20 | .. * moving templates into your app |
| 21 | .. * parent directory |
| 22 | .. * adding package boilerplate |
| 23 | .. * link to packaging docs |
| 24 | .. * package builder |
| 25 | |
| 26 | .. * using the package |
| 27 | .. * how to install |
| 28 | |
| 29 | .. * publishing |
| 30 | .. * options for publishing |
| 31 | .. * link to docs on PyPI |
| 32 | |
| 33 | Reusability matters |
| 34 | =================== |
| 35 | |
| 36 | It's a lot of work to design, build, test and maintain a web application. Many |
| 37 | Python and Django projects share common problems. Wouldn't it be great if we |
| 38 | could save some of this repeated work? |
| 39 | |
| 40 | Reusability is the way of life in Python. `The Python Package Index (PyPI) |
| 41 | <http://guide.python-distribute.org/contributing.html#pypi-info>`_ has a vast |
| 42 | range of packages you can use in your own Python programs. Check out `Django |
| 43 | Packages <http://www.djangopackages.com>`_ for existing reusable apps you could |
| 44 | incorporate in your project. Django itself is also just a Python package. This |
| 45 | means that you can take existing Python packages or Django apps and compose |
| 46 | them into your own web project. You only need to write the parts that make |
| 47 | your project unique. |
| 48 | |
| 49 | Let's say you were starting a new project that needed a polls app like the one |
| 50 | we've been working on. How do you make this app reusable? Luckily, you're well |
| 51 | on the way already. In :doc:`Tutorial 3 </intro/tutorial03>`, we saw how we |
| 52 | could decouple polls from the project-level URLconf using an ``include``. |
| 53 | In this tutorial, we'll take further steps to make the app easy to use in new |
| 54 | projects and ready to publish for others to install and use. |
| 55 | |
| 56 | .. admonition:: Package? App? |
| 57 | |
| 58 | A Python `package <http://docs.python.org/tutorial/modules.html#packages>`_ |
| 59 | provides a way of grouping related Python code for easy reuse. A package |
| 60 | contains one or more files of Python code (also known as "modules"). |
| 61 | |
| 62 | A package can be imported with ``import foo.bar`` or ``from foo import |
| 63 | bar``. For a directory (like ``polls``) to form a package, it must contain |
| 64 | a special file ``__init__.py``, even if this file is empty. |
| 65 | |
| 66 | A Django *app* is just a Python package that is specifically intended for |
| 67 | use in a Django project. An app may also use common Django conventions, |
| 68 | such as having a ``models.py`` file. |
| 69 | |
| 70 | Later on we use the term *packaging* to describe the process of making a |
| 71 | Python package easy for others to install. It can be a little confusing, we |
| 72 | know. |
| 73 | |
| 74 | Completing your reusable app |
| 75 | ============================ |
| 76 | |
| 77 | After the previous tutorials, our project should look like this:: |
| 78 | |
| 79 | mysite/ |
| 80 | manage.py |
| 81 | mysite/ |
| 82 | __init__.py |
| 83 | settings.py |
| 84 | urls.py |
| 85 | wsgi.py |
| 86 | polls/ |
| 87 | admin.py |
| 88 | __init__.py |
| 89 | models.py |
| 90 | tests.py |
| 91 | urls.py |
| 92 | views.py |
| 93 | |
| 94 | You also have a directory somewhere called ``mytemplates`` which you created in |
| 95 | :doc:`Tutorial 2 </intro/tutorial02>`. You specified its location in the |
| 96 | TEMPLATE_DIRS setting. This directory should look like this:: |
| 97 | |
| 98 | mytemplates/ |
| 99 | admin/ |
| 100 | base_site.html |
| 101 | polls/ |
| 102 | detail.html |
| 103 | index.html |
| 104 | results.html |
| 105 | |
| 106 | The polls app is already a Python package, thanks to the ``polls/__init__.py`` |
| 107 | file. That's a great start, but we can't just pick up this package and drop it |
| 108 | into a new project. The polls templates are currently stored in the |
| 109 | project-wide ``mytemplates`` directory. To make the app self-contained, it |
| 110 | should also contain the necessary templates. |
| 111 | |
| 112 | Inside the ``polls`` app, create a new ``templates`` directory. Now move the |
| 113 | ``polls`` template directory from ``mytemplates`` into the new |
| 114 | ``templates``. Your project should now look like this:: |
| 115 | |
| 116 | mysite/ |
| 117 | manage.py |
| 118 | mysite/ |
| 119 | __init__.py |
| 120 | settings.py |
| 121 | urls.py |
| 122 | wsgi.py |
| 123 | polls/ |
| 124 | admin.py |
| 125 | __init__.py |
| 126 | models.py |
| 127 | templates/ |
| 128 | polls/ |
| 129 | detail.html |
| 130 | index.html |
| 131 | results.html |
| 132 | tests.py |
| 133 | urls.py |
| 134 | views.py |
| 135 | |
| 136 | Your project-wide templates directory should now look like this:: |
| 137 | |
| 138 | mytemplates/ |
| 139 | admin/ |
| 140 | base_site.html |
| 141 | |
| 142 | Looking good! Now would be a good time to confirm that your polls application |
| 143 | still works correctly. How does Django know how to find the new location of |
| 144 | the polls templates even though we didn't modify :setting:`TEMPLATE_DIRS`? |
| 145 | Django has a :setting:`TEMPLATE_LOADERS` setting which contains a list |
| 146 | of callables that know how to import templates from various sources. One of |
| 147 | the defaults is :class:`django.template.loaders.app_directories.Loader` which |
| 148 | looks for a "templates" subdirectory in each of the :setting:`INSTALLED_APPS`. |
| 149 | |
| 150 | The ``polls`` directory could now be copied into a new Django project and |
| 151 | immediately reused. It's not quite ready to be published though. For that, we |
| 152 | need to package the app to make it easy for others to install. |
| 153 | |
| 154 | .. admonition:: Why nested? |
| 155 | |
| 156 | Why create a ``polls`` directory under ``templates`` when we're |
| 157 | already inside the polls app? This directory is needed to avoid conflicts in |
| 158 | Django's ``app_directories`` template loader. It's a good convention to use |
| 159 | the name of your app for this directory. |
| 160 | |
| 161 | Packaging your app |
| 162 | ================== |
| 163 | |
| 164 | Python *packaging* refers to preparing your app in a specific format that can |
| 165 | be easily installed and used. Django itself is packaged very much like |
| 166 | this. For a small app like polls, this process isn't too difficult. |
| 167 | |
| 168 | 1. First, create a parent directory for ``polls``, outside of your Django |
| 169 | project. Call this directory ``django-polls``. |
| 170 | |
| 171 | .. admonition:: Choosing a name for your app |
| 172 | |
| 173 | When choosing a name for your package, check resources like PyPI to avoid |
| 174 | naming conflicts with existing packages. It's often useful to prepend |
| 175 | ``django-`` to your module name when creating a package to distribute. |
| 176 | This helps others looking for Django apps identify your app as Django |
| 177 | specific. |
| 178 | |
| 179 | 2. Move the ``polls`` directory into the ``django-polls`` directory. |
| 180 | |
| 181 | 3. Create a file ``django-polls/README.txt`` with the following contents:: |
| 182 | |
| 183 | ===== |
| 184 | Polls |
| 185 | ===== |
| 186 | |
| 187 | Polls is a simple Django app to conduct Web-based polls. For each |
| 188 | question, visitors can choose between a fixed number of answers. |
| 189 | |
| 190 | Detailed documentation is in the "docs" directory. |
| 191 | |
| 192 | Quick start |
| 193 | ----------- |
| 194 | |
| 195 | 1. Add "polls" to your INSTALLED_APPS setting like this:: |
| 196 | |
| 197 | INSTALLED_APPS = ( |
| 198 | ... |
| 199 | 'polls', |
| 200 | ) |
| 201 | |
| 202 | 2. Include the polls URLconf in your project urls.py like this:: |
| 203 | |
| 204 | url(r'^polls/', include('polls.urls')), |
| 205 | |
| 206 | 3. Run `python manage.py syncdb` to create the polls models. |
| 207 | |
| 208 | 4. Start the development server and visit http://127.0.0.1:8000/admin/ |
| 209 | to create a poll (you'll need the Admin app enabled). |
| 210 | |
| 211 | 5. Visit http://127.0.0.1:8000/polls/ to participate in the poll. |
| 212 | |
| 213 | 5. Next we'll create a ``setup.py`` file which provides details about how to |
| 214 | build and install the app. A full explanation of this file is beyond the |
| 215 | scope of this tutorial, but the `official Python docs |
| 216 | <http://docs.python.org/distutils/setupscript.html>`_ have a good explanation. |
| 217 | Create a file ``django-polls/setup.py`` with the following contents:: |
| 218 | |
| 219 | from distutils.core import setup |
| 220 | |
| 221 | setup( |
| 222 | name='django-polls', |
| 223 | version='0.1', |
| 224 | packages=['polls'], |
| 225 | package_data={'polls': ['templates/polls/*']}, |
| 226 | license='', |
| 227 | long_description=open('README.txt').read(), |
| 228 | url='http://www.example.com/', |
| 229 | author='Your Name', |
| 230 | author_email='yourname@example.com', |
| 231 | ) |
| 232 | |
| 233 | 6. Only a limited set of files are included in the package by default. To |
| 234 | include additional files, we'll need to create a ``MANIFEST.in`` file. Once |
| 235 | again, we won't go into all the details here, but `the official Python docs |
| 236 | <http://docs.python.org/distutils/sourcedist.html#manifest-template>`_ have |
| 237 | details. To include the templates, create a file |
| 238 | ``django-polls/MANIFEST.in`` with the following contents:: |
| 239 | |
| 240 | recursive-include polls/templates * |
| 241 | |
| 242 | 7. It's optional, but recommended, to include detailed documentation with your |
| 243 | app. Create an empty directory ``django-polls/docs`` for future |
| 244 | documentation. Add an additional line to ``django-polls/MANIFEST.in``:: |
| 245 | |
| 246 | recursive-include docs * |
| 247 | |
| 248 | Note that the ``docs`` directory won't be included in your package unless |
| 249 | you add some files to it. Many Django apps also provide their documentation |
| 250 | online through sites like `readthedocs.org <http://readthedocs.org>`_. |
| 251 | |
| 252 | 8. Try building your package with ``python setup.py sdist`` (run from inside |
| 253 | ``django-polls``). This creates a directory called ``dist`` and builds your |
| 254 | new package, ``django-polls-0.1.tar.gz``. |
| 255 | |
| 256 | For more information on packaging, see `The Hitchhiker's Guide to Packaging |
| 257 | <http://guide.python-distribute.org/quickstart.html>`_. |
| 258 | |
| 259 | Using your own package |
| 260 | ====================== |
| 261 | |
| 262 | Since we moved the ``polls`` directory out of the project, it's no longer |
| 263 | working. We'll now fix this by installing our new ``django-polls`` package. |
| 264 | |
| 265 | .. admonition:: Installing as a system library |
| 266 | |
| 267 | The following steps install ``django-polls`` as a system library. In |
| 268 | general, it's best to avoid messing with your system libraries to avoid |
| 269 | breaking things. For this simple example though, the risk is low and it will |
| 270 | help with understanding packaging. We'll explain how to uninstall in |
| 271 | step 4. |
| 272 | |
| 273 | For experienced users, a neater way to manage your packages is to use |
| 274 | "virtualenv" (see below). |
| 275 | |
| 276 | 1. Inside ``django-polls/dist``, untar the new package |
| 277 | ``django-polls-0.1.tar.gz`` (e.g. ``tar xzvf django-polls-0.1.tar.gz``). If |
| 278 | you're using Windows, you can download the command-line tool bsdtar_ to do |
| 279 | this, or you can use a GUI-based tool such as 7-zip_. |
| 280 | |
| 281 | 2. Change into the directory created in step 1 (e.g. ``cd django-polls-0.1``). |
| 282 | |
| 283 | 3. If you're using GNU/Linux, Mac OS X or some other flavor of Unix, enter the |
| 284 | command ``sudo python setup.py install`` at the shell prompt. If you're |
| 285 | using Windows, start up a command shell with administrator privileges and |
| 286 | run the command ``setup.py install``. |
| 287 | |
| 288 | With luck, your Django project should now work correctly again. Run the |
| 289 | server again to confirm this. |
| 290 | |
| 291 | 4. To uninstall the package, delete the polls-related directories and files |
| 292 | from your system libraries. To find them, run the following at your Python |
| 293 | prompt. |
| 294 | |
| 295 | >>> import polls |
| 296 | >>> polls |
| 297 | <module 'polls' from '/usr/local/lib/python2.7/dist-packages/polls/__init__.pyc'> |
| 298 | |
| 299 | In this case, we would find the polls-related files and directories in |
| 300 | ``/usr/local/lib/python2.7/dist-packages/``. Your location may be different. |
| 301 | |
| 302 | .. _bsdtar: http://gnuwin32.sourceforge.net/packages/bsdtar.htm |
| 303 | .. _7-zip: http://www.7-zip.org/ |
| 304 | |
| 305 | Publishing your app |
| 306 | =================== |
| 307 | |
| 308 | Now that we've packaged and tested ``django-polls``, it's ready to share with |
| 309 | the world! If this wasn't just an example, you could now: |
| 310 | |
| 311 | * Email the package to a friend. |
| 312 | |
| 313 | * Upload the package on your Web site. |
| 314 | |
| 315 | * Post the package on a public repository, such as `The Python Package Index |
| 316 | (PyPI) <http://guide.python-distribute.org/contributing.html#pypi-info>`_. |
| 317 | |
| 318 | For more information on PyPI, see the `Quickstart |
| 319 | <http://guide.python-distribute.org/quickstart.html#register-your-package-with-the-python-package-index-pypi>`_ |
| 320 | section of The Hitchhiker's Guide to Packaging. One detail this guide mentions |
| 321 | is choosing the license under which your code is distributed. |
| 322 | |
| 323 | Installing Python packages with virtualenv |
| 324 | ========================================== |
| 325 | |
| 326 | Earlier, we installed the polls app as a system library. This has some |
| 327 | disadvantages: |
| 328 | |
| 329 | * Modifying the system libraries can affect other Python software on your |
| 330 | system. |
| 331 | |
| 332 | * You won't be able to run multiple versions of this package (or others with |
| 333 | the same name). |
| 334 | |
| 335 | Typically, these situations only arise once you're maintaining several Django |
| 336 | projects. When they do, the best solution is to use `virtualenv |
| 337 | <http://www.virtualenv.org/>`_. This tool allows you to maintain multiple |
| 338 | isolated Python environments, each with its own copy of the libraries and |
| 339 | package namespace. |
| 340 | |
| 341 | More about reusable apps |
| 342 | ======================== |
| 343 | |
| 344 | For more on writing reusable apps, see `What is a reusable app? |
| 345 | <http://ericholscher.com/projects/django-conventions/app/>`_ by Eric |
| 346 | Holsher. For a collection of reusable apps to use in your own projects, see |
| 347 | the `Pinax <http://pinaxproject.com/>`_ project. |