templates

Date: March 18th 2016
Last updated: March 18th 2016

Rather than hard coding the layout of each page in views.py I can use templates. There are a few things to note. First, there is some setup in the directory tree that is not provided by default. When creating a template it is advised to use the namespacing convention to make sure Django knows which file to choose. In the first code block below, Django will be looking for surferprofile/index.html.

create template directory

# change directory to app
# create template directory
mkdir templates
cd templates

# make namespace folder
mkdir surferprofile
cd surferprofile

# make index.html file etc
touch index.html

# tree < snipped >
surferprofile
    ├── admin.py
    ├── models.py
    ├── templates
    │   └── surferprofile #<=== namespace
    │       └── index.html
    ├── tests.py
    ├── urls.py
    └── views.py

Add to index.html
surfer_list is created in views.py below. If the list has entries in it then the html file will create a list with each surfer appearing on one line. Only the first name is requested and it is provided with a hyperlink containing the primary key.

Notice the href command. The surferprofile:profile string is written this way, instead of simply writing 'profile', because of the updated urls.py file below.

{% if surfer_list %}
    <ul>
    {% for surfer in surfer_list %}
        <li><a href="{% url 'surferprofile:profile' surfer.id %}">
            {{ surfer.firstname }}
            {{ surfer.lastname }}
            {{ surfer.height}}
        </a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No surfers are available.</p>
{% endif %}

Create and add surferprofile/profile.html
This html syntax will return the firstname and lastname (e.g. "Ray Blick") because of the special method used to return a string (__str__).

{{ surfer }}

Update views.py
The surfer list is created using the syntax used in the django shell example. I have bypassed the use of HttpResponse for rendering surfer_list to html. Instead I have used the render shortcut. Make sure to import each model into views.py otherwise this won't work.

I have updated the profilepage method by adding a shortcut to render a 404 page if the surfer id does not exist. Note the link between these methods. When a name is clicked on the index page, a new request sends the primary key to the profilepage method.

from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import render, get_object_or_404, render
from .models import Surfer #<========== Don't forget this

def index(request):
    surfer_list = Surfer.objects.all()
    context = {'surfer_list': surfer_list }
    return render(request, 'surferprofile/index.html', context)

def profilepage(request, surfer_id):
    surfer = get_object_or_404(Surfer, pk=surfer_id)
    return render(request, 'surferprofile/profile.html', {'surfer': surfer})

## temporarily removed surfdiary method

Urls.py
Note that urls.py remains almost the same, it is calling the same method. However, I have added app_name which provides namespacing for url patterns.

from django.conf.urls import url
from . import views

app_name = 'surferprofile' # <=== url namespacing
urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<surfer_id>[0-9]+)/$',views.profilepage, name='profile'),
    #url(r'^(?P<surfer_id>[0-9]+)/diary/$',views.creatediaryentry, name='diary'),
]

results matching ""

    No results matching ""