{"id":159,"date":"2026-03-03T08:53:48","date_gmt":"2026-03-03T03:53:48","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=159"},"modified":"2026-03-17T08:47:51","modified_gmt":"2026-03-17T03:47:51","slug":"views-and-templates","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/views-and-templates\/","title":{"rendered":"Views and Templates"},"content":{"rendered":"\n<p>In Django, <strong>Views and Templates<\/strong> work together to display data to users.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>View<\/strong> \u2192 Handles logic and processes requests<\/li>\n\n\n\n<li><strong>Template<\/strong> \u2192 Displays data using HTML<\/li>\n<\/ul>\n\n\n\n<p>Together, they connect the backend with the frontend.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a View?<\/h2>\n\n\n\n<p>A View is a Python function or class that receives a request and returns a response.<\/p>\n\n\n\n<p>It contains business logic.<\/p>\n\n\n\n<p>Example of a simple function-based view:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from django.http import HttpResponsedef home(request):<br>    return HttpResponse(\"Welcome to My Website\")<\/pre>\n\n\n\n<p>This view returns simple text in the browser.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Rendering a Template from a View<\/h2>\n\n\n\n<p>Instead of returning plain text, we usually render an HTML template.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from django.shortcuts import renderdef home(request):<br>    return render(request, \"home.html\")<\/pre>\n\n\n\n<p>Here, Django looks for a template file named <strong>home.html<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Template?<\/h2>\n\n\n\n<p>A Template is an HTML file that displays dynamic data.<\/p>\n\n\n\n<p>Templates are usually stored inside a folder named <code>templates<\/code>.<\/p>\n\n\n\n<p>Example structure:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">blog\/<br>\u2502<br>\u251c\u2500\u2500 templates\/<br>\u2502   \u2514\u2500\u2500 home.html<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Template Example<\/h2>\n\n\n\n<p>home.html<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;head&gt;<br>    &lt;title&gt;Home&lt;\/title&gt;<br>&lt;\/head&gt;<br>&lt;body&gt;<br>    &lt;h1&gt;Welcome to My Website&lt;\/h1&gt;<br>&lt;\/body&gt;<br>&lt;\/html&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Passing Data from View to Template<\/h2>\n\n\n\n<p>Views can send data to templates using a context dictionary.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def home(request):<br>    context = {<br>        \"name\": \"Ali\",<br>        \"age\": 25<br>    }<br>    return render(request, \"home.html\", context)<\/pre>\n\n\n\n<p>Now access it in template:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;h1&gt;Welcome {{ name }}&lt;\/h1&gt;<br>&lt;p&gt;Age: {{ age }}&lt;\/p&gt;<\/pre>\n\n\n\n<p>Django uses double curly braces <code>{{ }}<\/code> to display variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Template Tags<\/h2>\n\n\n\n<p>Django templates support logic using template tags.<\/p>\n\n\n\n<p>Example: Loop<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;ul&gt;<br>{% for item in items %}<br>    &lt;li&gt;{{ item }}&lt;\/li&gt;<br>{% endfor %}<br>&lt;\/ul&gt;<\/pre>\n\n\n\n<p>Example: Condition<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{% if user.is_authenticated %}<br>    &lt;p&gt;Welcome, {{ user.username }}&lt;\/p&gt;<br>{% else %}<br>    &lt;p&gt;Please login&lt;\/p&gt;<br>{% endif %}<\/pre>\n\n\n\n<p>Template tags use <code>{% %}<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Connecting View to URL<\/h2>\n\n\n\n<p>In urls.py:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from django.urls import path<br>from . import viewsurlpatterns = [<br>    path('', views.home, name='home'),<br>]<\/pre>\n\n\n\n<p>Now when a user visits the URL, the view runs and renders the template.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Flow of Views and Templates<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>User visits URL<\/li>\n\n\n\n<li>urls.py maps URL to view<\/li>\n\n\n\n<li>View processes request<\/li>\n\n\n\n<li>View sends data to template<\/li>\n\n\n\n<li>Template renders HTML<\/li>\n\n\n\n<li>Browser displays result<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Why Views and Templates Are Important<\/h2>\n\n\n\n<p>They help you:<\/p>\n\n\n\n<p>Separate logic from design<br>Build dynamic websites<br>Display database data<br>Create user-friendly interfaces<br>Maintain clean project structure<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Takeaway<\/h2>\n\n\n\n<p>Views handle logic and process requests, while Templates handle presentation.<\/p>\n\n\n\n<p>Together, they allow Django to generate dynamic web pages and deliver content to users efficiently.<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773719348657\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON FOR WEB DEVELOPMENT (PYWEB) > Django Framework > Views and Templates<\/span><\/span><\/div>\n\n\n<p><\/p>\n","protected":false},"menu_order":87,"template":"","class_list":["post-159","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Views and Templates - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Django views and templates: handle logic, pass data, render HTML, and create dynamic web pages efficiently with best practices.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/views-and-templates\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Views and Templates - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Django views and templates: handle logic, pass data, render HTML, and create dynamic web pages efficiently with best practices.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/views-and-templates\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-17T03:47:51+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/views-and-templates\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/views-and-templates\\\/\",\"name\":\"Views and Templates - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-03T03:53:48+00:00\",\"dateModified\":\"2026-03-17T03:47:51+00:00\",\"description\":\"Learn Django views and templates: handle logic, pass data, render HTML, and create dynamic web pages efficiently with best practices.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/views-and-templates\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/views-and-templates\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/views-and-templates\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON FOR WEB DEVELOPMENT (PYWEB) > Django Framework > Views and Templates\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Views and Templates - One Language. Endless Possibilities","description":"Learn Django views and templates: handle logic, pass data, render HTML, and create dynamic web pages efficiently with best practices.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/python\/lesson\/views-and-templates\/","og_locale":"en_US","og_type":"article","og_title":"Views and Templates - One Language. Endless Possibilities","og_description":"Learn Django views and templates: handle logic, pass data, render HTML, and create dynamic web pages efficiently with best practices.","og_url":"https:\/\/gigz.pk\/python\/lesson\/views-and-templates\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-17T03:47:51+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/views-and-templates\/","url":"https:\/\/gigz.pk\/python\/lesson\/views-and-templates\/","name":"Views and Templates - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-03T03:53:48+00:00","dateModified":"2026-03-17T03:47:51+00:00","description":"Learn Django views and templates: handle logic, pass data, render HTML, and create dynamic web pages efficiently with best practices.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/views-and-templates\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/views-and-templates\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/views-and-templates\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON FOR WEB DEVELOPMENT (PYWEB) > Django Framework > Views and Templates"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}