Ticket #16671: tutorial05.16.diff

File tutorial05.16.diff, 17.1 KB (added by Tim Graham, 9 years ago)

Updates based on feedback


    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..29210f0 100644
    a b Are you new to Django or to programming? This is the place to start! 
    4444  :doc:`Part 1 <intro/tutorial01>` |
    4545  :doc:`Part 2 <intro/tutorial02>` |
    4646  :doc:`Part 3 <intro/tutorial03>` |
    47   :doc:`Part 4 <intro/tutorial04>`
     47  :doc:`Part 4 <intro/tutorial04>` |
     49* **Advanced Tutorials:**
     50  :doc:`How to write reusable apps <intro/reusable_apps>`
    4952The model layer
  • docs/intro/index.txt

    diff --git a/docs/intro/index.txt b/docs/intro/index.txt
    index 19290a5..284fea0 100644
    a b place: read this material to quickly get up and running. 
    77.. toctree::
    88   :maxdepth: 1
    1010   overview
    1111   install
    1212   tutorial01
    1313   tutorial02
    1414   tutorial03
    1515   tutorial04
     16   reusable_apps
    1617   whatsnext
    1819.. seealso::
    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.
    2425    If you're new to programming entirely, you might want to start with this
    2526    `list of Python resources for non-programmers`_
    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`_.
    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..39d0c54
    - +  
     2Advanced tutorial: How to write reusable apps
     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.
     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.
     12.. Outline:
     14.. * motivation
     15..     * what is a python package?
     16..     * what is a django app?
     17..     * what is a reusable app?
     19.. * preparing
     20..     * moving templates into your app
     21..     * parent directory
     22..     * adding package boilerplate
     23..     * link to packaging docs
     24..     * package builder
     26.. * using the package
     27..     * how to install
     29.. * publishing
     30..     * options for publishing
     31..     * link to docs on PyPI
     33Reusability matters
     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?
     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.
     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.
     56.. admonition:: Package? App?
     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").
     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.
     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.
     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.
     74Completing your reusable app
     77After the previous tutorials, our project should look like this::
     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
     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::
     98    mytemplates/
     99        admin/
     100            base_site.html
     101        polls/
     102            detail.html
     103            index.html
     104            results.html
     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.
     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::
     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
     136Your project-wide templates directory should now look like this::
     138    mytemplates/
     139        admin/
     140            base_site.html
     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`.
     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.
     154.. admonition:: Why nested?
     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.
     161Packaging your app
     164Python *packaging* refers to preparing your app in a specific format that can
     165be easily installed and used. Django itself is packaged very much like
     166this. For a small app like polls, this process isn't too difficult.
     1681. First, create a parent directory for ``polls``, outside of your Django
     169   project. Call this directory ``django-polls``.
     171.. admonition::  Choosing a name for your app
     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.
     1792. Move the ``polls`` directory into the ``django-polls`` directory.
     1813. Create a file ``django-polls/README.txt`` with the following contents::
     183       =====
     184       Polls
     185       =====
     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.
     190       Detailed documentation is in the "docs" directory.
     192       Quick start
     193       -----------
     195       1. Add "polls" to your INSTALLED_APPS setting like this::
     197              INSTALLED_APPS = (
     198                  ...
     199                  'polls',
     200              )
     202       2. Include the polls URLconf in your project urls.py like this::
     204              url(r'^polls/', include('polls.urls')),
     206       3. Run `python manage.py syncdb` to create the polls models.
     208       4. Start the development server and visit
     209          to create a poll (you'll need the Admin app enabled).
     211       5. Visit to participate in the poll.
     2135. Next we'll create a ``setup.py`` file which provides details about how to
     214build and install the app.  A full explanation of this file is beyond the
     215scope of this tutorial, but the `official Python docs
     216<http://docs.python.org/distutils/setupscript.html>`_ have a good explanation.
     217Create a file ``django-polls/setup.py`` with the following contents::
     219    from distutils.core import setup
     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    )
     2336. 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::
     240       recursive-include polls/templates *
     2427. 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``::
     246       recursive-include docs *
     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>`_.
     2528. 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``.
     256For more information on packaging, see `The Hitchhiker's Guide to Packaging
     259Using your own package
     262Since we moved the ``polls`` directory out of the project, it's no longer
     263working. We'll now fix this by installing our new ``django-polls`` package.
     265.. admonition:: Installing as a system library
     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.
     273   For experienced users, a neater way to manage your packages is to use
     274   "virtualenv" (see below).
     2761. 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_.
     2812. Change into the directory created in step 1 (e.g. ``cd django-polls-0.1``).
     2833. 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``.
     288   With luck, your Django project should now work correctly again. Run the
     289   server again to confirm this.
     2914. 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.
     295   >>> import polls
     296   >>> polls
     297   <module 'polls' from '/usr/local/lib/python2.7/dist-packages/polls/__init__.pyc'>
     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.
     302.. _bsdtar: http://gnuwin32.sourceforge.net/packages/bsdtar.htm
     303.. _7-zip: http://www.7-zip.org/
     305Publishing your app
     308Now that we've packaged and tested ``django-polls``, it's ready to share with
     309the world! If this wasn't just an example, you could now:
     311* Email the package to a friend.
     313* Upload the package on your Web site.
     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>`_.
     318For more information on PyPI, see the `Quickstart
     320section of The Hitchhiker's Guide to Packaging. One detail this guide mentions
     321is choosing the license under which your code is distributed.
     323Installing Python packages with virtualenv
     326Earlier, we installed the polls app as a system library. This has some
     329* Modifying the system libraries can affect other Python software on your
     330  system.
     332* You won't be able to run multiple versions of this package (or others with
     333  the same name).
     335Typically, these situations only arise once you're maintaining several Django
     336projects. When they do, the best solution is to use `virtualenv
     337<http://www.virtualenv.org/>`_. This tool allows you to maintain multiple
     338isolated Python environments, each with its own copy of the libraries and
     339package namespace.
     341More about reusable apps
     344For more on writing reusable apps, see `What is a reusable app?
     345<http://ericholscher.com/projects/django-conventions/app/>`_ by Eric
     346Holsher. For a collection of reusable apps to use in your own projects, see
     347the `Pinax <http://pinaxproject.com/>`_ project.
  • docs/intro/tutorial04.txt

    diff --git a/docs/intro/tutorial04.txt b/docs/intro/tutorial04.txt
    index 8909caf..6e18df1 100644
    a b For full details on generic views, see the :doc:`generic views documentation 
    278278What's next?
    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
     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