`` HTML tag follow by ````. Delete the HTML comment and the following HTML tag ``
`` including it's contents. Here can see the lines you have to
delete:
.. literalinclude:: ../src/mysite/templates/base.orig.html
:language: html+django
:lines: 66-83
:lineno-start: 66
:dedent: 6
Now add two additional blocks to the remaining ``
`` HTML tag; one for the
headline and the other for the contents of the current page:
.. literalinclude:: ../src/mysite/templates/base.html
:language: html+django
:lines: 50-63
:emphasize-lines: 2-7
:lineno-start: 50
:dedent: 4
That's it! The base template is done!
Create the templates for the views
==================================
Now the base template is finished. The next step is to create the
missing templates for the views. First create the appropriate directory
structure:
::
$ mkdir -p marcador/templates/marcador
This structure is necessary so that the template loader can load the
templates. After that the app ``marcador`` has this structure:
::
marcador
|-- __init__.py
|-- admin.py
|-- migrations
| |-- 0001_initial.py
| `-- __init__.py
|-- models.py
|-- templates
| `-- marcador
|-- tests.py
|-- urls.py
`-- views.py
.. note::
If the development server was running while creating the new
directories you have to restart it. Otherwise the new directories
wouldn't be recognized.
Now create the templates inside the directory
:file:`marcador/templates/marcador`.
At first the template for the list view :file:`bookmark_list.html`:
.. literalinclude:: ../src/mysite/marcador/templates/marcador/bookmark_list.html
:language: html+django
:linenos:
This template extends the base template :file:`base.html` with the help
of the :djangodocs:`extends [` tag.
Therefor it's possible to extend the blocks defined inside the base
template. Besides the already mentioned ``for`` tag is used to iterate
over the list of bookmarks (``bookmarks``) and create a list entry for
each. The template for the bookmark is included using the
:djangodocs:`include ][` tag.
Now you create the template :file:`bookmark.html` which has been
included in the previous template using the ``include`` tag:
.. literalinclude:: ../src/mysite/marcador/templates/marcador/bookmark.html
:language: html+django
:lines: 1-17
:linenos:
In this template a ``Bookmark`` object is available which is bound to
the ``bookmark`` variable. In a template it's possible to access the
attributes of an object, for example the title by using
``bookmark.title``. The attributes can be modified by filters, for
example by using :djangodocs:`linebreaksbr
][` which converts all ``]
``
HTML tags to real line breaks.
It is also possible to access related objects, for example the tags.
Because ``bookmark.tags`` is a list of objects represented by a
:ref:`manager object ` you can access the ``Tag`` objects only
with the help of methods defined by the manager. ``bookmark.tags.count``
returns the number of tags. ``bookmark.tags.all`` creates an iterator
which can be used in a loop. To call these methods in your Python code
you have to put a pair of brackets at the end of the method
(``bookmark.tags.count()``). If the methods are called in a template no
brackets are allowed because you can't pass arguments to a method in a
template.
.. index:: url
.. _url-template-tag:
With the help of the :djangodocs:`url [` tag
a link to the bookmark owners other bookmarks is created at the end of
the template. Because the URL ``marcador_bookmark_user`` needs a user
name we need to pass it as an argument to the template tag. That implies
arguments can be passed to template tags.
At last the template for the user's bookmarks must be created,
:file:`bookmark_user.html`:
.. literalinclude:: ../src/mysite/marcador/templates/marcador/bookmark_user.html
:language: html+django
:linenos:
This template extends the template :file:`bookmark_list.html` because
there are only very small differences between them. We need only to
overwrite the blocks ``title`` and ``heading`` and can reuse the rest.
Very convenient!
Test the new templates
======================
After all templates have been created you can start the development
server and have a look at the bookmarks, which you have created using
the admin, at the front end under http://127.0.0.1:8000/.
The list of bookmarks should look like this:
.. image:: /images/marcador-latest-bookmarks.*
:alt: Frontend Bookmark List View
:align: center
The login form at the top of the page does not work yet. But you will finish it
right in the next chapter!
]