{"id":8335,"date":"2025-08-28T07:24:01","date_gmt":"2025-08-28T07:24:01","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=8335"},"modified":"2025-08-28T07:27:48","modified_gmt":"2025-08-28T07:27:48","slug":"template-view-pattern-with-example-in-python","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/","title":{"rendered":"Template View pattern with example in Python"},"content":{"rendered":"<p>Templates are often used in web frameworks. In the Python Django framework, a template view is used to dynamically render <a href=\"https:\/\/studysection.com\/blog\/drag-drop-in-html-with-jquery\/\">HTML<\/a> content. When the goal is to display some information on an HTML page, a Template view can do that very effectively.<\/p>\n<p>It\u2019s most suitable for static pages like \u201cabout-us\u201d that do not need any context variables. However, the template view can easily utilize context variables if needed.<\/p>\n<p>For better understanding, Let\u2019s create a basic Django project that will use template view to render a \u201ctest-page.html\u201d file that contains HTML content. Use \u201cpython3\u201d if you are following this tutorial on the <a href=\"https:\/\/blog.webnersolutions.com\/linux-grep-and-copy\/\">Linux system<\/a>. On Windows, use \u201cpython\u201d while executing commands.<\/p>\n<p>1. Open Terminal, create a new Python virtual environment in the current working directory, and activate it using:<\/p>\n<p>For Linux:<\/p>\n<p><code>$ python3 -m venv new_env<br \/>\n$ source new_env\/bin\/activate<\/code><\/p>\n<p>For Windows:<\/p>\n<p><code>&gt; python -m venv new_env<br \/>\n&gt; .\\new_env\\Script\\activate<\/code><\/p>\n<p>2. Install the Django framework in the current environment and start a new Python Django project using:<\/p>\n<p><code>$ pip install django<br \/>\n$ django-admin startproject project<\/code><\/p>\n<p>3. Create a new \u201capp\u201d in the project:<\/p>\n<p><code>$ cd project<br \/>\n$ python3 manage.py startapp app<\/code><\/p>\n<p>4. Open the \u201cviews.py\u201d file from \u201capp\u201d directory and paste the following code (make sure to remove the existing content in \u201cviews.py\u201d file):<\/p>\n<p>from django.views.generic.base import TemplateView<\/p>\n<p><code>class TestPage(TemplateView):<br \/>\ntemplate_name = 'test-page.html'<\/code><\/p>\n<p>5. Make a new \u201ctemplates\u201d named directory in \u201capp\u201d directory and create \u201ctest-page.html\u201d file inside \u201ctemplates\u201d directory with the following code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-8336\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2025\/08\/Screenshot-33-300x113.png\" alt=\"\" width=\"300\" height=\"113\" srcset=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2025\/08\/Screenshot-33-300x113.png 300w, https:\/\/studysection.com\/blog\/wp-content\/uploads\/2025\/08\/Screenshot-33.png 514w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>6. Open the \u201curls.py\u201d file inside \u201cproject\u201d directory and add the following code:<\/p>\n<p><code>from django.contrib import admin<br \/>\nfrom django.urls import path<br \/>\nfrom test_app.views import TestPage<\/code><\/p>\n<p><code>urlpatterns = [<br \/>\npath('admin\/', admin.site.urls),<br \/>\npath('', TestPage.as_view(), name=\"testpage\")<br \/>\n]<\/code><\/p>\n<p>7. Now open \u201csettings.py\u201d from \u201cproject\u201d directory and make the required changes in these two lines:<\/p>\n<p><code>ALLOWED_HOSTS = [\"127.0.0.1\", \"localhost\"]\n'DIRS': [\"test_app\/templates\"]<\/code><\/p>\n<p><code># \u2018DIRS\u2019 line is under \u201cTEMPLATES\u201d block<\/code><\/p>\n<p>8. Run below commands to complete the database initialization and migrations process for new project setup:<\/p>\n<p><code>$ python3 manage.py makemigrations<br \/>\n$ python3 manage.py migrate<\/code><\/p>\n<p>9. After successful database migration, execute the following command to run the test server:<\/p>\n<p><code>$ python3 manage.py runserver<\/code><\/p>\n<p>If everything is done correctly as explained above, the test server will start on http:\/\/120.0.0.1:8000 or http:\/\/localhost:8000 to access the Django application. By using the URL in the browser, you should see the rendered template similar as below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-8337\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2025\/08\/Screenshot-34-300x133.png\" alt=\"\" width=\"300\" height=\"133\" srcset=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2025\/08\/Screenshot-34-300x133.png 300w, https:\/\/studysection.com\/blog\/wp-content\/uploads\/2025\/08\/Screenshot-34.png 639w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Templates are often used in web frameworks. In the Python Django framework, a template view is used to dynamically render<\/p>\n","protected":false},"author":1,"featured_media":8339,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Template View pattern with example in Python<\/title>\n<meta name=\"description\" content=\"Templates are often used in web frameworks. In the Python Django framework, a template view pattern is used to render HTML content.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Template View pattern with example in Python\" \/>\n<meta property=\"og:description\" content=\"Templates are often used in web frameworks. In the Python Django framework, a template view pattern is used to render HTML content.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-28T07:24:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-28T07:27:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2025\/08\/Orange-and-Beige-Simple-Icon-Beauty-and-Fashion-Logo-6-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"940\" \/>\n\t<meta property=\"og:image:height\" content=\"788\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Template View pattern with example in Python\",\"datePublished\":\"2025-08-28T07:24:01+00:00\",\"dateModified\":\"2025-08-28T07:27:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/\"},\"wordCount\":312,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/\",\"url\":\"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/\",\"name\":\"Template View pattern with example in Python\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2025-08-28T07:24:01+00:00\",\"dateModified\":\"2025-08-28T07:27:48+00:00\",\"description\":\"Templates are often used in web frameworks. In the Python Django framework, a template view pattern is used to render HTML content.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Template View pattern with example in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Template View pattern with example in Python","description":"Templates are often used in web frameworks. In the Python Django framework, a template view pattern is used to render HTML content.","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:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Template View pattern with example in Python","og_description":"Templates are often used in web frameworks. In the Python Django framework, a template view pattern is used to render HTML content.","og_url":"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2025-08-28T07:24:01+00:00","article_modified_time":"2025-08-28T07:27:48+00:00","og_image":[{"width":940,"height":788,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2025\/08\/Orange-and-Beige-Simple-Icon-Beauty-and-Fashion-Logo-6-1.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Template View pattern with example in Python","datePublished":"2025-08-28T07:24:01+00:00","dateModified":"2025-08-28T07:27:48+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/"},"wordCount":312,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/","url":"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/","name":"Template View pattern with example in Python","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2025-08-28T07:24:01+00:00","dateModified":"2025-08-28T07:27:48+00:00","description":"Templates are often used in web frameworks. In the Python Django framework, a template view pattern is used to render HTML content.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/template-view-pattern-with-example-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Template View pattern with example in Python"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":49,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/8335"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=8335"}],"version-history":[{"count":1,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/8335\/revisions"}],"predecessor-version":[{"id":8338,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/8335\/revisions\/8338"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/8339"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=8335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=8335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=8335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}