<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sundaram on Medium]]></title>
        <description><![CDATA[Stories by Sundaram on Medium]]></description>
        <link>https://medium.com/@sundaram.2911?source=rss-498391800430------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*19z1q5rv36HBxllD</url>
            <title>Stories by Sundaram on Medium</title>
            <link>https://medium.com/@sundaram.2911?source=rss-498391800430------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 02:38:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sundaram.2911/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[SvelteKit: Where Speed Meets Simplicity in Web Development(Part-2)]]></title>
            <link>https://medium.com/@sundaram.2911/sveltekit-where-speed-meets-simplicity-in-web-development-part-2-d59b8b872a19?source=rss-498391800430------2</link>
            <guid isPermaLink="false">https://medium.com/p/d59b8b872a19</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[startup]]></category>
            <dc:creator><![CDATA[Sundaram]]></dc:creator>
            <pubDate>Mon, 02 Sep 2024 07:06:33 GMT</pubDate>
            <atom:updated>2024-09-02T07:06:33.548Z</atom:updated>
            <content:encoded><![CDATA[<p>In the previous blog, we covered the basics of Sveltekit (features, project structure ), in this blog we will be implementing a simple login page with validation using Zod, a library used for validation.</p><p>To grasp the basic concepts, <a href="https://medium.com/@sundaram.2911/sveltekit-where-speed-meets-simplicity-in-web-development-cc17503a4815">check out this story!</a></p><h4>Initial Setup</h4><ul><li>Navigate to the desired directory where you want to initialize your project, then execute the following commands:</li></ul><blockquote>Creating a Sveltekit project from scratch has been covered in the previous story <a href="https://medium.com/@sundaram.2911/sveltekit-where-speed-meets-simplicity-in-web-development-cc17503a4815">here</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*2IDS5mWcrO9xMsrv" /></figure><ul><li>Go ahead and open the project in any code editor, I will use VS Code.</li><li>Please refer to the <a href="https://medium.com/@sundaram.2911/sveltekit-where-speed-meets-simplicity-in-web-development-cc17503a4815">previous blog </a>for a detailed explanation of the project structure. It provides all the necessary clarifications on that aspect.</li></ul><h4>Configuration &amp; Implementation:</h4><p>Verify if the project setup has been done, correctly. To run the development server enter the following commands in the project directory:</p><pre>npm install<br>npm run dev</pre><ul><li>After running the commands you will get something like this if the setup is correct:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/0*yKqVKTiEnWLT42ng" /></figure><ul><li>if you head to localhost:5173, you will see something like this:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rHIuxHt6WAEmYTRQ" /></figure><ul><li>Now we start editing the +page.svelte file in the src/routes, this represents the home route.</li><li>The basic syntax of a svelte-kit code is as shown below:</li></ul><pre>&lt;script&gt;<br>    // This section includes JavaScript code for logic and data handling<br>&lt;/script&gt;<br><br>&lt;main&gt;<br>    &lt;!-- This section contains HTML markup for the page structure --&gt;<br>&lt;/main&gt;<br><br>&lt;style&gt;<br>    /* This section holds CSS styles for the component&#39;s presentation */<br>&lt;/style&gt;</pre><ul><li>For the login form, we will be validating the email and password for which, we will be using Zod, third-party library for validation, install it using this command:</li></ul><pre>npm i zod</pre><ul><li>Now we write a script to route the request from the home (current directory) to the /signup , we will be adding a simple &lt;a&gt; for this and will use goto function that helps in routing from one page to another.</li></ul><h4>Main Page:</h4><ul><li>Add the following piece of code to the <strong>&lt;script&gt;&lt;/script&gt;</strong>:</li></ul><pre>&lt;script&gt;<br>  import { goto } from &quot;$app/navigation&quot;;<br>  const navigateToSignup = () =&gt; {<br>    goto(&quot;/signup&quot;);<br>  };<br>&lt;/script&gt;</pre><blockquote>Summarizing the above code:</blockquote><p>This script sets up a function to navigate users to the /signup page. It imports the goto function from $app/navigation and defines navigateToSignup, which uses goto to redirect the user.</p><p>Next, we’ll add the markup code for this part:</p><pre>&lt;main&gt;<br>  &lt;h1&gt;Welcome to Our Application!&lt;/h1&gt;<br>  &lt;p&gt;<br>    We are excited to have you here. Click the button below to get started with<br>    signing up.<br>  &lt;/p&gt;<br>  &lt;button on:click={navigateToSignup}&gt;Go to Signup&lt;/button&gt;<br>&lt;/main&gt;</pre><blockquote>Summarizing the above code:</blockquote><p>The following code snippet creates a welcoming main page for the application. It includes a heading, a brief introductory paragraph, and a button. <strong>Clicking the button will navigate the user to the signup page.</strong></p><p>Next, we have the CSS code that adds basic styling to the page:</p><pre>&lt;style&gt;<br>  main {<br>    display: flex;<br>    flex-direction: column;<br>    align-items: center;<br>    justify-content: center;<br>    height: 100vh;<br>    text-align: center;<br>  }<br><br>  button {<br>    padding: 10px 20px;<br>    font-size: 16px;<br>    cursor: pointer;<br>    margin-top: 20px;<br>  }<br>&lt;/style&gt;</pre><p>After adding all the above code , the complete script should look something like this:</p><pre>&lt;script&gt;<br>  import { goto } from &quot;$app/navigation&quot;;<br><br>  // Function to navigate to the /signup page<br>  const navigateToSignup = () =&gt; {<br>    goto(&quot;/signup&quot;);<br>  };<br>&lt;/script&gt;<br><br>&lt;main&gt;<br>  &lt;h1&gt;Welcome to Our Application!&lt;/h1&gt;<br>  &lt;p&gt;<br>    We are excited to have you here. Click the button below to get started with<br>    signing up.<br>  &lt;/p&gt;<br>  &lt;button on:click={navigateToSignup}&gt;Go to Signup&lt;/button&gt;<br>&lt;/main&gt;<br><br>&lt;style&gt;<br>  main {<br>    display: flex;<br>    flex-direction: column;<br>    align-items: center;<br>    justify-content: center;<br>    height: 100vh;<br>    text-align: center;<br>  }<br><br>  button {<br>    padding: 10px 20px;<br>    font-size: 16px;<br>    cursor: pointer;<br>    margin-top: 20px;<br>  }<br>&lt;/style&gt;</pre><p>After adding the above script, head to the browser, the page should look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*AQpUeDIyRysZYwvs.png" /></figure><h4>Signup Page</h4><ul><li>Head over to the src/routes and create a new folder called “signup”, this represents the signup route(<strong>/signup)</strong>. In the folder create a new file called <strong>“+page.svelte”, </strong>this represents the main page of the signup route.</li><li>First, we include the JavaScript part that has the imports and some validation functions, include the following in the <strong>&lt;Script&gt;&lt;/Script&gt;</strong> part:</li></ul><pre>&lt;script&gt;<br>  import { goto } from &quot;$app/navigation&quot;;<br>  import { z } from &quot;zod&quot;;<br><br>  let email = &quot;&quot;;<br>  let password = &quot;&quot;;<br>  const emailSchema = z<br>    .string()<br>    .email({ message: &quot;Kindly enter a valid Email!&quot; });<br><br>  const passwordSchema = z<br>    .string()<br>    .min(8, {<br>      message: &quot;The password should contain a minimum of 8 characters!&quot;,<br>    })<br>    .max(10, {<br>      message: &quot;The password should contain a maximum of 10 characters!&quot;,<br>    });<br>  const validate = () =&gt; {<br>    try {<br>      emailSchema.parse(email);<br>      passwordSchema.parse(password);<br>      return true;<br>    } catch (error) {<br>      if (error.errors) {<br>        error.errors.forEach((err) =&gt; alert(err.message));<br>      }<br>      return false;<br>    }<br>  };<br><br>  const handleSubmit = (event) =&gt; {<br>    event.preventDefault();<br>    if (validate()) {<br>      alert(&quot;Response Captured!&quot;);<br>    }<br>  };<br>&lt;/script&gt;</pre><blockquote><strong>Summarising the above code:</strong></blockquote><ul><li><strong>Imports:</strong> The script imports goto from $app/navigation and <strong>zod</strong></li><li><strong>State:</strong> Initializes email and password variables.</li><li><strong>Validation:</strong> Defines emailSchema and passwordSchema to validate email and password inputs.</li><li><strong>Validation Function:</strong> The validate function checks inputs against schemas and shows alerts for validation errors.</li><li><strong>Submit Handler:</strong> handleSubmit prevents default form submission and shows a success alert if validation passes.</li></ul><p>Next, we look at structuring the page <strong>(HTML)</strong> part.</p><pre>&lt;main&gt;<br>  &lt;div class=&quot;card&quot;&gt;<br>    &lt;h2&gt;Signup&lt;/h2&gt;<br>    &lt;form on:submit={handleSubmit}&gt;<br>      &lt;div class=&quot;form-group&quot;&gt;<br>        &lt;label for=&quot;email&quot;&gt;Email:&lt;/label&gt;<br>        &lt;input<br>          type=&quot;email&quot;<br>          id=&quot;email&quot;<br>          bind:value={email}<br>          placeholder=&quot;Enter your email&quot;<br>          required<br>        /&gt;<br>      &lt;/div&gt;<br>      &lt;div class=&quot;form-group&quot;&gt;<br>        &lt;label for=&quot;password&quot;&gt;Password:&lt;/label&gt;<br>        &lt;input<br>          type=&quot;password&quot;<br>          id=&quot;password&quot;<br>          bind:value={password}<br>          placeholder=&quot;Enter your password&quot;<br>          required<br>        /&gt;<br>      &lt;/div&gt;<br>      &lt;button type=&quot;submit&quot;&gt;Signup&lt;/button&gt;<br>    &lt;/form&gt;<br>  &lt;/div&gt;<br>&lt;/main&gt;</pre><blockquote><strong>Summarising the above piece of code:</strong></blockquote><p>This code snippet defines the layout and functionality for a signup page. It features a card-style container with a heading and a form. The form includes required fields for entering an email and a password, and it includes a function to handle form submission. A submit button is provided to submit the form.</p><p>Next, we have the CSS that provides basic styling and positioning for the page, including the card and form elements.</p><pre>&lt;style&gt;<br>  main {<br>    display: flex;<br>    align-items: center;<br>    justify-content: center;<br>    height: 100vh;<br>    background-color: #f0f0f0;<br>  }<br><br>  .card {<br>    background: white;<br>    padding: 2rem;<br>    border-radius: 8px;<br>    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);<br>    width: 100%;<br>    max-width: 400px;<br>    text-align: center;<br>  }<br><br>  .form-group {<br>    margin-bottom: 1.5rem;<br>    text-align: left;<br>  }<br><br>  label {<br>    display: block;<br>    margin-bottom: 0.5rem;<br>    font-weight: bold;<br>  }<br><br>  input {<br>    width: 100%;<br>    padding: 0.75rem;<br>    font-size: 1rem;<br>    border-radius: 4px;<br>    border: 1px solid #ccc;<br>  }<br><br>  button {<br>    width: 100%;<br>    padding: 0.75rem;<br>    font-size: 1rem;<br>    cursor: pointer;<br>    background-color: #007bff;<br>    color: white;<br>    border: none;<br>    border-radius: 4px;<br>  }<br><br>  button:hover {<br>    background-color: #0056b3;<br>  }<br>&lt;/style&gt;</pre><p>After adding all the above code, the script should look like this:</p><pre>&lt;script&gt;<br>  import { goto } from &quot;$app/navigation&quot;;<br>  import { z } from &quot;zod&quot;;<br><br>  let email = &quot;&quot;;<br>  let password = &quot;&quot;;<br>  const emailSchema = z<br>    .string()<br>    .email({ message: &quot;Kindly enter a valid Email!&quot; });<br><br>  const passwordSchema = z<br>    .string()<br>    .min(8, {<br>      message: &quot;The password should contain a minimum of 8 characters!&quot;,<br>    })<br>    .max(10, {<br>      message: &quot;The password should contain a maximum of 10 characters!&quot;,<br>    });<br>  const validate = () =&gt; {<br>    try {<br>      emailSchema.parse(email);<br>      passwordSchema.parse(password);<br>      return true;<br>    } catch (error) {<br>      if (error.errors) {<br>        error.errors.forEach((err) =&gt; alert(err.message));<br>      }<br>      return false;<br>    }<br>  };<br><br>  const handleSubmit = (event) =&gt; {<br>    event.preventDefault();<br>    if (validate()) {<br>      alert(&quot;Response Captured!&quot;);<br>    }<br>  };<br>&lt;/script&gt;<br><br>&lt;main&gt;<br>  &lt;div class=&quot;card&quot;&gt;<br>    &lt;h2&gt;Signup&lt;/h2&gt;<br>    &lt;form on:submit={handleSubmit}&gt;<br>      &lt;div class=&quot;form-group&quot;&gt;<br>        &lt;label for=&quot;email&quot;&gt;Email:&lt;/label&gt;<br>        &lt;input<br>          type=&quot;email&quot;<br>          id=&quot;email&quot;<br>          bind:value={email}<br>          placeholder=&quot;Enter your email&quot;<br>          required<br>        /&gt;<br>      &lt;/div&gt;<br>      &lt;div class=&quot;form-group&quot;&gt;<br>        &lt;label for=&quot;password&quot;&gt;Password:&lt;/label&gt;<br>        &lt;input<br>          type=&quot;password&quot;<br>          id=&quot;password&quot;<br>          bind:value={password}<br>          placeholder=&quot;Enter your password&quot;<br>          required<br>        /&gt;<br>      &lt;/div&gt;<br>      &lt;button type=&quot;submit&quot;&gt;Signup&lt;/button&gt;<br>    &lt;/form&gt;<br>  &lt;/div&gt;<br>&lt;/main&gt;<br><br>&lt;style&gt;<br>  main {<br>    display: flex;<br>    align-items: center;<br>    justify-content: center;<br>    height: 100vh;<br>    background-color: #f0f0f0;<br>  }<br><br>  .card {<br>    background: white;<br>    padding: 2rem;<br>    border-radius: 8px;<br>    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);<br>    width: 100%;<br>    max-width: 400px;<br>    text-align: center;<br>  }<br><br>  .form-group {<br>    margin-bottom: 1.5rem;<br>    text-align: left;<br>  }<br><br>  label {<br>    display: block;<br>    margin-bottom: 0.5rem;<br>    font-weight: bold;<br>  }<br><br>  input {<br>    width: 100%;<br>    padding: 0.75rem;<br>    font-size: 1rem;<br>    border-radius: 4px;<br>    border: 1px solid #ccc;<br>  }<br><br>  button {<br>    width: 100%;<br>    padding: 0.75rem;<br>    font-size: 1rem;<br>    cursor: pointer;<br>    background-color: #007bff;<br>    color: white;<br>    border: none;<br>    border-radius: 4px;<br>  }<br><br>  button:hover {<br>    background-color: #0056b3;<br>  }<br>&lt;/style&gt;</pre><p>After adding the code, head over to localhost:5173/signup, it should look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0vmbMW11KHyVhh4X.png" /></figure><p>With the setup done, we now run the project and see if everything is working as expected, below I have inserted some screenshots of the output output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/0*qpm0tjpZ17aZbmbb.png" /><figcaption>This screen indicates an error in the email input</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/0*Q_gS7oBE5-uMrbBT.png" /><figcaption>This screen indicates an alert stating that the password should have a minimum of 8 characters</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/643/0*0XXetzCbxK9GReR7.png" /><figcaption>This screen indicates an error stating password can have a maximum of 8 characters</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/0*qg0uEK38q9TkAlra.png" /><figcaption>This screen represents a successful response.</figcaption></figure><blockquote>This article covered the implementation part of SvelteKit using a simple example where we had a main page and we added a signup page where users can navigate from the main page. The signup page features a simple card component with email and password input fields that are validated using custom schema validation with Zod.</blockquote><p><a href="https://github.com/Sundaram-2001/hashnode-project">Find the completed code here!</a></p><p>In the next blog, we will be covering some more concepts related to Sveltekit, until then stay tuned!</p><p><a href="https://x.com/sundaramtwts">Find me on X!</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/547/0*fnrwIhNkQcLLK15l.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d59b8b872a19" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SvelteKit: Where Speed Meets Simplicity in Web Development]]></title>
            <link>https://medium.com/@sundaram.2911/sveltekit-where-speed-meets-simplicity-in-web-development-cc17503a4815?source=rss-498391800430------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc17503a4815</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[sveltekit]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Sundaram]]></dc:creator>
            <pubDate>Thu, 29 Aug 2024 09:08:42 GMT</pubDate>
            <atom:updated>2024-08-29T09:08:42.236Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/477/1*Mu9OgQomTYp05UmVxV4DfQ.png" /></figure><h3>What is Sveltekit?</h3><p>Sveltekit is built on Svelte, a UI framework that allows developers to write frontend code in the languages they already know — HTML, CSS, JavaScript.</p><h3>Why Sveltekit?</h3><ul><li>Sveltekit leverages the Svelte compiler to produce highly optimized minimal JavaScript, resulting in faster load times, and better user experience. Along with this Sveltekit offers file-based routing, SSR, and SSG making SevelteKit a powerful yet simple tool for creating modern web applications.</li><li>Unlike other frontend libraries like React which rely on the virtual DOM to manage UI changes, Sveltekit works directly with the actual DOM to manage UI changes, making Sveltekit faster.</li></ul><h3>Getting Started: Creating Your First SvelteKit Project</h3><p>Here, I will show how to get started with Sveltekit by creating a simple skeleton project and then explaining the project structure of Sveltekit.</p><blockquote><strong>Having Nodejs installed is a must.</strong></blockquote><ul><li>Head over to the directory where you wish to create the project</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/0*9I4UmU8ItoHuiwUM" /></figure><ul><li>Once you are in the directory, enter the following command to create a Sveltekit project:</li></ul><pre>npm create svelte@latest &lt;project_name&gt;</pre><ul><li>After this, you will see a list consisting of the following options:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1N8ceXtju1YBFso5" /></figure><ul><li>From the above options we choose ‘<strong>Skeleton Project</strong>’, this will create the basic introductory files for creating a Sveltekit project.</li><li>Next, we get a list of options to choose from, I am using <strong><em>JavaScript</em></strong> and I chose ‘<strong>No’ </strong>for the options below.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/387/0*1lxao1Ldy6EIpgHr" /></figure><ul><li>Next, the prompt asks if we want linting for the project, for that we can choose to add ESLinting for our project. After executing the above steps we will get something like this, as shown in the image below:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/0*0kwJ3oyXP18RNYGi" /></figure><ul><li>After executing the above commands and starting the dev server, the welcome page should look something like this:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*76KJA05CJPT9qPRU" /></figure><h3>Project Structure of a Sveltekit App:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/0*8BouZ0QOH6NytBDu" /></figure><ul><li><strong>src:</strong> The src directory is the core part of the application, this is where all of the code goes.</li><li><strong>src/lib:</strong> The src/lib directory in SvelteKit contains reusable components, utility functions, and shared modules. For example, header.svelte here can be reused across multiple pages, promoting consistency and reducing code duplication.</li><li><strong>src/routes: </strong>SvelteKit uses file-based routing, where directories represent routes. For example, in the src/routes directory in the flowchart above, +page.svelte contains the content for the home route and +layout.svelte defines the layout for that route and its subdirectories. If you define a layout in /home, it will also apply to any subdirectories like /home/contact and /home/about (if you create one).</li><li><strong>static:</strong> The static directory has static assets such as fonts, images, etc. that you may use in the web application.</li><li><strong>tests:</strong> If you are using a testing framework to test your application then all the test files are included in the ‘tests’ directory.</li><li><strong>svelte.config.js: </strong>The svelte.config.js file consists of the configuration file for the sveltekit application.</li><li><strong>package.json:</strong> The package.json consists of all the dependencies being used in the project.</li><li><strong>vite.config.js:</strong> Internally sveltekit uses vite as a build tool, this file contains the configuration for vite.</li></ul><p><strong>We will see Sveltekit in action in the upcoming blogs, stay tuned!</strong></p><p><a href="https://x.com/sundaramtwts?t=OHZle9eb9m8ahUPDYyqidw&amp;s=09">Find me on X!</a></p><p><a href="https://x.com/sundaramtwts/status/1821805056250773615?t=GZ1jEW4zmI1X9xOVedUGCg&amp;s=19">Check out this project I’m working on!</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/495/0*PF4pnQ8Tb-edVmy2" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc17503a4815" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[An introduction to Django]]></title>
            <link>https://medium.com/@sundaram.2911/an-introduction-to-django-b17b51e3a7dc?source=rss-498391800430------2</link>
            <guid isPermaLink="false">https://medium.com/p/b17b51e3a7dc</guid>
            <category><![CDATA[beginner]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[backend-development]]></category>
            <dc:creator><![CDATA[Sundaram]]></dc:creator>
            <pubDate>Wed, 28 Feb 2024 18:00:56 GMT</pubDate>
            <atom:updated>2024-02-28T18:00:56.236Z</atom:updated>
            <content:encoded><![CDATA[<p>Pre-requisite: Basic knowledge of python.</p><p>Django is a python web framework that enables rapid development of secure and maintainable websites and is open source.</p><p>When a HTTPS request is received then application works out what is needed based on the URL and then as required the application may read or write information from database or perform any other task performed to satisfy the client request.</p><p>Pictorial representation of working of Django(Source :MDN Web Docs)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*QEOFVBkHGRISAY1R" /></figure><ul><li><strong>urls.py:</strong> Instead of defining all the routes/URL patters in a single function is possible but it is much more maintainable to define those patterns in a single file , in Django that file is called <a href="http://urls.py/">URLs.py</a> . It basically helps in routing all the URL requests to specific views (discussed in later sections) to fulfill user requests. It consists of various patterns having strings , numbers , etc. in the URL and are passed to the view function as data as the URL may consist of special parameters that might help in fetching data for sending response to the client.</li><li><strong>Views</strong>: Views in Django are request handler functions that receives HTTP request and returns HTTP response. Views access data through models to satisfy requests from clients.</li><li><strong>Models</strong> : Models in Django can be simply defined as a database where the structure of application data is defined and it provides mechanisms to Create , Read , Update and Delete the data in the application for fulfilling the user request.</li><li><strong>Templates :</strong> Templates are files (like HTML files) with placeholders that is populated by actual content. Views create dynamic HTML files using templates and populate it with data from models. A template can be used to define the structure of any file , not only HTML!</li></ul><h3>Routing Requests (urls.py)</h3><p>So , when a request is received from the front-end then based on the URL it is routed to a specific view using urlmapper , it basically defines a set of URLs and the corresponding views to which the request will be routed to that specific view. All of this is stored in <a href="http://urls.py/">urls.py</a></p><p>Below is an example of how <a href="http://urls.py/">urls.py</a> looks like:</p><pre>urlpatterns = [<br>    path(&#39;admin/&#39;, admin.site.urls),<br>    path(&#39;book/&lt;int:id&gt;/&#39;, views.book_detail, name=&#39;book_detail&#39;),<br>    path(&#39;catalog/&#39;, include(&#39;catalog.urls&#39;)),<br>    re_path(r&#39;^([0-9]+)/$&#39;, views.best),<br>]</pre><p>urlpatterns in the above example is a python list that consists of the path/re_path function , that basically takes two arguments. The first argument is a pattern (route) that will be matched , the pattern may consist of some other parameters that are passed in the URL and are then captured and sent to views for fetching some data . The second argument is also a function that is in a module called views <strong>(views.py)</strong> , where the function is stored . In the example above , for the path <strong>book/&lt;int:id&gt;/ </strong>the request is routed to the views module and a function called <strong>book_detail</strong></p><h3>Processing Requests(views.py)</h3><p><strong>Views.py</strong> is responsible for handling the HTTP response and sending back the HTTP request , in between <strong>views.py</strong> will interact with the database to manipulate/fetch the data and then send back the response to the client.</p><p>The example below shows a very simple example where the index() function in the views model is receiving HTTP request from some URL and been called by the URL mapper. It takes request object as parameter and returns response object , in this case it is returning a hard coded string.</p><pre># filename: views.py (Django view functions)<br>from django.http import HttpResponse<br>def index(request):<br>    return HttpResponse(&#39;Hello from Sundaram!&#39;)</pre><p>In the above example , first we import the <strong>HTTPResponse</strong> object from the <strong>Django.HTTP</strong> module to use it in the function.</p><p>Next , a function named index is defined that takes request(<strong>HTTP.Request</strong>) object as input parameter and then it returns a response , a hardcoded string in this case.</p><h3>Working with Data in Django</h3><p>Django web applications query data through models that are defined in <strong>models.py</strong> file. The models basically define the structure of stored data , their maximum size , type of data that can be stored etc. Once you define the model and other part of the code , then you can use any database and all the inner communication is done internally by django</p><p>Let’s see a sample of how a <strong>models.py</strong> looks like:</p><pre>from django.db import models<br>class Team(models.Model):<br>    team_name = models.CharField(max_length=40)<br><br>    TEAM_LEVELS = (<br>        (&#39;U09&#39;, &#39;Under 09s&#39;),<br>        (&#39;U10&#39;, &#39;Under 10s&#39;),<br>        (&#39;U11&#39;, &#39;Under 11s&#39;),<br><br>    )<br>    team_level = models.CharField(max_length=3, choices=TEAM_LEVELS, default=&#39;U11&#39;)</pre><p>Let’s breakdown the above code and see what each line of code is doing:</p><ul><li><strong>from django.db import models: </strong>Importing the models module from django that will provide functions to interact with the data.</li><li><strong>class Team(models.Model):</strong> Defines a model named Team that inherits the base <strong>‘Model’</strong> class of Django.</li><li><strong>team_name = models.CharField(max_length=40):</strong> The <strong>Team</strong> model has a single field <strong>team_name</strong> of type character (<strong>CharField</strong>) with a maximum length of 40 characters.</li></ul><pre>TEAM_LEVELS = (<br>    (&#39;U09&#39;, &#39;Under 09s&#39;),<br>    (&#39;U10&#39;, &#39;Under 10s&#39;),<br>    (&#39;U11&#39;, &#39;Under 11s&#39;),<br>    # …<br>    # list other team levels<br>)</pre><p><strong>TEAM_LEVELS</strong> above is a tuple that basically is defining a list of choices for the <strong>team_level</strong> field . It contains two elements: one human readable format and the other that is stored in the database.</p><pre>team_level = models.CharField(max_length=3, choices=TEAM_LEVELS, default=&#39;U11&#39;)</pre><p>The above code snippet defines another firls called <strong>team_level</strong> of the type character (<strong>CharField</strong>) with maximum length of 3 characters. Another parameter is <strong>choices</strong> that basically defines the set of values that particular field can have , in this case the <strong>team_level</strong> field can have pre-defined set of values from the tuple <strong>TEAM_LEVELS</strong> defined above. Another field <strong>default</strong> gives a default value to that field , in this case the <strong>team_level</strong> field is assigned a value of <strong>U11</strong>.</p><h3><strong>Querying Data</strong></h3><p>Once you define the models , views now we need to work on querying/ fetching that data . For this Django provides a simple query API that helps in searching the associated databases and can match against a number of fields based on different criteria like case-insensitive , exact match , etc.</p><p>Let us look at an example below:</p><pre>from django.shortcuts import render<br>def index(request):<br>    list_teams = Team.objects.filter(team_level__exact=&quot;U09&quot;)<br>    context = {&#39;youngest_teams&#39;: list_teams}<br>    return render(request, &#39;/best/index.html&#39;, context)</pre><ul><li>The first line imports the render() function from <strong>django.shortcuts</strong> module that is used to render the <strong>HTTPResponse</strong> for the incoming request.</li><li>The second line defines the index function to handle the incoming request .</li><li>The third line queries the database using the Django model<strong> Team</strong> It retrieves a list of teams where the <strong>team_level</strong> field is exactly equal to “U09”. The double underscore (__) is used in Django queries for field lookups.</li><li>Then we define a variable called <strong>contexts</strong> , this variable basically defines the data that is to be passed down the HTML Template for rendering it to the response. In this case the <strong>list_teams</strong> will be passed down to the HTML template and will be rendered in the response page. The <strong>youngest_teams</strong> variable is used as a reference in the HTML template.</li><li>Then , we are finally returning the response using the render() function imported above. The render() function accepts 2 arguments: The incoming <strong>request</strong> , the path to the <strong>template</strong> that will be sent as a response and the <strong>context</strong> variable that contains the data that will be passed down the template to be rendered in the <strong>HTTPResponse</strong>.</li></ul><h3>Rendering Response</h3><p>Below we will see how the data is rendered through the HTML template:</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;head&gt;<br>  &lt;meta charset=&quot;utf-8&quot;&gt;<br>  &lt;title&gt;Home page&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>  {% if youngest_teams %}<br>    &lt;ul&gt;<br>      {% for team in youngest_teams %}<br>        &lt;li&gt;{{ team.team_name }}&lt;/li&gt;<br>      {% endfor %}<br>    &lt;/ul&gt;<br>  {% else %}<br>    &lt;p&gt;No teams are available.&lt;/p&gt;<br>  {% endif %}<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>In the above section we saw the file path <strong>‘/best/index.html’</strong> , the above script is index.html , it consists of everything that will be rendered as the response to the request.</p><p>In the above script first it is checked if a variable named <strong>youngest_teams</strong> exists , if it does then it iterates over each value in <strong>youngest_teams</strong> using for loop and then each team name is rendered in the form of list.</p><h3>Conclusion:</h3><p>This blog covered how the internal communication in Django web applications is carried out among the various directories in order to serve an incoming request by routing the request to the intended handler , fetching or updating the data in the database and then rendering the data in the database.</p><p>In the later blogs we will see some more advanced concepts.</p><p>Happy Coding fellow devs! :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b17b51e3a7dc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>