{"id":75290,"date":"2020-01-18T14:12:25","date_gmt":"2020-01-18T08:42:25","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=75290"},"modified":"2024-04-12T14:17:12","modified_gmt":"2024-04-12T08:47:12","slug":"using-templates-in-views","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/","title":{"rendered":"Using Templates in Views"},"content":{"rendered":"<p class=\"VSKILLbodytext\">You\u2019ve learned the basics of using the template system; now let\u2019s use this knowledge to create a view.<\/p>\n<p class=\"VSKILLbodytext\">Recall the <span class=\"pre\">current_datetime<\/span> view in <span class=\"pre\">mysite.views<\/span>, which we started in the previous chapter. Here\u2019s what it looks like:<\/p>\n<pre class=\"VSKILLbodytext\">from django.http import HttpResponse\nimport datetime\n\u00a0def current_datetime(request):\n\u00a0\u00a0\u00a0 now = datetime.datetime.now()\n\u00a0\u00a0\u00a0 html = \"&lt;html&gt;&lt;body&gt;It is now %s.&lt;\/body&gt;&lt;\/html&gt;\" % now\n\u00a0\u00a0\u00a0 return HttpResponse(html)<\/pre>\n<p class=\"VSKILLbodytext\">Let\u2019s change this view to use Django\u2019s template system. At first, you might think to do something like this:<\/p>\n<pre class=\"VSKILLbodytext\">from django.template import Template, Context\nfrom django.http import Http\nResponse\nimport datetime\ndef current_datetime(request):\n\u00a0\u00a0\u00a0 now = datetime.datetime.now()\n\u00a0\u00a0\u00a0 t = Template(\"&lt;html&gt;&lt;body&gt;It is now {{ current_date\n }}.&lt;\/body&gt;&lt;\/html&gt;\")\n\u00a0\u00a0\u00a0 html = t.render(Context({'current_date': now}))\n\u00a0\u00a0\u00a0 return HttpResponse(html)\n\n<\/pre>\n<p>Sure, that uses the template system, but it doesn\u2019t solve the problems we pointed out in the introduction of this chapter. Namely, the template is still embedded in the Python code. Let\u2019s fix that by putting the template in a separate file, which this view will load.<\/p>\n<p class=\"VSKILLbodytext\">You might first consider saving your template somewhere on your filesystem and using Python\u2019s built-in file-opening functionality to read the contents of the template. Here\u2019s what that might look like, assuming the template was saved as the file \/home\/djangouser\/templates\/mytemplate.html:<\/p>\n<pre class=\"VSKILLbodytext\">from django.template import Template, \nContextfrom django.http import HttpResponse\nimport datetime\ndef current_datetime(request):\n\u00a0 \u00a0\u00a0now = datetime.datetime.now()\n\u00a0\u00a0\u00a0 # Simple way of using templates from the filesystem.\n\u00a0\u00a0\u00a0 # This doesn't account for missing files!\n\u00a0\u00a0\u00a0 fp = open('\/home\/djangouser\/templates\/mytemplate.html')\n\u00a0\u00a0\u00a0 t = Template(fp.read())\n\u00a0\u00a0\u00a0 fp.close()\n\u00a0\u00a0\u00a0 html = t.render(Context({'current_date': now}))\n\u00a0\u00a0\u00a0 return HttpResponse(html)<\/pre>\n<p>This approach, however, is inelegant for these reasons:<\/p>\n<ul>\n<li>It doesn\u2019t handle the case of a missing file. If the file html doesn\u2019t exist or isn\u2019t readable, the open() call will raise an IOError exception.<\/li>\n<li>It hard-codes your template location. If you were to use this technique for every view function, you\u2019d be duplicating the template locations. Not to mention it involves a lot of typing!<\/li>\n<li>It includes a lot of boring boilerplate code. You\u2019ve got better things to do than to write calls to open(), read(), and fp.close() each time you load a template.<\/li>\n<\/ul>\n<p>To solve these issues, we\u2019ll use <em>template loading<\/em> and <em>template directories<\/em><em>.<\/em><\/p>\n<p><strong>Template Loading<\/strong><\/p>\n<p>Django provides a convenient and powerful API for loading templates from the filesystem, with the goal of removing redundancy both in your template-loading calls and in your templates themselves. In order to use this template-loading API, first you\u2019ll need to tell the framework where you store your templates. The place to do this is in your settings file \u2013 the settings.py file that I mentioned last chapter, when I introduced the ROOT_URLCONF setting. If you\u2019re following along, open your settings.py and find the TEMPLATES setting. It\u2019s a list of configurations, one for each engine:<\/p>\n<pre>TEMPLATES = [\n{\n'BACKEND': 'django.template.backends.django.DjangoTemplates',\n'DIRS': [],\n'APP_DIRS': True,\n'OPTIONS': {\n# ... some options here ...\n},\n},\n]<\/pre>\n<p>BACKEND is a dotted Python path to a template engine class implementing Django\u2019s template backend API. The built-in backends are django.template.backends.django.DjangoTemplates and django.template.backends.jinja2.Jinja2.<\/p>\n<p>Since most engines load templates from files, the top-level configuration for each engine contains three common settings:<\/p>\n<ul>\n<li>DIRS defines a list of directories where the engine should look for template source files, in search order.<\/li>\n<li>APP_DIRS tells whether the engine should look for templates inside installed applications. By convention, when APPS_DIRS is set to True, DjangoTemplates looks for a \\templates subdirectory in each of the INSTALLED_APPS. This allows the template engine to find application templates even if DIRS is empty.<\/li>\n<li>OPTIONS contains backend-specific settings.<\/li>\n<\/ul>\n<h3>Template Directories<\/h3>\n<p>DIRS, by default, is an empty list. To tell Django\u2019s template-loading mechanism where to look for templates, pick a directory where you\u2019d like to store your templates and add it to DIRS, like so:<\/p>\n<pre>    'DIRS': [\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 '\/home\/html\/templates\/site',\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 '\/home\/html\/templates\/default',\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ],<\/pre>\n<p>There are a few things to note:<\/p>\n<ul>\n<li>Unless you are building a very simple program with no apps, you are better off leaving DIRS empty. The default settings file configures APP_DIRS to True, so you are better off having a \u201ctemplates\u201d subdirectory in your Django app.<\/li>\n<li>If you want to have a set of master templates at project root, e.g. mysite\\templates, you do need to set DIRS, like so:<\/li>\n<\/ul>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0 'DIRS': [os.path.join(BASE_DIR, 'templates')],<\/pre>\n<ul>\n<li>Your templates directory does not have to be called templates \u2013 Django doesn\u2019t put any restrictions on the names you use \u2013 but it makes your project structure much easier to understand if you stick to convention.If you don\u2019t want to go with the default, or can\u2019t for some reason, you can specify any directory you want, as long as the directory and templates within that directory are readable by the user account under which your web server runs.<\/li>\n<li>If you\u2019re on Windows, include your drive letter and use Unix-style forward slashes rather than backslashes, as follows:<\/li>\n<\/ul>\n<pre>\u00a0\u00a0 'DIRS':\u00a0\u00a0\u00a0\u00a0\u00a0\n    [\u00a0\u00a0\u00a0\u00a0\u00a0 \n    'C:\/www\/django\/templates',\u00a0\u00a0\u00a0\u00a0 \n   \u00a0]<\/pre>\n<p>As we have not yet created a Django app, I am going to use a very simple configuration to demonstrate how template loading works. First, you will have to set DIRS to [os.path.join(BASE_DIR,&#8217;templates&#8217;)] as per the example above. Your settings file should now look like this:<\/p>\n<pre>TEMPLATES = [\n    {\n        'BACKEND': 'django.template.backends.django.DjangoTemplates',\n        'DIRS': [os.path.join(BASE_DIR,'templates')],\n        'APP_DIRS': True,\n        'OPTIONS': {<\/pre>\n<p><span lang=\"EN-US\">With DIRS set, the next thing to do is create a \\templates directory inside your root \\mysite folder. When you are finished, your folder structure should look like this:<\/span><\/p>\n<pre>\\mysite_project\u00a0\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\mysite\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\mysite\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\templates\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 manage.py<\/pre>\n<p>Next step is to change the view code to use Django\u2019s template-loading functionality rather than hard-coding the template paths. Returning to our current_datetime view, let\u2019s change it like so:<\/p>\n<pre>#mysite\\mysite\\views.py\n\nfrom django.template.loader import get_template\nfrom django.http import HttpResponse\nimport datetime\n\ndef current_datetime(request):\nnow = datetime.datetime.now()\nt = get_template('current_datetime.html')\nhtml = t.render({'current_date': now})\nreturn HttpResponse(html)<\/pre>\n<p>Generally, you will use a Django template loader rather than using the low-level template API as in the previous examples. In this example, we\u2019re using the function django.template.loader.get_template() rather than loading the template from the filesystem manually. The get_template() function takes a template name as its argument, figures out where the template lives on the filesystem, opens that file, and returns a compiled Template object.<\/p>\n<p>Also note the difference between the render() method here and the previous examples. In the previous examples we are calling django.template.Template.render, which requires you to pass in a Context object. get_template() returns a backend-dependent Template from django.template.backends.base.Template, in which the render() method only accepts a dictionary object, not a Context object.<\/p>\n<p>Our template in this example is current_datetime.html, but there\u2019s nothing special about that .html extension. You can give your templates whatever extension makes sense for your application, or you can leave off extensions entirely.<\/p>\n<p>To determine the location of the template on your filesystem, get_template() will look in order:<\/p>\n<ul>\n<li>If APP_DIRS is set to True, and assuming you are using the DTL, it will look for a \/templates directory in the current app.<\/li>\n<li>If it does not find your template in the current app, get_template() combines your template directories from DIRS with the template name that you pass to get_template() and steps through each of them in order until it finds your template. For example, if the first entry in your DIRS is set to &#8216;\/home\/django\/mysite\/templates&#8217;, the above get_template() call would look for the template \/home\/django\/mysite\/templates\/current_datetime.html.<\/li>\n<li>If get_template() cannot find the template with the given name, it raises a TemplateDoesNotExist exception.<\/li>\n<\/ul>\n<p>To see what a template exception looks like, fire up the Django development server again by running python manage.py runserver within your Django project\u2019s directory. Then, point your browser at the page that activates the current_datetime view (e.g., http:\/\/127.0.0.1:8000\/time\/). Assuming your DEBUG setting is set to True and you haven\u2019t yet created a current_datetime.html template, you should see a Django error page highlighting the TemplateDoesNotExist error<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-128542 size-full\" src=\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/5.jpg\" alt=\"\" width=\"852\" height=\"727\" \/><\/p>\n<p>This error page is similar to the one explained earlier, with one additional piece of debugging information: a \u201cTemplate-loader postmortem\u201d section. This section tells you which templates Django tried to load, along with the reason each attempt failed (e.g., \u201cFile does not exist\u201d). This information is invaluable when you\u2019re trying to debug template-loading errors.<\/p>\n<p>To solve our missing template problem, let\u2019s create the current_datetime.html file using the following template code:<\/p>\n<pre># \\mysite_project\\mysite\\templates\\current_datetime.html\n\n&lt;html&gt;\n&lt;body&gt;\nIt is now {{ current_date }}\n&lt;\/body&gt;\n&lt;\/html&gt;\nIt is now {{ current_date }}.<\/pre>\n<p>Save this file to the \\mysite\\templates directory you created previously. Refresh the page in your web browser, and you should see the fully rendered page<\/p>\n<h3>render()<\/h3>\n<p>So far, I\u2019ve shown you how to load a template, fill a Context and return an HttpResponse object with the result of the rendered template. Next step was to optimize it to use get_template() instead of hard-coding templates and template paths.<\/p>\n<p>Django\u2019s developers recognized that, because this is such a common idiom, Django needed a shortcut that could do all this in one line of code. This shortcut is a function called render(), which lives in the module django.shortcuts. Most of the time, you\u2019ll be using render() rather than loading templates and creating Context and HttpResponse objects manually \u2013 however, it\u2019s useful to remember the complete process because it does come in handy for some non-standard use-cases.<\/p>\n<p>Here\u2019s the ongoing current_datetime example rewritten to use render():<\/p>\n<pre>from django.shortcuts import render\nimport datetime\n\ndef current_datetime(request):\nnow = datetime.datetime.now()\nreturn render(request, 'current_datetime.html', {'current_date': now})<\/pre>\n<p>What a difference! Let\u2019s step through the code changes:<\/p>\n<ul>\n<li>We no longer have to import get_template, Template, Context, or HttpResponse. Instead, we import django.shortcuts.render. The import datetime remains.<\/li>\n<li>Within the current_datetime function, we still calculate now, but the template loading, context creation, template rendering, and HttpResponse creation are all taken care of by the render() call. Because render() returns an HttpResponse object, we can simply return that value in the view.<\/li>\n<\/ul>\n<p>The first argument to render() is the request, the second is the name of the template to use. The third argument, if given, should be a dictionary to use in creating a Context for that template. If you don\u2019t provide a third argument, render() will use an empty dictionary.<\/p>\n<h3>Template Subdirectories<\/h3>\n<p>It can get unwieldy to store all of your templates in a single directory. You might like to store templates in subdirectories of your template directory, and that\u2019s fine.<\/p>\n<p>In fact, I recommend doing so; some more advanced Django features (such as the generic views system) expect this template layout as a default convention. It also gives your templates their own namespace, the utility of which we will explore later in the book when we start building Django apps.<\/p>\n<p>Storing templates in subdirectories of your template directory is easy. In your calls to get_template(), just include the subdirectory name and a slash before the template name, like so:<\/p>\n<pre>\u00a0\u00a0\u00a0 t = get_template('dateapp\/current_datetime.html')<\/pre>\n<p>Because render() is a small wrapper around get_template(), you can do the same thing with the second argument to render(), like this:<\/p>\n<pre>    return render(request, 'dateapp\/current_datetime.html', {'current_date': now})<\/pre>\n<p>There\u2019s no limit to the depth of your subdirectory tree. Feel free to use as many subdirectories as you like. As with all other methods in this chapter, Windows users must remember to use forwardslashes (\/) in path names, not backslashes (\\).<\/p>\n<h3>The include Template Tag<\/h3>\n<p>Now that we\u2019ve covered the template-loading mechanism, we can introduce a built-in template tag that takes advantage of it: {% include %}.<\/p>\n<p>This tag allows you to include the contents of another template. The argument to the tag should be the name of the template to include, and the template name can be either a variable or a hard-coded (quoted) string, in either single or double quotes.<\/p>\n<p>Anytime you have the same code in multiple templates, consider using an {% include %} to remove the duplication. These two examples include the contents of the template nav.html. The examples are equivalent and illustrate that either single or double quotes are allowed:<\/p>\n<pre>{% include 'nav.html' %}\n{% include \"nav.html\" %}<\/pre>\n<p>This example includes the contents of the template includes\/nav.html:<\/p>\n<pre>{% include 'includes\/nav.html' %}<\/pre>\n<p>This example includes the contents of the template whose name is contained in the variable template_name:<\/p>\n<pre>{% include template_name %}<\/pre>\n<p>the {% include %} tag also allows relative paths:<\/p>\n<pre>{% include '.\/nav.html' %}\n{% include '..\/nav_base.html' %}\n\n\n<\/pre>\n<p>As in get_template(), the file name of the template is determined by either adding the path to the \\templates directory in the current Django app (if APPS_DIR is True) or by adding the template directory from DIRS to the requested template name. Included templates are evaluated with the context of the template that\u2019s including them.<\/p>\n<p>For example, consider these two templates:<\/p>\n<pre># mypage.html\n\n&lt;html&gt;\n&lt;body&gt;\n{% include \"includes\/nav.html\" %}\n&lt;h1&gt;{{ title }}&lt;\/h1&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n\n# includes\/nav.html\n\nYou are in: {{ current_section }}<\/pre>\n<p>If you render mypage.html with a context containing current_section, then the variable will be available in the \u201cincluded\u201d template, as you would expect.<\/p>\n<p>If, in an {% include %} tag, a template with the given name isn\u2019t found, Django will do one of two things:<\/p>\n<ul>\n<li>If DEBUG is set to True, you\u2019ll see the TemplateDoesNotExist exception on a Django error page.<\/li>\n<li>If DEBUG is set to False, the tag will fail silently, displaying nothing in the place of the tag.<\/li>\n<\/ul>\n<p>There is no shared state between included templates \u2013 each include is a completely independent rendering process. Blocks are evaluated before they are included. This means that a template that includes blocks from another will contain blocks that have already been evaluated and rendered \u2013 not blocks that can be overridden by, for example, an extending template.<\/p>\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/certified-django-developer\/\" target=\"_blank\" rel=\"noopener noreferrer\">Back to Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You\u2019ve learned the basics of using the template system; now let\u2019s use this knowledge to create a view. Recall the current_datetime view in mysite.views, which we started in the previous chapter. Here\u2019s what it looks like: from django.http import HttpResponse import datetime \u00a0def current_datetime(request): \u00a0\u00a0\u00a0 now = datetime.datetime.now() \u00a0\u00a0\u00a0 html = &#8220;&lt;html&gt;&lt;body&gt;It is now %s.&lt;\/body&gt;&lt;\/html&gt;&#8221;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8655],"tags":[8667],"class_list":["post-75290","page","type-page","status-publish","hentry","category-django-web-development","tag-using-templates-in-views"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using Templates in Views - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using Templates in Views - Tutorial\" \/>\n<meta property=\"og:description\" content=\"You\u2019ve learned the basics of using the template system; now let\u2019s use this knowledge to create a view. Recall the current_datetime view in mysite.views, which we started in the previous chapter. Here\u2019s what it looks like: from django.http import HttpResponse import datetime \u00a0def current_datetime(request): \u00a0\u00a0\u00a0 now = datetime.datetime.now() \u00a0\u00a0\u00a0 html = &quot;&lt;html&gt;&lt;body&gt;It is now %s.&lt;\/body&gt;&lt;\/html&gt;&quot;...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:47:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/5.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/\",\"name\":\"Using Templates in Views - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/5.jpg\",\"datePublished\":\"2020-01-18T08:42:25+00:00\",\"dateModified\":\"2024-04-12T08:47:12+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/#primaryimage\",\"url\":\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/5.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/5.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using Templates in Views\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using Templates in Views - Tutorial","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:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/","og_locale":"en_US","og_type":"article","og_title":"Using Templates in Views - Tutorial","og_description":"You\u2019ve learned the basics of using the template system; now let\u2019s use this knowledge to create a view. Recall the current_datetime view in mysite.views, which we started in the previous chapter. Here\u2019s what it looks like: from django.http import HttpResponse import datetime \u00a0def current_datetime(request): \u00a0\u00a0\u00a0 now = datetime.datetime.now() \u00a0\u00a0\u00a0 html = \"&lt;html&gt;&lt;body&gt;It is now %s.&lt;\/body&gt;&lt;\/html&gt;\"...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:47:12+00:00","og_image":[{"url":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/5.jpg","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/","name":"Using Templates in Views - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/#primaryimage"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/#primaryimage"},"thumbnailUrl":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/5.jpg","datePublished":"2020-01-18T08:42:25+00:00","dateModified":"2024-04-12T08:47:12+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/#primaryimage","url":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/5.jpg","contentUrl":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/5.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/using-templates-in-views\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Using Templates in Views"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=75290"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75290\/revisions"}],"predecessor-version":[{"id":83329,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75290\/revisions\/83329"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=75290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=75290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=75290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}