Django Templates (DTL) Sinhala Guide | Context, Inheritance & Examples
ආයුබෝවන් යාළුවනේ! වෙබ් ඩිවලොප්මන්ට් කියන මාර්ගයේ යනකොට, අපිට තියෙන ලොකුම අභියෝගයක් තමයි දත්ත (data) ලස්සනට, පිලිවෙලකට, අපේ පරිශීලකයන්ට පේන්න සලස්වන එක. "බැරිද මේක තව ටිකක් ලස්සනට කරන්න?" කියලා ඔයාලටත් හිතිලා ඇති, නේද? අන්න ඒකට තමයි Django Templates අපිට උදව් කරන්නේ! Django කියන්නේ Python වලින් වෙබ් සයිට් හදන්න තියෙන සුපිරි ෆ්රේම්වර්ක් එකක්. ඒකේ මේ Template System එක කියන්නේ අපේ වෙබ් පේජ් එකේ පෙනුම (presentation) ඒකේ ඇතුලේ තියෙන ලොජික් (business logic) වලින් වෙන් කරලා තියාගන්න පුළුවන් ගේමක්. මේකෙන් අපේ කෝඩ් එක මාරම ලස්සන වෙනවා වගේම, අපිට ලේසියෙන් ඒක මැනේජ් කරගන්නත් පුළුවන්.
මේ Guide එකෙන් අපි Django Templates වල ලෝකයට එබී බලනවා. Django Template Language (DTL) කියන්නේ මොකක්ද, Context එකකින් අපේ දත්ත ටෙම්ප්ලේට් එකට ගෙනියන්නේ කොහොමද, වගේම Template Inheritance වගේ සුපිරි ක්රම පාවිච්චි කරලා අපේ කෝඩ් එක repeat වෙන එක නවත්තගන්නේ කොහොමද කියලා අපි කතා කරනවා. අවසානයේදී, මේ හැමදේම පාවිච්චි කරලා අපි Blog Posts ටිකක් වෙබ් සයිට් එකක display කරන්නේ කොහොමද කියලා ප්රායෝගිකව කරලා බලමු. එහෙනම්, අපි පටන් ගමු!
Django Template Language (DTL) කියන්නේ මොනවාද?
සරලවම කිව්වොත්, DTL කියන්නේ Django වලටම ආවේණික වුණු HTML ෆයිල් ඇතුලේ දත්ත display කරන්න, ලොජික් ටිකක් කරන්න පුළුවන් භාෂාවක්. මේක JavaScript වගේ full-fledged programming language එකක් නෙවෙයි. මේකේ ප්රධානම අරමුණ HTML එකට දත්ත ඇතුල් කරන එක සහ presentations වලට අවශ්ය මූලික ලොජික් ටික සපයන එකයි. DTL වල ප්රධාන කොටස් තුනක් තියෙනවා:
Variables
Variables පාවිච්චි කරන්නේ view එකෙන් template එකට එවන දත්ත display කරන්න. මේවා {{ variable_name }} කියන විදියට තමයි ලියන්නේ. උදාහරණයක් විදියට, view එකෙන් {'name': 'කමල්'} කියලා එව්වොත්, template එකේ {{ name }} කියලා දැම්මාම 'කමල්' කියලා display වෙනවා. dot notation එක පාවිච්චි කරලා dictionary එකක keys වලට හෝ object එකක attributes වලට access කරන්න පුළුවන්.
<h1>ආයුබෝවන්, {{ user.first_name }}!</h1>
<p>අද දවස: {{ current_date|date:"Y-m-d" }}</p>Tags
Tags පාවිච්චි කරන්නේ DTL එකේදී යම්කිසි ලොජික් එකක් (logic) කරන්න. උදාහරණයක් විදියට, loops, conditional statements වගේ දේවල්. මේවා {% tag_name %} කියන විදියට තමයි ලියන්නේ. හැම tag එකකටම වගේ closing tag එකක් තියෙනවා.
{% if user.is_authenticated %}
<p>ඔබ ලොග් වී ඇත.</p>
{% else %}
<p>කරුණාකර ලොග් වන්න.</p>
{% endif %}
{% for item in item_list %}
<li>{{ item.name }}</li>
{% endfor %}Filters
Filters පාවිච්චි කරන්නේ variable එකක දත්ත modify කරන්න. මේවා variable එකෙන් පස්සේ vertical bar (|) එකක් දාලා ලියනවා. උදාහරණයක් විදියට, string එකක් lowercase කරන්න, date එකක් format කරන්න වගේ දේවල්.
<p>නාමය (Lower): {{ user.name|lower }}</p>
<p>Date (Formatted): {{ post.pub_date|date:"F d, Y" }}</p>
<p>Paragraph Count: {{ text|wordcount }}</p>මේවා තමයි DTL වල මූලිකම දේවල්. මේවා ගැන තවදුරටත් කතා කරමු.
Context එකේ බලය (The Power of Context)
හරි, දැන් අපි DTL වලින් HTML ඇතුලේ දත්ත දාන්න පුළුවන් බව දැනගත්තා. හැබැයි මේ දත්ත templates වලට එන්නේ කොහොමද? අන්න ඒකට තමයි Context එක පාවිච්චි කරන්නේ! Context එක කියන්නේ සරලවම Python dictionary එකක්. අපේ Django views වලින් template එකට යවන්න ඕනේ හැම දත්තයක්ම මේ dictionary එක ඇතුලේ දාලා තමයි යවන්නේ.
View එකෙන් Context එකක් හදන හැටි
සාමාන්යයෙන් අපේ views.py ෆයිල් එකේ function-based view එකක් හෝ class-based view එකක් ඇතුලේ තමයි අපි මේ දත්ත හදන්නේ. ඊට පස්සේ, render() function එකට තුන්වෙනි argument එක විදියට මේ dictionary එක pass කරනවා.
# myapp/views.py
from django.shortcuts import render
from .models import Post # අපි පස්සේ හදන Post model එක
def blog_list(request):
posts = Post.objects.all().order_by('-created_at') # හැම post එකක්ම ගන්නවා
context = {
'title': 'මගේ Blog එක',
'posts': posts,
'user_name': 'රන්ජිත්'
}
return render(request, 'blog/post_list.html', context)
def post_detail(request, pk):
post = Post.objects.get(pk=pk) # නිශ්චිත post එකක් ගන්නවා
context = {
'post': post
}
return render(request, 'blog/post_detail.html', context)මේ උදාහරණයේදී, blog_list view එක title, posts, user_name කියන දත්ත templates වලට යවනවා. post_detail view එක post කියන එක යවනවා.
Template එකක Context දත්ත පාවිච්චි කරන හැටි
Templates ඇතුලේදී, අපි {{ variable_name }} පාවිච්චි කරලා මේ දත්ත වලට access කරනවා. Python dictionary එකක key එකක් වගේම, object එකක attribute එකක් වගේම මේවාට access කරන්න පුළුවන්.
<!-- blog/post_list.html -->
<h1>{{ title }}</h1>
<p>ආයුබෝවන්, {{ user_name }}!</p>
{% if posts %}
<ul>
{% for post in posts %}
<li>
<h2>{{ post.title }}</h2>
<p>පළකලේ: {{ post.created_at|date:"Y-m-d" }}</p>
<p>{{ post.content|truncatechars:150 }}</p>
<a href="/blog/{{ post.pk }}/">වැඩිදුර කියවන්න</a>
</li>
{% endfor %}
</ul>
{% else %}
<p>තවම කිසිදු Blog Post එකක් නැත.</p>
{% endif %}
මෙහිදී {{ title }}, {{ user_name }}, {{ post.title }} වගේ ඒවා Context එකෙන් එන දත්ත. truncatechars:150 කියන්නේ filter එකක්, ඒකෙන් content එකේ අකුරු 150කට සීමා කරනවා.
මේ විදියට Context එක පාවිච්චි කරලා අපේ Python logic එකයි, HTML presentation එකයි ලස්සනට වෙන් කරලා තියාගන්න පුළුවන්.
Template Inheritance – කම්මැලිකම අඩුකරගන්න!
වෙබ් සයිට් එකක් හදනකොට, අපිට ගොඩක් වෙලාවට එකම වගේ කොටස් (header, footer, navigation bar වගේ) හැම පේජ් එකකම පෙන්වන්න වෙනවා. හිතන්න, අපේ Blog එකේ හැම පේජ් එකටම උඩින් 'මගේ Blog එක' කියලා header එකක්, යටින් copyright notice එකක් තියෙන්න ඕනේ කියලා. මේ හැම පේජ් එකකම ඒ HTML කෝඩ් එක ආයෙ ආයෙ ලියන එක අපරාධයක් නේද? අන්න ඒකට තමයි Template Inheritance කියන සුපිරි feature එක Django අපිට දීලා තියෙන්නේ.
Template Inheritance කියන්නේ, එක master template එකක් හදලා (ඒකට කියන්නේ base template එකක් කියලා), අනිත් හැම template එකක්ම ඒ base template එකෙන් extend කරන එක. මේක හරියට අම්මා කෙනෙක්ගෙන් දරුවෙක් ගුණාංග උරුම කරගන්නවා වගේ වැඩක්.
base.html එකක් හදන හැටි
මුලින්ම අපි පොදු කොටස් තියෙන base.html ෆයිල් එකක් හදමු. මේකේදී අපි {% block block_name %} කියන tag එක පාවිච්චි කරනවා, අපේ extend කරන templates වලට වෙනස් කරන්න පුළුවන් තැන් සලකුණු කරන්න.
<!-- blog/base.html -->
<!DOCTYPE html>
<html lang="si">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}මගේ Django Blog එක{% endblock %}</title>
<style>
body { font-family: Arial, sans-serif; margin: 0; padding: 0; background-color: #f4f4f4; }
header { background-color: #333; color: white; padding: 1em 0; text-align: center; }
nav ul { list-style-type: none; padding: 0; text-align: center; background-color: #eee; }
nav ul li { display: inline; margin: 0 15px; }
nav ul li a { text-decoration: none; color: #333; }
.container { width: 80%; margin: auto; overflow: hidden; padding: 20px 0; }
footer { background-color: #333; color: white; text-align: center; padding: 1em 0; position: fixed; bottom: 0; width: 100%; }
</style>
</head>
<body>
<header>
<h1><a href="/blog/" style="color: white; text-decoration: none;">මගේ Django Blog එක</a></h1>
<nav>
<ul>
<li><a href="/blog/">මුල් පිටුව</a></li>
<li><a href="#">අප ගැන</a></li>
<li><a href="#">සම්බන්ධ වන්න</a></li>
</ul>
</nav>
</header>
<div class="container">
{% block content %}
<!-- Child templates will fill this content -->
{% endblock content %}
</div>
<footer>
<p>© 2023 මගේ Django Blog එක. සියලු හිමිකම් ඇවිරිණි.</p>
</footer>
</body>
</html>මෙහිදී අපි title එකටයි, content එකටයි block tags දැම්මා. මේවා තමයි අපේ child templates වලට වෙනස් කරන්න පුළුවන් තැන්.
Child Templates හදන හැටි
දැන් අපි base.html එක extend කරන child templates හදමු. මේවායේ මුලින්ම {% extends 'blog/base.html' %} කියන tag එක දාන්න ඕනේ. ඊට පස්සේ base.html එකේ තියෙන block tags ඇතුලේ අපිට ඕනේ විදියට අලුත් content එක දාන්න පුළුවන්.
<!-- blog/post_list.html (child template) -->
{% extends 'blog/base.html' %}
{% block title %}සියලුම Blog Posts{% endblock %}
{% block content %}
<h2>අලුත්ම Blog Posts</h2>
{% if posts %}
{% for post in posts %}
<article style="background-color: white; padding: 15px; margin-bottom: 15px; border-radius: 5px;">
<h3><a href="{% url 'post_detail' pk=post.pk %}" style="color: #333; text-decoration: none;">{{ post.title }}</a></h3>
<p>පළකලේ: {{ post.created_at|date:"F d, Y" }}</p>
<p>{{ post.content|truncatewords:50 }} <a href="{% url 'post_detail' pk=post.pk %}">වැඩිදුර කියවන්න</a></p>
</article>
{% endfor %}
{% else %}
<p>තවම කිසිදු Blog Post එකක් නැත.</p>
{% endif %}
{% endblock content %}මේ post_list.html එකේ අපි base.html එකේ title block එකයි, content block එකයි override කරලා තියෙනවා. මේකෙන් වෙන්නේ base.html එකේ පොදු කොටස් එහෙමම තියාගෙන, අපිට ඕනේ දේවල් විතරක් වෙනස් කරන්න පුළුවන් වෙන එක. මේකෙන් අපේ කෝඩ් එක මාරම clean වෙනවා වගේම, නඩත්තු කරන්නත් ලේසියි.
Blog Posts Display කරමු (Let's Display Blog Posts Practically)
හරි, දැන් අපි DTL, Context, Template Inheritance ගැන theoretical විදියට දැනගත්තා. දැන් මේවා එකට පාවිච්චි කරලා ඇත්තටම Blog Posts ටිකක් වෙබ් සයිට් එකක display කරමු. අපි සරල Blog application එකක් හදනවා කියලා හිතමු.
1. Model එක හදමු (models.py)
මුලින්ම අපි Blog Post එකක් නියෝජනය කරන්න සරල Model එකක් හදමු.
# myproject/blogapp/models.py
from django.db import models
from django.utils import timezone
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
created_at = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.title
class Meta:
ordering = ['-created_at'] # අලුත්ම posts මුලින් පෙන්වන්නමේක blogapp කියලා app එකක් හදලා ඒක ඇතුලේ models.py එකට දාන්න ඕනේ. makemigrations සහ migrate කරන්න අමතක කරන්න එපා.
python manage.py makemigrations blogapp
python manage.py migrate2. Views හදමු (views.py)
දැන් අපි Blog Posts ලැයිස්තුවයි, තනි Post එකක විස්තරයි පෙන්වන්න Views දෙකක් හදමු.
# myproject/blogapp/views.py
from django.shortcuts import render, get_object_or_404
from .models import Post
def post_list(request):
posts = Post.objects.all() # සියලුම posts ලබා ගන්න
context = {'posts': posts}
return render(request, 'blogapp/post_list.html', context)
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk) # නිශ්චිත post එකක් ලබා ගන්න, නැත්නම් 404 error
context = {'post': post}
return render(request, 'blogapp/post_detail.html', context)3. URLs සකසමු (urls.py)
Views වලට access කරන්න URLs හදමු. මුලින්ම myproject/urls.py එකේ අපේ blogapp එකේ URLs include කරන්න.
# myproject/myproject/urls.py
from django.contrib import admin
from django.urls import path, include # include එක import කරන්න
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blogapp.urls')), # blogapp එකේ urls include කරනවා
]දැන් blogapp එක ඇතුලේ අලුතින් urls.py ෆයිල් එකක් හදලා අපේ Views වලට URLs define කරන්න.
# myproject/blogapp/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.post_list, name='post_list'),
path('<int:pk>/', views.post_detail, name='post_detail'), # <int:pk> කියන්නේ Post ID එක
]name='post_list' සහ name='post_detail' කියන්නේ templates වල {% url 'name' %} tag එක පාවිච්චි කරලා URLs reverse කරන්න පුළුවන්. මේකෙන් අපේ links dynamic වෙනවා.
4. Templates හදමු (templates/blogapp/)
අපි blogapp/templates/blogapp/ කියලා ෆෝල්ඩරයක් හදලා ඒ ඇතුලේ අපේ templates හදමු. (settings.py එකේ TEMPLATES config එක හරිද කියලා බලන්න.)
base.html
මේක කලින් section එකේ දුන්නු base.html එකමයි. blogapp/templates/blogapp/base.html එකට මේක දාන්න.
<!-- blogapp/templates/blogapp/base.html -->
<!DOCTYPE html>
<html lang="si">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}මගේ Django Blog එක{% endblock %}</title>
<style>
body { font-family: Arial, sans-serif; margin: 0; padding: 0; background-color: #f4f4f4; }
header { background-color: #333; color: white; padding: 1em 0; text-align: center; }
nav ul { list-style-type: none; padding: 0; text-align: center; background-color: #eee; }
nav ul li { display: inline; margin: 0 15px; }
nav ul li a { text-decoration: none; color: #333; }
.container { width: 80%; margin: 20px auto; overflow: hidden; padding: 20px; background-color: white; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
footer { background-color: #333; color: white; text-align: center; padding: 1em 0; margin-top: 20px; }
.post-card { border-bottom: 1px solid #eee; padding-bottom: 15px; margin-bottom: 15px; }
.post-card:last-child { border-bottom: none; }
</style>
</head>
<body>
<header>
<h1><a href="{% url 'post_list' %}" style="color: white; text-decoration: none;">මගේ Django Blog එක</a></h1>
<nav>
<ul>
<li><a href="{% url 'post_list' %}">මුල් පිටුව</a></li>
<li><a href="#">අප ගැන</a></li>
<li><a href="#">සම්බන්ධ වන්න</a></li>
</ul>
</nav>
</header>
<div class="container">
{% block content %}
<!-- Child templates will fill this content -->
{% endblock content %}
</div>
<footer>
<p>© 2023 මගේ Django Blog එක. සියලු හිමිකම් ඇවිරිණි.</p>
</footer>
</body>
</html>post_list.html
මේකත් කලින් section එකේ post_list.html එකමයි. blogapp/templates/blogapp/post_list.html එකට දාන්න.
<!-- blogapp/templates/blogapp/post_list.html -->
{% extends 'blogapp/base.html' %}
{% block title %}සියලුම Blog Posts{% endblock %}
{% block content %}
<h2>අලුත්ම Blog Posts</h2>
{% if posts %}
{% for post in posts %}
<article class="post-card">
<h3><a href="{% url 'post_detail' pk=post.pk %}" style="color: #333; text-decoration: none;">{{ post.title }}</a></h3>
<p><em>පළකලේ: {{ post.created_at|date:"F d, Y @ P" }}</em></p>
<p>{{ post.content|truncatewords:50 }} <a href="{% url 'post_detail' pk=post.pk %}">වැඩිදුර කියවන්න</a></p>
</article>
{% endfor %}
{% else %}
<p>තවම කිසිදු Blog Post එකක් නැත.</p>
{% endif %}
{% endblock content %}post_detail.html
තනි Blog Post එකක විස්තර පෙන්වන්න template එකක්.
<!-- blogapp/templates/blogapp/post_detail.html -->
{% extends 'blogapp/base.html' %}
{% block title %}{{ post.title }}{% endblock %}
{% block content %}
<article>
<h2>{{ post.title }}</h2>
<p><em>පළකලේ: {{ post.created_at|date:"F d, Y @ P" }}</em></p>
<p>{{ post.content|linebreaksbr }}</p>
<hr>
<a href="{% url 'post_list' %}">සියලුම Posts වෙත ආපසු යන්න</a>
</article>
{% endblock content %}මෙහිදී {{ post.content|linebreaksbr }} filter එක පාවිච්චි කරලා text එකේ තියෙන line breaks HTML <br> tags බවට පත් කරනවා.
5. Post ටිකක් දාගමු (Admin Panel එකෙන්)
දැන් Admin Panel එකට ගිහින් Posts ටිකක් හදන්න ඕනේ. ඒකට blogapp/admin.py එකට මේ ටික එකතු කරන්න:
# myproject/blogapp/admin.py
from django.contrib import admin
from .models import Post
admin.site.register(Post)python manage.py createsuperuser කරලා superuser කෙනෙක් හදලා /admin වලට ගිහින් Posts ටිකක් දාගන්න. ඊට පස්සේ http://127.0.0.1:8000/blog/ වලට ගියාම ඔයාගේ posts ලස්සනට display වෙයි!
Best Practices සහ Trouble-shooting (Best Practices and Troubleshooting)
හරි, දැන් අපි Django Templates හොඳටම පාවිච්චි කරන්න ඉගෙන ගත්තා. හැබැයි ඕනෑම දෙයක් කරනකොට, ඒක හරියට, පිළිවෙලකට කරන එක ගොඩක් වැදගත්. ඒ වගේම, සමහර වෙලාවට පොඩි පොඩි අවුල් ඇතිවෙන්න පුළුවන්. ඒවාට මුහුණ දෙන්නේ කොහොමද කියලත් බලමු.
Best Practices: Templates Clean කරගනිමු!
- Presentation Logic විතරක් තියාගන්න: Templates වල ප්රධානම අරමුණ දත්ත පෙන්වන එක. Complex business logic, database queries වගේ දේවල් Views ඇතුලෙම කරන්න. Templates වල
ifstatements,forloops වගේ සරල presentation logic විතරක් තියාගන්න. මේකෙන් කෝඩ් එක කියවන්න ලේසි වෙනවා වගේම, නඩත්තු කරන්නත් පහසුයි. (Separate concerns කියන්නේ මේකට තමයි.) - Template Inheritance උපරිමයෙන් පාවිච්චි කරන්න: අපි කලින් කතා කරපු විදියට,
base.htmlඑකක් හදලා හැම template එකක්ම ඒකෙන් extend කරන්න. පොදු CSS, JavaScript, header, footer වගේ දේවල්base.htmlඑකට දාන්න. මේකෙන් කෝඩ් duplicate වෙන එක නතර වෙනවා. - Reusability (නැවත භාවිතය) ගැන හිතන්න: සමහර template කොටස් (උදා: blog post card එකක්) අපිට තව තැන් වල පාවිච්චි කරන්න අවශ්ය වෙන්න පුළුවන්. ඒ වගේ වෙලාවට
{% include 'path/to/partial.html' %}tag එක පාවිච්චි කරන්න පුළුවන්. මේකෙන් කෝඩ් එක තවදුරටත් modular වෙනවා. - යහපත් නාමකරණය (Good Naming Conventions): Variables, blocks, files වලට තේරුමක් තියෙන නම් දෙන්න.
post_list.html,post_detail.htmlවගේ පැහැදිලි නම් පාවිච්චි කරන්න. - Comments පාවිච්චි කරන්න: Template එකේ සංකීර්ණ කොටස් තියෙනවා නම්,
{# This is a comment #}කියලා comments දාන්න. මේවා HTML output එකට යන්නේ නෑ.
Troubleshooting: අවුල් වෙලාවට මොකද කරන්නේ?
- Template Not Found Error:
- හේතුව: Django ට ඔබේ template file එක හොයාගන්න බැරිවෙලා.
- විසඳුම:
settings.pyඑකේTEMPLATESconfig එකේDIRSවලට ඔබේ template folder එකේ (e.g.,os.path.join(BASE_DIR, 'templates')) path එක හරියට දීලා තියෙනවද කියලා බලන්න.- App එක ඇතුලේ
templates/app_name/ව්යුහය හරියට තියෙනවද කියලා බලන්න. (myapp/templates/myapp/my_template.htmlවගේ). render()function එකේ template path එක (e.g.,'blogapp/post_list.html') හරියට දීලා තියෙනවද කියලා බලන්න.
- Variable Not Displaying / Silent Failure:
- හේතුව: Template එකේදී
{{ variable }}එක display වෙන්නේ නැත්නම්, ඒ variable එකContextඑකට ඇවිත් නැති වෙන්න පුළුවන්, නැත්නම් නම වැරදි වෙන්න පුළුවන්. Django Templates වල නැති variable එකක් display කරන්න හැදුවොත් error එකක් දෙන්නේ නැතුව හිස් තැනක් (empty string) පෙන්නනවා (silent failure). - විසඳුම:
- View එකේදී
contextdictionary එකට variable එක හරියට pass කරලා තියෙනවද කියලා බලන්න. (e.g.,{'post': post_object}). - Template එකේදී variable නම හරියට ලියලා තියෙනවද කියලා බලන්න. (e.g.,
{{ post.title }}නිකම්{{ title }}නෙවෙයි නම්). - View එකේදී
print(context)කරලා බලන්න templates වලට යන data මොනවද කියලා.
- View එකේදී
- හේතුව: Template එකේදී
- Syntax Errors (Tags/Filters):
- හේතුව: DTL tags හෝ filters වල syntax එක වැරදියි. (e.g.,
{% if %}එකට{% endif %}නැතිවීම,|එක වෙනුවට:දැමීම.) - විසඳුම:
- Error message එක හොඳින් කියවන්න. Django error pages ගොඩක් useful.
- Django documentation එක බලලා නිවැරදි syntax එක 확인 කරන්න.
- හේතුව: DTL tags හෝ filters වල syntax එක වැරදියි. (e.g.,
මේ උපදෙස් ටික මතක තියාගත්තොත්, ඔයාලට Django Templates එක්ක වැඩ කරන එක තවත් පහසු වෙයි!
නිගමනය (Conclusion)
ඉතින් යාළුවනේ, අපි Django Templates වල ලෝකය ගැන ගොඩක් දේවල් ඉගෙන ගත්තා. Django Template Language (DTL) කියන්නේ මොකක්ද, variables, tags, filters පාවිච්චි කරන්නේ කොහොමද කියලා අපි බැලුවා. ඒ වගේම, Views වලින් Templates වලට දත්ත යවන්න Context එක පාවිච්චි කරන හැටිත්, හැම පේජ් එකකම පොදු කොටස් නැවත නැවත නොලියා ඉන්න Template Inheritance පාවිච්චි කරන හැටිත් අපි සාකච්ඡා කළා. අවසානයේදී, මේ හැමදේම එකට පාවිච්චි කරලා, අපි සරල Blog application එකක Posts ලස්සනට display කරන හැටිත් ප්රායෝගිකව කරලා බැලුවා.
මතක තියාගන්න, Django Templates වල ප්රධානම අරමුණ තමයි presentation layer එක business logic එකෙන් වෙන් කරලා තියාගන්න එක. මේකෙන් අපේ කෝඩ් එක clean වෙනවා වගේම, වැඩ කරන්නත් ලේසියි.
දැන් ඔයාලට පුළුවන් මේක ඔයාලගේම project එකක implement කරලා බලන්න. ඔයාලට Templates එක්ක වැඩ කරනකොට ඇතිවුණු අත්දැකීම්, ප්රශ්න, හෝ share කරන්න තියෙන tips මොනවද? පහළින් comment එකක් දාලා කියන්න! තවදුරටත් Django ගැන ඉගෙන ගන්න අපේ අනෙකුත් Guideස් බලන්නත් අමතක කරන්න එපා. ජයවේවා!