Fixed #19706 - Tweaks to the tutorial.

Thanks Daniele Procida.
This commit is contained in:
Tim Graham 2013-02-07 05:51:25 -05:00
parent 43efefae69
commit aa85ccf8ce
5 changed files with 116 additions and 98 deletions

View file

@ -50,8 +50,8 @@ projects and ready to publish for others to install and use.
Python package easy for others to install. It can be a little confusing, we
know.
Completing your reusable app
============================
Your project and your reusable app
==================================
After the previous tutorials, our project should look like this::
@ -67,78 +67,28 @@ After the previous tutorials, our project should look like this::
admin.py
models.py
tests.py
urls.py
views.py
You also have a directory somewhere called ``mytemplates`` which you created in
:doc:`Tutorial 2 </intro/tutorial02>`. You specified its location in the
TEMPLATE_DIRS setting. This directory should look like this::
mytemplates/
admin/
base_site.html
polls/
detail.html
index.html
results.html
The polls app is already a Python package, thanks to the ``polls/__init__.py``
file. That's a great start, but we can't just pick up this package and drop it
into a new project. The polls templates are currently stored in the
project-wide ``mytemplates`` directory. To make the app self-contained, it
should also contain the necessary templates.
Inside the ``polls`` app, create a new ``templates`` directory. Now move the
``polls`` template directory from ``mytemplates`` into the new
``templates``. Your project should now look like this::
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
polls/
admin.py
__init__.py
models.py
templates/
polls/
detail.html
index.html
results.html
tests.py
urls.py
views.py
mytemplates/
admin/
base_site.html
Your project-wide templates directory should now look like this::
mytemplates/
admin/
base_site.html
Looking good! Now would be a good time to confirm that your polls application
still works correctly. How does Django know how to find the new location of
the polls templates even though we didn't modify :setting:`TEMPLATE_DIRS`?
Django has a :setting:`TEMPLATE_LOADERS` setting which contains a list
of callables that know how to import templates from various sources. One of
the defaults is :class:`django.template.loaders.app_directories.Loader` which
looks for a "templates" subdirectory in each of the :setting:`INSTALLED_APPS`.
You created ``mysite/mytemplates`` in :doc:`Tutorial 2 </intro/tutorial02>`,
and ``polls/templates`` in :doc:`Tutorial 3 </intro/tutorial03>`. Now perhaps
it is clearer why we chose to have separate template directories for the
project and application: everything that is part of the polls application is in
``polls``. It makes the application self-contained and easier to drop into a
new project.
The ``polls`` directory could now be copied into a new Django project and
immediately reused. It's not quite ready to be published though. For that, we
need to package the app to make it easy for others to install.
.. admonition:: Why nested?
Why create a ``polls`` directory under ``templates`` when we're
already inside the polls app? This directory is needed to avoid conflicts in
Django's ``app_directories`` template loader. For example, if two
apps had a template called ``base.html``, without the extra directory it
wouldn't be possible to distinguish between the two. It's a good convention
to use the name of your app for this directory.
.. _installing-reusable-apps-prerequisites:
Installing some prerequisites