Django HTML template

Introduction

Here's how to create an HTML template file for django. As a model, we will use the SampleModel described in Previous article.

Embedding variables and functions in HTML files

In the HTML file, you can use {{variable name}} to embed the variable passed from views.py in the HTML file. You can also control the display contents using a function by setting {% function name argument 1 argument 2 ...%}.

Display record list

For class-based views, the list of records to display is passed to the template as model name_list. Use {% for%} to extract this record list one by one and display it. Similarly for function-based views, it's a good idea to pass the model's record list as context to the HTML file.

app/samplemodel_list.html


{% for sample in samplemodel_list %}
    <h1>{{ sample.char_sample }}</h1>
    <p>{{ sample.text_sample }}</p>
{% endfor %}

View individual records

For class-based views, individual records are passed to the template as model name.

app/samplemodel_detail.html


<h1>{{ samplemodel.char_sample }}</h1>
<p>{{ samplemodel.text_sample }}</p>

Summary

Here, I explained the basics of creating HTML templates for django. Next time, I'll cover template inheritance and custom tag creation.

Recommended Posts

Django HTML Template # 2
Django HTML template
Django # 2 (template)
Django Template notes
Django Template Tips
Django template file organization
Flask HTML template settings
Django
Make the model a string on a Django HTML template
Template registration from Django Bootstrap
Show Django ManyToManyField in Template
template
Common html to rent with Django
Make a filter with a django template
[Tips] How to do template extends when creating HTML with django
How to do arithmetic with Django template
django update
Django note 4
Django memorandum
django search
Django Summary
Django test
(Note) Template file search order in Django
Django Note 5
Django hands-on
Touch django
django notes
Django Summary
Django basics
Django Shoho
WEB application development using Django [Template addition]
Django defaults
Django + Docker
Django Glossary
Django search
(For myself) Django_1 (Basic / Hello World / Template)
Install Django
Django: References
Django Note 1
Edit Django shift table HTML and CSS
Django note 3
Django note 2
Resolve Angural JS and Django template conflicts
Django startup
Write a short if-else for Django Template
Django notes
Django NullCharField
Django template reads Makdown and reStructuredText written in an external file as HTML
In Django, display batch (command) results sequentially in HTML
Specify the view URL in your Django template
Run a Python file from html using Django