Ticket #16671: tutorial05.17.diff

File tutorial05.17.diff, 19.3 KB (added by Tim Graham, 12 years ago)
  • AUTHORS

    diff --git a/AUTHORS b/AUTHORS
    index 5799b94..6f9b410 100644
    a b answer newbie questions, and generally made Django that much better:  
    380380    Christian Metts
    381381    michal@plovarna.cz
    382382    Slawek Mikula <slawek dot mikula at gmail dot com>
     383    Katie Miller <katie@sub50.com>
    383384    Shawn Milochik <shawn@milochik.com>
    384385    mitakummaa@gmail.com
    385386    Taylor Mitchell <taylor.mitchell@gmail.com>
    answer newbie questions, and generally made Django that much better:  
    510511    Johan C. Stöver <johan@nilling.nl>
    511512    Nowell Strite <http://nowell.strite.org/>
    512513    Thomas Stromberg <tstromberg@google.com>
     514    Ben Sturmfels <ben@sturm.com.au>
    513515    Travis Swicegood <travis@domain51.com>
    514516    Pascal Varet
    515517    SuperJared
  • docs/index.txt

    diff --git a/docs/index.txt b/docs/index.txt
    index 5055edf..a6d9ed2 100644
    a b Are you new to Django or to programming? This is the place to start!  
    4646  :doc:`Part 3 <intro/tutorial03>` |
    4747  :doc:`Part 4 <intro/tutorial04>`
    4848
     49* **Advanced Tutorials:**
     50  :doc:`How to write reusable apps <intro/reusable-apps>`
     51
    4952The model layer
    5053===============
    5154
  • docs/intro/index.txt

    diff --git a/docs/intro/index.txt b/docs/intro/index.txt
    index 19290a5..afb1825 100644
    a b place: read this material to quickly get up and running.  
    66
    77.. toctree::
    88   :maxdepth: 1
    9    
     9
    1010   overview
    1111   install
    1212   tutorial01
    1313   tutorial02
    1414   tutorial03
    1515   tutorial04
     16   reusable-apps
    1617   whatsnext
    17    
     18
    1819.. seealso::
    1920
    2021    If you're new to Python_, you might want to start by getting an idea of what
    2122    the language is like. Django is 100% Python, so if you've got minimal
    2223    comfort with Python you'll probably get a lot more out of Django.
    23    
     24
    2425    If you're new to programming entirely, you might want to start with this
    2526    `list of Python resources for non-programmers`_
    26    
     27
    2728    If you already know a few other languages and want to get up to speed with
    2829    Python quickly, we recommend `Dive Into Python`_ (also available in a
    2930    `dead-tree version`_). If that's not quite your style, there are quite
    3031    a few other `books about Python`_.
    31    
     32
    3233    .. _python: http://python.org/
    3334    .. _list of Python resources for non-programmers: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers
    3435    .. _dive into python: http://diveintopython.net/
    3536    .. _dead-tree version: http://www.amazon.com/exec/obidos/ASIN/1590593561/ref=nosim/jacobian20
    36     .. _books about Python: http://wiki.python.org/moin/PythonBooks
    37  No newline at end of file
     37    .. _books about Python: http://wiki.python.org/moin/PythonBooks
  • new file docs/intro/reusable-apps.txt

    diff --git a/docs/intro/reusable-apps.txt b/docs/intro/reusable-apps.txt
    new file mode 100644
    index 0000000..236345c
    - +  
     1=============================================
     2Advanced tutorial: How to write reusable apps
     3=============================================
     4
     5This advanced tutorial begins where :doc:`Tutorial 4 </intro/tutorial04>` left
     6off. We'll be turning our Web-poll into a standalone Python package you can
     7reuse in new projects and share with other people.
     8
     9If you haven't recently completed Tutorials 1–4, we encourage you to review
     10these 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
     33Reusability matters
     34===================
     35
     36It's a lot of work to design, build, test and maintain a web application. Many
     37Python and Django projects share common problems. Wouldn't it be great if we
     38could save some of this repeated work?
     39
     40Reusability 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
     42range of packages you can use in your own Python programs. Check out `Django
     43Packages <http://www.djangopackages.com>`_ for existing reusable apps you could
     44incorporate in your project. Django itself is also just a Python package. This
     45means that you can take existing Python packages or Django apps and compose
     46them into your own web project. You only need to write the parts that make
     47your project unique.
     48
     49Let's say you were starting a new project that needed a polls app like the one
     50we've been working on. How do you make this app reusable? Luckily, you're well
     51on the way already. In :doc:`Tutorial 3 </intro/tutorial03>`, we saw how we
     52could decouple polls from the project-level URLconf using an ``include``.
     53In this tutorial, we'll take further steps to make the app easy to use in new
     54projects 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
     74Completing your reusable app
     75============================
     76
     77After 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
     94You also have a directory somewhere called ``mytemplates`` which you created in
     95:doc:`Tutorial 2 </intro/tutorial02>`. You specified its location in the
     96TEMPLATE_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
     106The polls app is already a Python package, thanks to the ``polls/__init__.py``
     107file. That's a great start, but we can't just pick up this package and drop it
     108into a new project. The polls templates are currently stored in the
     109project-wide ``mytemplates`` directory. To make the app self-contained, it
     110should also contain the necessary templates.
     111
     112Inside 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
     136Your project-wide templates directory should now look like this::
     137
     138    mytemplates/
     139        admin/
     140            base_site.html
     141
     142Looking good! Now would be a good time to confirm that your polls application
     143still works correctly.  How does Django know how to find the new location of
     144the polls templates even though we didn't modify :setting:`TEMPLATE_DIRS`?
     145Django has a :setting:`TEMPLATE_LOADERS` setting which contains a list
     146of callables that know how to import templates from various sources.  One of
     147the defaults is :class:`django.template.loaders.app_directories.Loader` which
     148looks for a "templates" subdirectory in each of the :setting:`INSTALLED_APPS`.
     149
     150The ``polls`` directory could now be copied into a new Django project and
     151immediately reused. It's not quite ready to be published though. For that, we
     152need 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.  For example, if two
     159   apps had a template called ``base.html``, without the extra directory, it
     160   wouldn't be possible to distinguish between the two. It's a good convention
     161   to use the name of your app for this directory.
     162
     163.. _installing-reuseable-apps-prerequisites:
     164
     165Installing some prerequisites
     166=============================
     167
     168The current state of Python packaging is a `bit muddled`_. For this tutorial,
     169we're going to use distribute_ to build our package. It's an actively
     170maintained fork of the defunct ``setuptools`` project which has some problems
     171which will never be fixed). We'll also be using `pip`_ to uninstall it after
     172we're finished. You should install these two packages now. If you need help,
     173you can refer to :ref:`how to install Django with pip
     174<installing-official-release>`. You can install ``distribute`` the same way.
     175
     176.. _bit muddled: http://guide.python-distribute.org/introduction.html#current-state-of-packaging
     177.. _distribute: http://pypi.python.org/pypi/distribute
     178.. _pip: http://pypi.python.org/pypi/pip
     179
     180Packaging your app
     181==================
     182
     183Python *packaging* refers to preparing your app in a specific format that can
     184be easily installed and used. Django itself is packaged very much like
     185this. For a small app like polls, this process isn't too difficult.
     186
     1871. First, create a parent directory for ``polls``, outside of your Django
     188   project. Call this directory ``django-polls``.
     189
     190.. admonition::  Choosing a name for your app
     191
     192   When choosing a name for your package, check resources like PyPI to avoid
     193   naming conflicts with existing packages. It's often useful to prepend
     194   ``django-`` to your module name when creating a package to distribute.
     195   This helps others looking for Django apps identify your app as Django
     196   specific.
     197
     1982. Move the ``polls`` directory into the ``django-polls`` directory.
     199
     2003. Create a file ``django-polls/README.txt`` with the following contents::
     201
     202       =====
     203       Polls
     204       =====
     205
     206       Polls is a simple Django app to conduct Web-based polls. For each
     207       question, visitors can choose between a fixed number of answers.
     208
     209       Detailed documentation is in the "docs" directory.
     210
     211       Quick start
     212       -----------
     213
     214       1. Add "polls" to your INSTALLED_APPS setting like this::
     215
     216              INSTALLED_APPS = (
     217                  ...
     218                  'polls',
     219              )
     220
     221       2. Include the polls URLconf in your project urls.py like this::
     222
     223              url(r'^polls/', include('polls.urls')),
     224
     225       3. Run `python manage.py syncdb` to create the polls models.
     226
     227       4. Start the development server and visit http://127.0.0.1:8000/admin/
     228          to create a poll (you'll need the Admin app enabled).
     229
     230       5. Visit http://127.0.0.1:8000/polls/ to participate in the poll.
     231
     2324. Create a ``django-polls/LICENSE`` file. Choosing a license is beyond the
     233scope of this tutorial, but suffice it to say that code released publicly
     234without a license is *useless*. Django and many Django-compatible apps are
     235distributed under the BSD license; however, you're free to pick your own
     236license. Just be aware that your licensing choice will affect who is able
     237to use your code.
     238
     2395. Next we'll create a ``setup.py`` file which provides details about how to
     240build and install the app.  A full explanation of this file is beyond the
     241scope of this tutorial, but the `distribute docs
     242<http://packages.python.org/distribute/setuptools.html>`_ have a good explanation.
     243Create a file ``django-polls/setup.py`` with the following contents::
     244
     245    from setuptools import setup, find_packages
     246
     247    setup(
     248        name='django-polls',
     249        version='0.1',
     250        install_requires=['distribute'],
     251        packages=find_packages(),
     252        include_package_data=True,
     253        long_description=open('README.txt').read(),
     254        url='http://www.example.com/',
     255        author='Your Name',
     256        author_email='yourname@example.com',
     257        classifiers=[
     258            'Environment :: Web Environment',
     259            'Framework :: Django',
     260            'Intended Audience :: Developers',
     261            'License :: OSI Approved :: BSD License', # example license
     262            'Operating System :: OS Independent',
     263            'Programming Language :: Python',
     264            'Programming Language :: Python :: 2.6',
     265            'Programming Language :: Python :: 2.7',
     266            'Topic :: Internet :: WWW/HTTP',
     267            'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
     268        ],
     269    )
     270
     271.. admonition:: I thought you said ``setuptools`` was defunct?
     272
     273    Distribute is a drop-in replacement for ``setuptools``. Even though we
     274    appear to import from ``setuptools``, since we have ``distribute``
     275    installed, it will override the import. The ``install_requires`` line
     276    ensure that ``distribute`` will be installed for anyone who is installing
     277    our package.
     278
     2796. Only a limited set of files are included in the package by default. To
     280   include additional files, we'll need to create a ``MANIFEST.in`` file. The
     281   distribute docs referred to in the previous step discuss this file in more
     282   details. To include the templates, create a file
     283   ``django-polls/MANIFEST.in`` with the following contents::
     284
     285       recursive-include polls/templates *
     286
     2877. It's optional, but recommended, to include detailed documentation with your
     288   app. Create an empty directory ``django-polls/docs`` for future
     289   documentation. Add an additional line to ``django-polls/MANIFEST.in``::
     290
     291       recursive-include docs *
     292
     293   Note that the ``docs`` directory won't be included in your package unless
     294   you add some files to it. Many Django apps also provide their documentation
     295   online through sites like `readthedocs.org <http://readthedocs.org>`_.
     296
     2978. Try building your package with ``python setup.py sdist`` (run from inside
     298   ``django-polls``). This creates a directory called ``dist`` and builds your
     299   new package, ``django-polls-0.1.tar.gz``.
     300
     301For more information on packaging, see `The Hitchhiker's Guide to Packaging
     302<http://guide.python-distribute.org/quickstart.html>`_.
     303
     304Using your own package
     305======================
     306
     307Since we moved the ``polls`` directory out of the project, it's no longer
     308working. We'll now fix this by installing our new ``django-polls`` package.
     309
     310.. admonition:: Installing as a system library
     311
     312   The following steps install ``django-polls`` as a system library. In
     313   general, it's best to avoid messing with your system libraries to avoid
     314   breaking things. For this simple example though, the risk is low and it will
     315   help with understanding packaging. We'll explain how to uninstall in
     316   step 4.
     317
     318   For experienced users, a neater way to manage your packages is to use
     319   "virtualenv" (see below).
     320
     3211. Inside ``django-polls/dist``, untar the new package
     322   ``django-polls-0.1.tar.gz`` (e.g. ``tar xzvf django-polls-0.1.tar.gz``). If
     323   you're using Windows, you can download the command-line tool bsdtar_ to do
     324   this, or you can use a GUI-based tool such as 7-zip_.
     325
     3262. Change into the directory created in step 1 (e.g. ``cd django-polls-0.1``).
     327
     3283. If you're using GNU/Linux, Mac OS X or some other flavor of Unix, enter the
     329   command ``sudo python setup.py install`` at the shell prompt.  If you're
     330   using Windows, start up a command shell with administrator privileges and
     331   run the command ``setup.py install``.
     332
     333   With luck, your Django project should now work correctly again. Run the
     334   server again to confirm this.
     335
     3364. To uninstall the package, use pip (you already :ref:`installed it
     337   <installing-reuseable-apps-prerequisites>`, right?)::
     338
     339    sudo pip uninstall django-polls
     340
     341.. _bsdtar: http://gnuwin32.sourceforge.net/packages/bsdtar.htm
     342.. _7-zip: http://www.7-zip.org/
     343.. _pip: http://pypi.python.org/pypi/pip
     344
     345Publishing your app
     346===================
     347
     348Now that we've packaged and tested ``django-polls``, it's ready to share with
     349the world! If this wasn't just an example, you could now:
     350
     351* Email the package to a friend.
     352
     353* Upload the package on your Web site.
     354
     355* Post the package on a public repository, such as `The Python Package Index
     356  (PyPI) <http://guide.python-distribute.org/contributing.html#pypi-info>`_.
     357
     358For more information on PyPI, see the `Quickstart
     359<http://guide.python-distribute.org/quickstart.html#register-your-package-with-the-python-package-index-pypi>`_
     360section of The Hitchhiker's Guide to Packaging. One detail this guide mentions
     361is choosing the license under which your code is distributed.
     362
     363Installing Python packages with virtualenv
     364==========================================
     365
     366Earlier, we installed the polls app as a system library. This has some
     367disadvantages:
     368
     369* Modifying the system libraries can affect other Python software on your
     370  system.
     371
     372* You won't be able to run multiple versions of this package (or others with
     373  the same name).
     374
     375Typically, these situations only arise once you're maintaining several Django
     376projects. When they do, the best solution is to use `virtualenv
     377<http://www.virtualenv.org/>`_. This tool allows you to maintain multiple
     378isolated Python environments, each with its own copy of the libraries and
     379package namespace.
  • docs/intro/tutorial03.txt

    diff --git a/docs/intro/tutorial03.txt b/docs/intro/tutorial03.txt
    index 169e6cd..a6c730a 100644
    a b Load the page in your Web browser, and you should see a bulleted-list  
    315315containing the "What's up" poll from Tutorial 1. The link points to the poll's
    316316detail page.
    317317
     318.. admonition:: Organizing Templates
     319
     320    Rather than one big templates directory, you can also store templates
     321    within each app. We'll discuss this in more detail in the `reuseable apps
     322    tutorial</intro/reuseable-apps>`.
     323
    318324A shortcut: :func:`~django.shortcuts.render`
    319325--------------------------------------------
    320326
  • docs/intro/tutorial04.txt

    diff --git a/docs/intro/tutorial04.txt b/docs/intro/tutorial04.txt
    index 8909caf..dfee827 100644
    a b For full details on generic views, see the :doc:`generic views documentation  
    278278What's next?
    279279============
    280280
    281 The tutorial ends here for the time being. In the meantime, you might want to
    282 check out some pointers on :doc:`where to go from here </intro/whatsnext>`.
     281The beginner tutorial ends here for the time being. In the meantime, you might
     282want to check out some pointers on :doc:`where to go from here
     283</intro/whatsnext>`.
     284
     285If you are familiar with Python packaging and interested in learning how to
     286turn polls into a "reusable app", check out :doc:`Advanced tutorial: How to
     287write reusable apps</intro/reusable-apps>`.
Back to Top