DJango Note: From the beginning (creating a view from a template)

Finally, we will create a page using the template.

Set the location of the template

setting.py ######

TEMPLATE_DIRS = (
	'path/to/your/templates'	#It looks good anywhere
)

Of course, once you set it up, you have to prepare the place properly.

Create template

Once the template directory is ready, create more subdirectories for each app and create HTML templates in it. This time, create index.html in "polls".

index.html ######

{% if latest_poll_list %}
    <ul>
    {% for poll in latest_poll_list %}
        <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

HTML is coming in between Python programs. Is each process from {% hoge…%} to {% endhoge%}? There is a part surrounded by {{}} inside, but this may be something like echo in PHP. It seems that the value is returned to HTML as it is.

Specify template as view

Once you have a template, set it as the view for that page.

views.py ######

from django.template import Context, loader
from polls.models import Poll
from django.http import HttpResponse

def index(request):
    latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
    temp = loader.get_template('polls/index.html')
    contxt = Context({
        'latest_poll_list': latest_poll_list,  #Show the latest one?
    })
    return HttpResponse(temp.render(contxt))

Also, if you use the shortcuts module, you can write it short as follows.

from django.shortcuts import render_to_response
#This eliminates the need for templates and http
from polls.models import Poll

def index(request):
    latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
    return render_to_response('polls/index.html',
                              {'latest_poll_list': latest_poll_list})
    #Are you returning with the feeling that temp and contxt of the previous code are put together?

Personally, I prefer the previous writing style at the longest (because it is easy to understand). I want to use the shortcut after getting used to it.

*** By the way, there were two unsettling points, so I would like to mention them ***

  1. ’-pub_date’: What does the first hyphen mean?

  2. [: 5]: Similarly, the colon is unclear.

Next time, we will resume from the error screen settings.

Recommended Posts

DJango Note: From the beginning (creating a view from a template)
DJango Memo: From the beginning (creating a view)
DJango Note: From the beginning (using a generic view)
DJango Note: From the beginning (form processing)
DJango Memo: From the beginning (preparation)
DJango Note: From the beginning (simplification and splitting of URLConf)
DJango Memo: From the beginning (model settings)
DJango memo: From the beginning (editing the management screen) There is a mystery
Specify the view URL in your Django template
DJango Memo: From the beginning (Error screen settings)
Steps from installing Python 3 to creating a Django app
Make the model a string on a Django HTML template
Template registration from Django Bootstrap
The story of a Django model field disappearing from a class
DJango Memo: From the beginning (more edits to the management screen)
Django: Import a class from a string
DJango memo: From the beginning (using the management screen) my addictive point
Get the value from the [Django] Form
Make a filter with a django template
A note that you want to manually decorate the parameters passed in the Django template form item by item
How to get a namespaced view name from a URL (path_info) in Django
How to pass values to JavaScript variables directly from the [Django] template tag
[Note] Read a file from another directory
Learning notes from the beginning of Python 1
Omit BOM from the beginning of the string
Commands for creating a new django project
(Note) Template file search order in Django
Creating a login screen with Django allauth
A note on enabling PostgreSQL with Django
Use Django from a local Python script
Learning notes from the beginning of Python 2
Get only the text from the Django form.
Write a short if-else for Django Template
A note about doing the Pyramid tutorial
Django note 4
A series of amateur infrastructure engineers touching Django with Docker ⑤: View, Template, CSS
How to count the number of elements in Django and output to a template
Django # 2 (template)
The story of Django creating a library that might be a little more useful
[Django] Create a form that automatically fills in the address from the zip code
Let's display a simple template that is ideal for Django for the first time
Django Note 1
Django note 3
Django note 2
A note on customizing the dict list class
A note about the python version of python virtualenv
Try modifying the TortoiseHg file view a bit
Finding the beginning of Abenomics from NT magnification 2
A note about the new style base class
Run a Python file from html using Django
A story of creating 16 * 16 dots from a Digimon photo
Finding the beginning of Abenomics from NT magnification 1
[Django] Hit a command you made from within the process that runs on manage.py.