{"id":75181,"date":"2020-01-18T12:22:32","date_gmt":"2020-01-18T06:52:32","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=75181"},"modified":"2024-04-12T14:17:11","modified_gmt":"2024-04-12T08:47:11","slug":"your-first-view-dynamic-content","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-view-dynamic-content\/","title":{"rendered":"Your First View: Dynamic Content"},"content":{"rendered":"<p>As our first goal, let\u2019s create a Web page that displays the current date and time. This is a good example of a <em>dynamic<\/em> Web page, because the contents of the page are not static\u2014rather, the contents change according to the result of a computation (in this case, a calculation of the current time). This simple example doesn\u2019t involve a database or any sort of user input\u2014just the output of your server\u2019s internal clock.<\/p>\n<p>To create this page, we\u2019ll write a <em>view function<\/em>. A view function, or <em>view<\/em> for short, is simply a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image \u2026 or anything, really. The view itself contains whatever arbitrary logic is necessary to return that response. This code can live anywhere you want, as long as it\u2019s on your Python path. There\u2019s no other requirement\u2014no \u201cmagic,\u201d so to speak. For the sake of putting the code <em>somewhere<\/em>, let\u2019s create a file called views.py in the mysite directory, which you created in the previous chapter. Here\u2019s a view that returns the current date and time, as an HTML document:<\/p>\n<pre>from django.http import HttpResponse\n\nimport datetime\n\ndef current_datetime(request):&nbsp;&nbsp;\n\n&nbsp; &nbsp; now = datetime.datetime.now()&nbsp;&nbsp;&nbsp; \n\n&nbsp;&nbsp;&nbsp; html = \"&lt;html&gt;&lt;body&gt;It is now %s.&lt;\/body&gt;&lt;\/html&gt;\" % now&nbsp;&nbsp;&nbsp; \n\n&nbsp;&nbsp;&nbsp; return HttpResponse(html)\n\n\n<\/pre>\n<p>Let\u2019s step through this code one line at a time:<\/p>\n<ul>\n<li>First, we import the class HttpResponse, which lives in the django.http module.<\/li>\n<li>Then we import the datetime module from Python\u2019s standard library, the set of useful modules that comes with Python. The datetime module contains several functions and classes for dealing with dates and times, including a function that returns the current time.<\/li>\n<li>Next, we define a function called current_datetime. This is the view function. Each view function takes an HttpRequest object as its first parameter, which is typically named request.<\/li>\n<li>Note that the name of the view function doesn\u2019t matter; it doesn\u2019t have to be named in a certain way in order for Django to recognize it. We\u2019re calling it current_datetime here, because that name clearly indicates what it does, but it could just as well be named super_duper_awesome_current_time, or something equally revolting. Django doesn\u2019t care. The next section explains how Django finds this function.<\/li>\n<li>The first line of code within the function calculates the current date\/time, as a datetime.datetime object, and stores that as the local variable now.<\/li>\n<li>The second line of code within the function constructs an HTML response using Python\u2019s format-string capability. The %s within the string is a placeholder, and the percent sign after the string means \u201cReplace the %s with the value of the variable now.\u201d (Yes, the HTML is invalid, but we\u2019re trying to keep the example simple and short.)<\/li>\n<li>Finally, the view returns an HttpResponse object that contains the generated response. Each view function is responsible for returning an HttpResponse object. (There are exceptions, but we\u2019ll get to those later.)<\/li>\n<\/ul>\n<p><strong>Django\u2019s Time Zone<\/strong> &#8211; Django includes a TIME_ZONE setting that defaults to America\/Chicago. This probably isn\u2019t where you live, so you might want to change it in your settings.py.<\/p>\n<p>As our first goal, let\u2019s create a web page that outputs that famous example message: \u201cHello world.\u201d If you were publishing a simple \u201cHello world\u201d web page without a web framework, you\u2019d simply type \u201cHello world\u201d into a text file, call it \u201chello.html\u201c, and upload it to a directory on a web server somewhere. Notice in that process you\u2019ve specified two key pieces of information about that web page: its contents (the string &#8220;Hello world&#8221;) and its URL (e.g. http:\/\/www.example.com\/hello.html).<\/p>\n<h3>Your First View<\/h3>\n<p>With Django, you specify those same two things, but in a different way. The contents of the page are produced by a view function, and the URL is specified in a URLconf. First, let\u2019s write our \u201cHello world\u201d view function.<\/p>\n<p>Within the inner mysite directory that we created in the last chapter, create an empty file called views.py. This Python module will contain our views for this chapter. Make sure you put the file in the inner mysite directory, i.e. the \\mysite\\mysite\\ directory, not the directory containing manage.py.<\/p>\n<p>Our \u201cHello world\u201d view is simple. Here\u2019s the entire function, plus import statements, which you should type into the views.py file:<\/p>\n<pre>from django.http import HttpResponsedef hello(request):&nbsp;&nbsp;&nbsp; return HttpResponse(\"Hello world\")<\/pre>\n<p>Let\u2019s step through this code one line at a time:<\/p>\n<ul>\n<li>First, we import the class HttpResponse, which lives in the django.http module. We need to import this class because it\u2019s used later in our code.<\/li>\n<li>Next, we define a function called hello \u2013 the view function.<\/li>\n<li>Each view function takes at least one parameter, called request by convention. This is an object that contains information about the current web request that has triggered this view, and is an instance of the class django.http.HttpRequest.<\/li>\n<\/ul>\n<p>In this example, we don\u2019t do anything with request, but it must be the first parameter of the view nonetheless. Note that the name of the view function doesn\u2019t matter; it doesn\u2019t have to be named in a certain way in order for Django to recognize it. We\u2019re calling it hello here, because that name clearly indicates the gist of the view, but it could just as well be named hello_wonderful_beautiful_world, or something equally revolting.<\/p>\n<p>The next section, \u201cYour First URLconf\u201d, will shed light on how Django finds this function. The function is a simple one-liner: it merely returns an HttpResponse object that has been instantiated with the text &#8220;Hello world&#8221;. The main lesson here is this: a view is just a Python function that takes an HttpRequest as its first parameter and returns an instance of HttpResponse. In order for a Python function to be a Django view, it must do these two things. (There are exceptions, but we\u2019ll get to those later.)<\/p>\n\n\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/certified-django-developer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Back Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As our first goal, let\u2019s create a Web page that displays the current date and time. This is a good example of a dynamic Web page, because the contents of the page are not static\u2014rather, the contents change according to the result of a computation (in this case, a calculation of the current time). This&#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":[2599],"class_list":["post-75181","page","type-page","status-publish","hentry","category-django-web-development","tag-dynamic-content"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Your First View: Dynamic Content - 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\/your-first-view-dynamic-content\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Your First View: Dynamic Content - Tutorial\" \/>\n<meta property=\"og:description\" content=\"As our first goal, let\u2019s create a Web page that displays the current date and time. This is a good example of a dynamic Web page, because the contents of the page are not static\u2014rather, the contents change according to the result of a computation (in this case, a calculation of the current time). This...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-view-dynamic-content\/\" \/>\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:11+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 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\/your-first-view-dynamic-content\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-view-dynamic-content\/\",\"name\":\"Your First View: Dynamic Content - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-18T06:52:32+00:00\",\"dateModified\":\"2024-04-12T08:47:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-view-dynamic-content\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-view-dynamic-content\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-view-dynamic-content\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Your First View: Dynamic Content\"}]},{\"@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":"Your First View: Dynamic Content - 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\/your-first-view-dynamic-content\/","og_locale":"en_US","og_type":"article","og_title":"Your First View: Dynamic Content - Tutorial","og_description":"As our first goal, let\u2019s create a Web page that displays the current date and time. This is a good example of a dynamic Web page, because the contents of the page are not static\u2014rather, the contents change according to the result of a computation (in this case, a calculation of the current time). This...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-view-dynamic-content\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:47:11+00:00","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-view-dynamic-content\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-view-dynamic-content\/","name":"Your First View: Dynamic Content - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-18T06:52:32+00:00","dateModified":"2024-04-12T08:47:11+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-view-dynamic-content\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/your-first-view-dynamic-content\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-view-dynamic-content\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Your First View: Dynamic Content"}]},{"@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\/75181","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=75181"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75181\/revisions"}],"predecessor-version":[{"id":83317,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75181\/revisions\/83317"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=75181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=75181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=75181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}