DEV Community

Dr. Azad Rasul
Dr. Azad Rasul

Posted on

10- Django: create templates

First, create a directory called templates in your polls directory. Django will look for templates in there.

In "Sublime Text": select polls> right click> New Folder> name it "templates"

Within the templates directory, create another directory called polls, and within that create a file called index.html.

Select polls folder inside templates folder> right click> New File> Save as: index.html

Then, put the following code in that template:

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}
Enter fullscreen mode Exit fullscreen mode

Then, update our index view in polls/views.py to use the template:

from django.http import HttpResponse
from django.template import loader

from .models import Question


def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    template = loader.get_template('polls/index.html')
    context = {
        'latest_question_list': latest_question_list,
    }
    return HttpResponse(template.render(context, request))

def detail(request, question_id):
    return HttpResponse("You're looking at question %s." % question_id)

def results(request, question_id):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % question_id)

def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)
Enter fullscreen mode Exit fullscreen mode

Now, open http://127.0.0.1:8000/polls/ in your browser.

Inside polls directory in templates create datail.html and write following code in it:

<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>
Enter fullscreen mode Exit fullscreen mode

Add a shortcut: get_object_or_404() to polls/views.py

from django.shortcuts import get_object_or_404, render

from .models import Question
# ...
def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})
Enter fullscreen mode Exit fullscreen mode

To remove hardcoded URLs in templates, edite polls/index.html to:

<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
Enter fullscreen mode Exit fullscreen mode

So, index.html should be like:

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}
Enter fullscreen mode Exit fullscreen mode

If you like the content, please SUBSCRIBE to my channel for the future content

Top comments (0)