<?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"><channel><title><![CDATA[Lakshminarayana's blog]]></title><description><![CDATA[A passionate Full Stack Developer, Expert in IIoT and AI enthusiast specialized in building innovative solutions at the intersection of web development and arti]]></description><link>https://blogs.lnbg.in</link><generator>RSS for Node</generator><lastBuildDate>Fri, 05 Jun 2026 21:08:00 GMT</lastBuildDate><atom:link href="https://blogs.lnbg.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How React’s Virtual DOM Works ?]]></title><description><![CDATA[1. Why we need something like a Virtual DOM?
Directly manipulating the browser’s Real DOM is expensive:

Each DOM change can trigger layout, style recalculation and paint.

Changing many nodes one-by-]]></description><link>https://blogs.lnbg.in/how-react-s-virtual-dom-works</link><guid isPermaLink="true">https://blogs.lnbg.in/how-react-s-virtual-dom-works</guid><dc:creator><![CDATA[Lakshminarayana]]></dc:creator><pubDate>Fri, 08 May 2026 15:25:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/64b83a718021768736270112/4a9d8358-f2e4-41c4-9881-808aa399d118.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>1. Why we need something like a Virtual DOM?</h2>
<p>Directly manipulating the browser’s Real DOM is expensive:</p>
<ul>
<li><p>Each DOM change can trigger layout, style recalculation and paint.</p>
</li>
<li><p>Changing many nodes one-by-one leads to layout thrashing and poor performance.</p>
</li>
<li><p>Writing manual, fine-grained DOM-diffing is error-prone and hard to maintain.</p>
</li>
</ul>
<p>React’s Virtual DOM (VDOM) is a lightweight JavaScript representation of the UI. React uses it to compute a minimal set of changes and apply them in batches to the Real DOM, minimizing costly DOM operations.</p>
<h2>2. Real DOM vs Virtual DOM a quick comparison</h2>
<ul>
<li><p><strong>Real DOM</strong></p>
<ul>
<li><p>Browser-managed tree of nodes.</p>
</li>
<li><p>Updating is relatively slow (layout, style, paint).</p>
</li>
<li><p>Imperative: you tell the browser exactly what to change.</p>
</li>
</ul>
</li>
<li><p><strong>Virtual DOM</strong></p>
<ul>
<li><p>JavaScript object tree representing the UI (elements, props, children).</p>
</li>
<li><p>Very cheap to create and compare.</p>
</li>
<li><p>Declarative: you describe what the UI should look like; React figures out how to update the Real DOM.</p>
</li>
</ul>
</li>
</ul>
<h2>3. Initial rendering</h2>
<ul>
<li><p>Component code runs its render() (or functional component body) and returns a tree of React elements (JS objects describing tags, props, and children).</p>
</li>
<li><p>React builds a Virtual DOM tree from those elements.</p>
</li>
<li><p>React converts (renders) that VDOM tree into Real DOM nodes and appends them into the document. This conversion may be batched/optimized.</p>
</li>
<li><p>Browser paints the initial UI.</p>
</li>
</ul>
<h2>4. How state or props changes trigger a re-render</h2>
<ul>
<li><p>When a component’s state or props change (e.g., via setState, useState setter, or new props from a parent), React marks that component as needing update.</p>
</li>
<li><p>React calls the component’s render function again (or re-runs the functional component) to produce a new VDOM tree for that component subtree.</p>
</li>
<li><p>Important: calling render does not directly modify the Real DOM — it produces a new VDOM representation.</p>
</li>
</ul>
<h2>5. Creating a new Virtual DOM tree</h2>
<p>Each update produces a new VDOM tree (or at least for the affected subtree). The old VDOM tree is retained (or referenced) so React can compare old vs new. The VDOM is typically a lightweight nested structure like:</p>
<p>{ type: 'div', props: { className: 'card' }, children: [ ... ] }</p>
<h2>6. What “diffing” (reconciliation) means</h2>
<p>Diffing = comparing the old VDOM tree with the new VDOM tree to find the minimal set of changes required to update the Real DOM so it matches the new VDOM.</p>
<p>Key ideas React uses (simple mental model):</p>
<ul>
<li><p>Compare nodes at the same position in the tree.</p>
</li>
<li><p>If node types differ (e.g., 'div' vs 'span' or ComponentA vs ComponentB), replace the node.</p>
</li>
<li><p>If types are the same, update attributes/props and recurse into children.</p>
</li>
<li><p>For lists of children, keys are used to match items across updates and avoid unnecessary re-creation.</p>
</li>
</ul>
<h2>7. How React finds the minimal required changes</h2>
<p>React’s reconciliation uses heuristics to avoid an O(n^3) general tree-diff. Simplified rules:</p>
<ol>
<li><p>If two nodes have different types, replace the subtree.</p>
</li>
<li><p>If same type, update props and compare children.</p>
</li>
<li><p>For children arrays:</p>
<ul>
<li><p>If keys are provided, use keys to match old and new children and move/update only those that changed.</p>
</li>
<li><p>If no keys, fall back to index-based comparison (which may cause more replacements).</p>
</li>
</ul>
</li>
<li><p>Only update attributes that changed; leave others untouched.</p>
</li>
<li><p>Update text nodes directly if text content changed.</p>
</li>
</ol>
<p>Because the VDOM is cheap to traverse and compare, React can compute a minimal "patch" set before touching the Real DOM.</p>
<h2>8. Applying patches: updating only changed nodes in the Real DOM</h2>
<p>After diffing, React has a list of operations (patches), e.g.:</p>
<ul>
<li><p>Update prop on DOM node A</p>
</li>
<li><p>Replace DOM node B with new node</p>
</li>
<li><p>Insert node C</p>
</li>
<li><p>Remove node D</p>
</li>
</ul>
<p>React batches many of these changes and applies them in the commit phase. By doing only the needed updates, it avoids unnecessary DOM writes and reduces reflows/layouts.</p>
<h2>9. Why this approach improves performance?</h2>
<ul>
<li><p>Fewer DOM operations: only necessary updates are applied.</p>
</li>
<li><p>Batch updates: changes can be grouped to avoid repeated layout calculations.</p>
</li>
<li><p>Faster decision-making: comparing JS objects in memory is much faster than DOM operations.</p>
</li>
<li><p>Predictable updates: declarative rendering reduces accidental inefficient DOM manipulations.</p>
</li>
</ul>
<h2>10. High-level React flow: render → diff → commit</h2>
<ol>
<li><p>Render (Render Phase)</p>
<ul>
<li><p>React calls render functions to produce a new VDOM tree (pure, side-effect free).</p>
</li>
<li><p>Multiple updates can be scheduled and combined here.</p>
</li>
</ul>
</li>
<li><p>Diffing / Reconciliation</p>
<ul>
<li><p>Compare old VDOM tree with new VDOM tree.</p>
</li>
<li><p>Produce a list of patch operations describing minimal changes.</p>
</li>
</ul>
</li>
<li><p>Commit (Commit Phase)</p>
<ul>
<li><p>Apply patches to the Real DOM.</p>
</li>
<li><p>Run lifecycle effects that require DOM (e.g., useLayoutEffect, refs).</p>
</li>
<li><p>Browser paints.</p>
</li>
</ul>
</li>
</ol>
<p>Note: React’s Fiber architecture reorganizes work into units to allow pausing and prioritizing updates (concurrency). For this article we avoid diving into Fiber internals—focus on the mental model above.</p>
<h2>11. Step-by-step lifecycle example (simple)</h2>
<ol>
<li><p>Initial render:</p>
<ul>
<li>App renders → VDOM-A created → commit → Real DOM reflects VDOM-A.</li>
</ul>
</li>
<li><p>setState called in a component:</p>
<ul>
<li><p>React schedules update → component re-renders → VDOM-B created.</p>
</li>
<li><p>React diffs VDOM-A vs VDOM-B → determines patches.</p>
</li>
<li><p>Commit phase applies patches to Real DOM → Real DOM updated to VDOM-B.</p>
</li>
</ul>
</li>
<li><p>Repeat for subsequent updates.</p>
</li>
</ol>
<h2>Small code example (conceptual)</h2>
<pre><code class="language-jsx">function TodoList({ items }) {
  return (
    &lt;ul&gt;
      {items.map(item =&gt; &lt;li key={item.id}&gt;{item.text}&lt;/li&gt;)}
    &lt;/ul&gt;
  );
}
</code></pre>
<p>When items change:</p>
<p>React re-renders TodoList → produces new VDOM for</p>
<ul>
<li><p>and children.</p>
</li>
<li><p>Because each</p>
</li>
<li><p>has a stable key, React reorders/updates only affected</p>
</li>
<li><p>nodes rather than recreating all.</p>
</li>
</ul>
<h2>Common misconceptions (clear and simple)</h2>
<ul>
<li><p>“React always re-renders the whole page” — No. React re-renders components to produce VDOM but only commits minimal DOM updates.</p>
</li>
<li><p>“Virtual DOM is a DOM clone kept in memory” — It’s a lightweight JavaScript representation (not a full browser DOM).</p>
</li>
<li><p>“You don’t need to worry about performance” — VDOM helps a lot, but you still need good keys, avoid heavy synchronous work during render, and memoize when appropriate.</p>
</li>
</ul>
<h2>Conclusion — mental model to keep</h2>
<ul>
<li><p>Think declaratively: describe what UI should be.</p>
</li>
<li><p>React converts descriptions to a lightweight tree (VDOM).</p>
</li>
<li><p>On updates, React creates a new VDOM, diffs it against the old, and applies only necessary changes to the Real DOM.</p>
</li>
<li><p>This reduces expensive DOM operations and keeps UI updates efficient.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Django for Beginners and Beyond: A Complete Learning Path]]></title><description><![CDATA[Step 1. What is Django?
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. It takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinven...]]></description><link>https://blogs.lnbg.in/django-for-beginners-and-beyond-a-complete-learning-path</link><guid isPermaLink="true">https://blogs.lnbg.in/django-for-beginners-and-beyond-a-complete-learning-path</guid><category><![CDATA[Django]]></category><category><![CDATA[django rest framework]]></category><category><![CDATA[Tailwind CSS]]></category><category><![CDATA[Flowbite]]></category><category><![CDATA[Python]]></category><category><![CDATA[Jinja2]]></category><dc:creator><![CDATA[Lakshminarayana]]></dc:creator><pubDate>Thu, 01 May 2025 13:10:22 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/QUwM2LDVs3A/upload/17dc7f2bc90c24cac50679668045b139.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-step-1-what-is-django">Step 1. What is Django?</h2>
<p>Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. It takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel.</p>
<p>Django is a full-featured web framework that follows the Model-View-Controller (MVC) architectural pattern. It provides a set of tools and libraries for building web applications, including an ORM, a templating engine, and a built-in admin interface.</p>
<h2 id="heading-step-2-django-setup">Step 2. Django Setup</h2>
<p>To get started with Django, you’ll need to install it on your computer. You can do this by running the following command in your terminal, after setting up a virtual environment:</p>
<pre><code class="lang-bash">python -m venv .venv <span class="hljs-comment"># Command to setup virtual environment</span>

<span class="hljs-comment"># for macOS and linux</span>
<span class="hljs-comment"># python3 -m venv .venv</span>
</code></pre>
<p>Now to activate the virtual environment we use the following command:</p>
<pre><code class="lang-bash">.venv\Scripts\activate <span class="hljs-comment"># To activate the virtual environment</span>

<span class="hljs-comment"># for macOS and linux</span>
<span class="hljs-comment"># source .venv/bin/activate</span>
</code></pre>
<h2 id="heading-step-3-django-project">Step 3. <strong>Django Project</strong></h2>
<p>A Django project is a collection of settings and configurations that define the structure and behavior of a web application. It includes the code for the application, as well as the templates, static files, and other resources that make up the application.</p>
<p>To create a new Django project, you can use the following command:</p>
<pre><code class="lang-bash">django-admin startproject demoproject
<span class="hljs-built_in">cd</span> demoproject
</code></pre>
<p>This will create a new directory called <code>demoproject</code> with the basic structure of a Django project.</p>
<h2 id="heading-step-4-start-a-django-server">Step 4. <strong>Start a Django Server</strong></h2>
<p>To start the Django server, you can use the following command:</p>
<pre><code class="lang-bash">python manage.py runserver
</code></pre>
<p>This will start the server and make it accessible at <a target="_blank" href="http://localhost:8000"><strong>http://localhost:8000</strong></a>.</p>
<p>Ignore the unapplied migrations warning. This is a common issue when starting a new Django project. We will address this in a later section.</p>
<h2 id="heading-step-5-creating-our-first-views">Step 5. <strong>Creating our first views</strong></h2>
<p>Create a new file called <code>views.py</code> in the <code>demoproject</code> directory. In this file, we will define a few views that are simple functions that return a response. We want to have home page, about page, and contact page. Each of these pages will return html content.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.http <span class="hljs-keyword">import</span> HttpResponse

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">home</span>(<span class="hljs-params">request</span>):</span>
    <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">"&lt;h1&gt;Welcome to Chai's Django Project: Home page&lt;/h1&gt;"</span>)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">about</span>(<span class="hljs-params">request</span>):</span>
    <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">"&lt;h1&gt;Welcome to Chai's Django Project: About page&lt;/h1&gt;"</span>)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">contact</span>(<span class="hljs-params">request</span>):</span>
    <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">"&lt;h1&gt;Welcome to Chai's Django Project: Contact page&lt;/h1&gt;"</span>)
</code></pre>
<p>Now, let’s create a new file called <code>urls.py</code> in the <code>demoproject</code> directory. In this file, we will define the URL patterns for our application. If the file is already there, you can just add the following code to the end of the file:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> path
<span class="hljs-keyword">from</span> . <span class="hljs-keyword">import</span> views

urlpatterns = [
    path(<span class="hljs-string">''</span>, views.home, name=<span class="hljs-string">'home'</span>),
    path(<span class="hljs-string">'about/'</span>, views.about, name=<span class="hljs-string">'about'</span>),
    path(<span class="hljs-string">'contact/'</span>, views.contact, name=<span class="hljs-string">'contact'</span>),
]
</code></pre>
<p>Now, let’s run the server again and visit the following URLs:</p>
<ul>
<li><p><a target="_blank" href="http://localhost:8000/"><strong>http://localhost:8000/</strong></a></p>
</li>
<li><p><a target="_blank" href="http://localhost:8000/about/"><strong>http://localhost:8000/about/</strong></a></p>
</li>
<li><p><a target="_blank" href="http://localhost:8000/contact/"><strong>http://localhost:8000/contact/</strong></a></p>
</li>
</ul>
<p>You should see the following output in the above routes:</p>
<pre><code class="lang-plaintext">Welcome to Chai's Django Project: Home page
Welcome to Chai's Django Project: About page
Welcome to Chai's Django Project: Contact page
</code></pre>
<h2 id="heading-step-6-adding-templates-app-in-django">Step 6. Adding <strong>Templates App in Django</strong></h2>
<p>In Django, templates are used to generate HTML pages. They are used to display data and perform logic in a web application. To create a template, you can create a new file called <code>templates/layout.html</code> in the root directory. Make sure that template folder is at same level as <code>manage.py</code> file.</p>
<pre><code class="lang-xml"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Welcome to Chai's Django Project<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Welcome to Chai's Django Project<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Now if you need to view this template in the <a target="_blank" href="http://localhost:8000/"><strong>http://localhost:8000/</strong></a> you need to make some changes in the <code>views.py</code> and <code>settings.py</code> file:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Python : settings.py</span>

<span class="hljs-string">'DIRS'</span>: [<span class="hljs-string">'templates'</span>], <span class="hljs-comment"># add the name of the templates directory inside this array</span>
</code></pre>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.http <span class="hljs-keyword">import</span> HttpResponse
<span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">home</span>(<span class="hljs-params">request</span>):</span>
    <span class="hljs-comment"># return HttpResponse("&lt;h1&gt;Welcome to Chai's Django Project: Home page&lt;/h1&gt;")</span>
    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">"index.html"</span>)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">about</span>(<span class="hljs-params">request</span>):</span>
    <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">"&lt;h1&gt;Welcome to Chai's Django Project: About page&lt;/h1&gt;"</span>)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">contact</span>(<span class="hljs-params">request</span>):</span>
    <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">"&lt;h1&gt;Welcome to Chai's Django Project: Contact page&lt;/h1&gt;"</span>)
</code></pre>
<h2 id="heading-step-7-adding-css-and-javascript">Step 7. <strong>Adding CSS and JavaScript</strong></h2>
<p>To add CSS and JavaScript to your Django project, you can create a new file called <code>static/css/style.css</code> in the root of the <code>demoproject</code> directory. In this file, you can write CSS code that will be used to style the HTML page. You can also create a new file called <code>static/js/script.js</code> in the same directory. In this file, you can write JavaScript code that will be used to add interactivity to the HTML page.</p>
<p>To add these static files go to <code>settings.py</code> and add the following line:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> os <span class="hljs-comment"># import this at the top of settings.py file</span>

STATIC_URL = <span class="hljs-string">'/static/'</span> <span class="hljs-comment"># below this add the following line</span>
STATICFILES_DIRS = [os.path.join(BASE_DIR, <span class="hljs-string">'static'</span>)]
</code></pre>
<p>In the index.html file, at the top of the file add the following line:</p>
<pre><code class="lang-xml">{% load static %} 
<span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"{% static 'css/style.css' %}"</span>&gt;</span>
</code></pre>
<p>Now, let’s run the server again and visit the <a target="_blank" href="http://localhost:8000/"><strong>http://localhost:8000/</strong></a> URL. You should see the output.</p>
<h2 id="heading-step-8-adding-jinja2-templates-to-django-project">Step 8. Adding Jinja2 Templates to Django Project</h2>
<p>Jinja2 templates are written in a simple text format called HTML. The syntax is very similar to HTML, but with some additional features. You need to inject variables into the template using the <code>{{ variable }}</code> syntax. For example, if you want to display a name, you can use the following code:</p>
<pre><code class="lang-python">Hello {{ name }}!
</code></pre>
<p>Jinja2 is a template engine for Python. It is a very powerful template engine that can be used to render HTML, XML, and other formats. It is also used to render templates for the Django admin interface. If you are in Django, you don’t need to install Jinja2 separately. It is already installed with Django. Django also comes with a built-in template configurations that allows you to use Jinja2 templates.</p>
<p>For more details on how to implement Jinja Templates in Django refer the article: <a target="_blank" href="https://blogs.lnbg.in/getting-started-with-jinja-templates-in-django">Getting Started with Jinja Templates in Django</a></p>
<h2 id="heading-step-9-install-tailwind-css-and-flowbite-to-your-django-project">Step 9. <strong>Install Tailwind CSS and Flowbite to your Django project</strong></h2>
<p>Tailwind CSS is a CSS framework that allows you to build custom styles for your web pages. It provides a set of pre-built classes that you can use to style your HTML elements, whereas Flowbite is an open-source library of UI components based on the Tailwind CSS featuring all of the commonly used components that a website requires, such as buttons, dropdowns, navigation bars, modals, but also some more advanced interactive elements such as datepickers. For step-by-step installation refer this article <a target="_blank" href="https://blogs.lnbg.in/how-to-add-tailwind-css-and-flowbite-to-your-django-project">how-to-add-tailwind-css-and-flowbite-to-your-django-project</a></p>
<h2 id="heading-step-10-adding-apps-in-django">Step 10. Adding <strong>Apps in Django</strong></h2>
<p>The most common way to organize your Django project is to use apps. An app is a self-contained module that contains models, views, templates, and other components of your project. Apps allow you to organize your code into logical units and make it easier to manage and maintain your project.</p>
<p>You can create it manually or use the <code>startapp</code> command to create a new app for you. To create an app, navigate to the directory where you want to create the app and run the following command:</p>
<pre><code class="lang-bash">python manage.py startapp demoapp
</code></pre>
<p>This will create a new directory called <code>demoapp</code> with the necessary files and directories for an app.</p>
<p>To add an app to your project, you need to add it to the <code>INSTALLED_APPS</code> setting in your project’s <code>settings.py</code> file. You can do this by adding the app’s name to the list of installed apps:</p>
<pre><code class="lang-xml">INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'demoapp',
]
</code></pre>
<p>In Django, templates are organized into apps. Each app can have its own templates directory, which contains the templates for that app. Create a new directory called <code>templates</code> in your app’s directory. Inside the demoapp directory, create a <code>templates</code> directory and add a <code>my_app.html</code> file to it.</p>
<p>Add your basic html code to the <code>my_app.html</code> file.</p>
<p>To serve this file, we need a view and a url. Create a new file called <code>views.py</code> in your app’s directory. Add the following code to the file:</p>
<pre><code class="lang-xml">from django.shortcuts import render

def all_chai(request):
    return render(request, 'my_app.html')
</code></pre>
<p>This view will render the <code>my_app.html</code> template when it is called.</p>
<p>Create a new file called <code>urls.py</code> in your app’s directory. Add the following code to the file:</p>
<pre><code class="lang-xml">from django.urls import path
from . import views

urlpatterns = [
    path('', views.my_app, name='my_app'),
]
</code></pre>
<p>This urlpattern will map the root URL of the app to the <code>my_app</code> view.</p>
<p>Now, we need to make aware of this new urlpattern in our project’s <code>urls.py</code> file. Add the following code to the project’s <code>urls.py</code> file:</p>
<pre><code class="lang-xml">from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('demoapp/', include('chai.urls')),
]
</code></pre>
<p>Now, we can access the <code>my_app</code> view by going to <a target="_blank" href="http://127.0.0.1:8000/demoapp/">http://localhost:8000/demoapp</a></p>
]]></content:encoded></item><item><title><![CDATA[How to Add Tailwind CSS and Flowbite to Your Django Project]]></title><description><![CDATA[Django is a strong web framework that helps developers create solid web apps. To improve the look and user interface of your Django project, you can add Tailwind CSS and Flowbite components. This guide will show you how to add Tailwind CSS, turn on T...]]></description><link>https://blogs.lnbg.in/how-to-add-tailwind-css-and-flowbite-to-your-django-project</link><guid isPermaLink="true">https://blogs.lnbg.in/how-to-add-tailwind-css-and-flowbite-to-your-django-project</guid><category><![CDATA[Tailwind CSS]]></category><category><![CDATA[Django]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[projects]]></category><category><![CDATA[GitHub]]></category><category><![CDATA[CSS]]></category><category><![CDATA[HTML5]]></category><category><![CDATA[Python]]></category><category><![CDATA[Python 3]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[Flowbite]]></category><category><![CDATA[component libraries]]></category><category><![CDATA[components]]></category><dc:creator><![CDATA[Lakshminarayana]]></dc:creator><pubDate>Tue, 18 Feb 2025 14:04:14 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1739855965092/c07a4eef-3d3f-4c1e-bbba-4987b1b91701.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Django is a strong web framework that helps developers create solid web apps. To improve the look and user interface of your Django project, you can add Tailwind CSS and Flowbite components. This guide will show you how to add Tailwind CSS, turn on Tailwind's JIT (Just-In-Time) mode for live reloading, and use Flowbite components. To begin with Django installation and setup, refer <a target="_blank" href="https://lnbg.hashnode.dev/django-for-beginners-and-beyond-a-complete-learning-path">here</a>.</p>
<h2 id="heading-install-the-django-tailwind-package-via-pip">Install the Django Tailwind package via pip:</h2>
<p>To add Tailwind CSS to your Django project, you'll need to have a Node.js and NPM installed on your system. If you haven't already, install Node.js and NPM (Node Package Manager). You can install Node.js from <a target="_blank" href="https://nodejs.org/en/download">here</a>. And then run the below command to install the following packages using pip:</p>
<pre><code class="lang-bash">pip install django-tailwind
pip install <span class="hljs-string">'django-tailwind[reload]'</span>
</code></pre>
<p>This will install the <code>django-tailwind</code> package and the <code>django-tailwind[reload]</code> package, which includes the <code>tailwind-django</code> command-line tool.</p>
<p>Once you have installed Tailwind CSS, you can use the <code>tailwind-django</code> command to generate the necessary files for your project. This command will create a <code>tailwind.config.js</code> file in your project directory, as well as a <code>static/css/tailwind.css</code> file that contains the compiled Tailwind CSS.</p>
<p>Now, add <code>tailwind</code> to your <code>INSTALLED_APPS</code> in your <code>settings.py</code> file:</p>
<pre><code class="lang-python">INSTALLED_APPS = [
  <span class="hljs-comment"># other Django apps</span>
  <span class="hljs-string">'tailwind'</span>,
]
</code></pre>
<p>Next, run the below command to create a <strong>Tailwind CSS</strong> compatible Django app, I’d like to call it <code>theme</code>. Customize with your own names:</p>
<pre><code class="lang-bash">python manage.py tailwind init
</code></pre>
<p>Add the newly create <code>theme</code> to your <code>INSTALLED_APPS</code> in your <code>settings.py</code> file:</p>
<pre><code class="lang-python">INSTALLED_APPS = [
  <span class="hljs-comment"># other Django apps</span>
  <span class="hljs-string">'tailwind'</span>,
  <span class="hljs-string">'theme'</span>
]
</code></pre>
<p>Register the generated <code>theme</code> app by adding the following line to <code>settings.py</code> file:</p>
<pre><code class="lang-python">TAILWIND_APP_NAME = <span class="hljs-string">'theme'</span>
</code></pre>
<p>Make sure that the <code>INTERNAL_IPS</code> list is present in the <code>settings.py</code> file and contains the <code>127.0.0.1</code> IP address:</p>
<pre><code class="lang-python">INTERNAL_IPS = [
    <span class="hljs-string">"127.0.0.1"</span>,
]
</code></pre>
<p>Now, install <em>Tailwind CSS</em> dependencies, by running the following command:</p>
<pre><code class="lang-bash">python manage.py tailwind install
</code></pre>
<p>The <code>django-tailwind</code> package comes with a simple <code>base.html</code> template located at <code>your_tailwind_app_name/templates/base.html</code>. You can always extend or delete it if you already have a layout. If you are not using the <code>base.html</code> template that comes with <code>django-tailwind</code>, you can configure by adding the below lines in choice of your template:</p>
<pre><code class="lang-xml">{% load static tailwind_tags %}
...
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
   ...
   {% tailwind_css %}
   ...
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
</code></pre>
<p>The <code>{% tailwind_css %}</code> tag includes Tailwind’s stylesheet.</p>
<p>Let’s also add and configure <code>django_browser_reload</code>, which takes care of automatic page and CSS refreshes in the development mode. Add it to <code>INSTALLED_APPS</code> in <code>settings.py</code>:</p>
<pre><code class="lang-xml">INSTALLED_APPS = [
  # other Django apps
  'tailwind',
  'theme',
  'django_browser_reload'
]
</code></pre>
<p>The middleware should be listed after any that encode the response, such as Django’s <code>GZipMiddleware</code>. The middleware automatically inserts the required script tag on HTML responses before <code>&lt;/body&gt;</code> when <code>DEBUG</code> is <code>True.</code></p>
<p>Include <code>django_browser_reload</code> URL in your root <code>url.py</code> :</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> include, path

urlpatterns = [
    ...,
    path(<span class="hljs-string">"__reload__/"</span>, include(<span class="hljs-string">"django_browser_reload.urls"</span>)),
]
</code></pre>
<p>include this at the end of all the paths.</p>
<p>Now, finally to render the tailwind in your project you need to configure an last step that is to include the path of NPM. That’s because this app needs node.js to be installed in your system. And you need to provide the path to the node.js executable in your <code>settings.py</code> file: You can use command <code>where node</code> to find the path to the node.js executable.</p>
<pre><code class="lang-bash">NPM_BIN_PATH = <span class="hljs-string">'npm.cmd'</span> <span class="hljs-comment"># For Windows</span>

NPM_BIN_PATH = <span class="hljs-string">'/usr/local/bin/npm'</span> <span class="hljs-comment"># For MacOS and linux</span>
</code></pre>
<p>Now, all the steps are completed to start Django Tailwind in development mode, run the following command in a terminal to get started:</p>
<pre><code class="lang-bash">python manage.py tailwind start
</code></pre>
<p>This will start a long-running process that monitors files for changes. Use <code>CTRL</code> + <code>C</code> to stop the process.</p>
<p>Here's what's happening behind the scenes:</p>
<ol>
<li><p>The stylesheet updates every time you add or remove a CSS class in a Django template.</p>
</li>
<li><p><code>django-browser-reload</code> watches for changes in HTML and CSS files. When a Django template or CSS is updated, the browser refreshes them automatically. This provides a smooth development experience without needing to reload the page to see updates.</p>
</li>
</ol>
<h2 id="heading-installing-tailwind-css-django-flowbite"><strong>Installing Tailwind CSS Django - Flowbite</strong></h2>
<p>Now that we have successfully installed Tailwind CSS, if we want to implement or use some components, we can use the Flowbite component library to do so. Below are the given steps to get stated with the Flowbite:</p>
<p>Install Flowbite as a dependency using NPM</p>
<pre><code class="lang-bash">npm install flowbite
</code></pre>
<p>Make some desired changes in the <code>tailwind.config.js</code> file:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">module</span>.exports = {
  <span class="hljs-attr">content</span>: [
      <span class="hljs-string">'./templates/**/*.html'</span>,
      <span class="hljs-string">'./node_modules/flowbite/**/*.js'</span>
  ],
  <span class="hljs-attr">theme</span>: {
    <span class="hljs-attr">extend</span>: {},
  },
  <span class="hljs-attr">plugins</span>: [
      <span class="hljs-built_in">require</span>(<span class="hljs-string">'flowbite/plugin'</span>)
  ]
}
</code></pre>
<p>Include Flowbite’s JavaScript file inside the <code>layout.html</code> file just before the end of the <code>&lt;body&gt;</code> tag using CDN or by including it directly from the <code>node_modules/</code> folder</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/npm/flowbite@3.1.2/dist/flowbite.min.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>Let’s start by adding a <a target="_blank" href="https://flowbite.com/docs/components/navbar/">Navbar component</a> inside the <code>layout.html</code> file:</p>
<pre><code class="lang-xml">{% load compress %}
{% load static %}

<span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"X-UA-Compatible"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"IE=edge"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Django + Tailwind CSS + Flowbite<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>

    {% compress css %}
    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"{% static 'src/output.css' %}"</span>&gt;</span>
    {% endcompress %}

<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">body</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"bg-green-50"</span>&gt;</span>

    <span class="hljs-comment">&lt;!-- Add this --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">nav</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"bg-green-50 border-gray-200 px-2 sm:px-4 py-2.5 rounded-sm dark:bg-gray-800"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container flex flex-wrap items-center justify-between mx-auto"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"{{ .Site.Params.homepage }}/"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"flex items-center"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"/docs/images/logo.svg"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"h-6 mr-3 sm:h-9"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Flowbite Logo"</span> /&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"self-center text-xl font-semibold whitespace-nowrap dark:text-white"</span>&gt;</span>Flowbite Django<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">data-collapse-toggle</span>=<span class="hljs-string">"mobile-menu"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"button"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"inline-flex items-center p-2 ml-3 text-sm text-gray-500 rounded-lg md:hidden hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-gray-200 dark:text-gray-400 dark:hover:bg-gray-700 dark:focus:ring-gray-600"</span> <span class="hljs-attr">aria-controls</span>=<span class="hljs-string">"mobile-menu"</span> <span class="hljs-attr">aria-expanded</span>=<span class="hljs-string">"false"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"sr-only"</span>&gt;</span>Open main menu<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"w-6 h-6"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"currentColor"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"0 0 20 20"</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">path</span> <span class="hljs-attr">fill-rule</span>=<span class="hljs-string">"evenodd"</span> <span class="hljs-attr">d</span>=<span class="hljs-string">"M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 15a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z"</span> <span class="hljs-attr">clip-rule</span>=<span class="hljs-string">"evenodd"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">path</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hidden w-6 h-6"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"currentColor"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"0 0 20 20"</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">path</span> <span class="hljs-attr">fill-rule</span>=<span class="hljs-string">"evenodd"</span> <span class="hljs-attr">d</span>=<span class="hljs-string">"M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"</span> <span class="hljs-attr">clip-rule</span>=<span class="hljs-string">"evenodd"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">path</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hidden w-full md:block md:w-auto"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"mobile-menu"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"flex flex-col mt-4 md:flex-row md:space-x-8 md:mt-0 md:text-sm md:font-medium"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"block py-2 pl-3 pr-4 text-white bg-green-700 rounded-sm md:bg-transparent md:text-green-700 md:p-0 dark:text-white"</span> <span class="hljs-attr">aria-current</span>=<span class="hljs-string">"page"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"block py-2 pl-3 pr-4 text-gray-700 border-b border-gray-100 hover:bg-gray-50 md:hover:bg-transparent md:border-0 md:hover:text-green-700 md:p-0 dark:text-gray-400 md:dark:hover:text-white dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent dark:border-gray-700"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"block py-2 pl-3 pr-4 text-gray-700 border-b border-gray-100 hover:bg-gray-50 md:hover:bg-transparent md:border-0 md:hover:text-green-700 md:p-0 dark:text-gray-400 md:dark:hover:text-white dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent dark:border-gray-700"</span>&gt;</span>Services<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"block py-2 pl-3 pr-4 text-gray-700 border-b border-gray-100 hover:bg-gray-50 md:hover:bg-transparent md:border-0 md:hover:text-green-700 md:p-0 dark:text-gray-400 md:dark:hover:text-white dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent dark:border-gray-700"</span>&gt;</span>Pricing<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"block py-2 pl-3 pr-4 text-gray-700 hover:bg-gray-50 md:hover:bg-transparent md:border-0 md:hover:text-green-700 md:p-0 dark:text-gray-400 md:dark:hover:text-white dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent"</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- End of new HTML --&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container mx-auto mt-4"</span>&gt;</span>
        {% block content %}
        {% endblock content %}
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/npm/flowbite@3.1.2/dist/flowbite.min.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h2 id="heading-creating-a-tailwind-css-production-build">Creating a Tailwind CSS production build</h2>
<p>To create a production build of your theme, run the following command:</p>
<pre><code class="lang-bash">python manage.py tailwind build
</code></pre>
<p>{% tailwind_preload_css %} tag is used to replace the development build with a bundle optimized for production. The tag generates a preload directive for your stylesheet, which improves loading performance in production. Place it above the <code>{% tailwind_css %}</code> tag:</p>
<pre><code class="lang-xml">{% load tailwind_tags %}
<span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Django Tailwind<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    {% tailwind_preload_css %}
    {% tailwind_css %}
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>By following these steps, you have successfully integrated Tailwind CSS and Flowbite components into your Djan<a target="_blank" href="https://django-tailwind.readthedocs.io/en/latest/installation.html">go p</a><a target="_blank" href="https://django-tailwind.readthedocs.io/en/latest/settings.html">rojec</a>t. Thi<a target="_blank" href="https://django-tailwind.readthedocs.io/en/latest/installation.html">s se</a><a target="_blank" href="https://django-tailwind.readthedocs.io/en/latest/settings.html">tup a</a>llows you to leverage the power of Tailwind's utility-first CSS and Flowbite's pre-designed components to create a visually appealing and responsive web application.</p>
]]></content:encoded></item><item><title><![CDATA[Getting Started with Jinja Templates in Django]]></title><description><![CDATA[Understanding Jinja2 Tags with Examples
Django is a powerful web framework that allows developers to build robust web applications. To get started with the Django setup and installation refer here.
Jinja2 is a powerful templating engine for Python, w...]]></description><link>https://blogs.lnbg.in/getting-started-with-jinja-templates-in-django</link><guid isPermaLink="true">https://blogs.lnbg.in/getting-started-with-jinja-templates-in-django</guid><category><![CDATA[jinja]]></category><category><![CDATA[Python]]></category><category><![CDATA[Jinja2]]></category><category><![CDATA[Python 3]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[Django]]></category><category><![CDATA[template]]></category><category><![CDATA[template-engine]]></category><dc:creator><![CDATA[Lakshminarayana]]></dc:creator><pubDate>Tue, 18 Feb 2025 07:49:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1739855289900/0c0d8c85-1ede-4abd-8b3e-864644ac12d7.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-understanding-jinja2-tags-with-examples">Understanding Jinja2 Tags with Examples</h2>
<p>Django is a powerful web framework that allows developers to build robust web applications. To get started with the Django setup and installation refer <a target="_blank" href="https://lnbg.hashnode.dev/django-for-beginners-and-beyond-a-complete-learning-path">here</a>.</p>
<p>Jinja2 is a powerful templating engine for Python, widely used in web frameworks like Flask and Django. It allows developers to create dynamic web pages by embedding Python-like expressions in HTML. Here, we'll explore some of the essential Jinja2 tags and provide examples to illustrate their usage.</p>
<p>Some of the common template tags in Jinja2 are:</p>
<ol>
<li><h2 id="heading-extends"><code>% extends %}</code></h2>
</li>
</ol>
<p>The <code>{% extends %}</code> tag is used to extend a base template. It takes a template name as an argument, and extends the base template with the contents of the template file.</p>
<p>For example, the following code extends the <code>layout.html</code> template with the contents of a template file called <code>child.html</code> , the code looks like:</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-html">{% extends "base.html" %}

{% block content %}
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Welcome to my website!<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is a child template.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
{% endblock %}
</code></pre>
<p>In this example, the <code>content</code> block in the child template overrides the <code>content</code> block in the base template, and the <code>title</code> block is not overridden.</p>
<ol start="2">
<li><h2 id="heading-load"><code>{% load %}</code></h2>
<p> The <code>{% load %}</code> tag is used to load a template tag library. It takes a library name as an argument, and loads the template tag library with that name. For example, the following code loads the <code>static</code> template tag library:</p>
<pre><code class="lang-xml"> {% load static %}
</code></pre>
<p> This will load the <code>static</code> template tag library, which provides a set of template tags for working with static files.</p>
</li>
<li><h2 id="heading-block-and-endblock"><code>{ block %} and {% endblock %}</code></h2>
</li>
</ol>
<p>The <code>{% block %}</code> tag is used to define a block of content that can be overridden in child templates. It takes a name as an argument, and defines a block with that name that can be overridden or modified in child templates. For example, the following code defines a base template that includes a header and a footer with some default values:</p>
<pre><code class="lang-xml"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>{% block title %}My Website{% endblock %}<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    {% block content %}
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Welcome to my website!<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    {% endblock %}
    <span class="hljs-tag">&lt;<span class="hljs-name">footer</span>&gt;</span>
        {% block footer %}
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Copyright © 2021<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        {% endblock %}
    <span class="hljs-tag">&lt;/<span class="hljs-name">footer</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>And the following code defines a child template that overriding the <code>content</code> block:</p>
<pre><code class="lang-xml">{% extends "base.html" %}

{% block title %}My Website{% endblock %}

{% block content %}
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Welcome to my website!<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is a child template.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
{% endblock %}
</code></pre>
<p>In this example, the <code>content</code> block in the child template overrides the <code>content</code> block in the base template, and the <code>title</code> block is not overridden.</p>
<ol start="4">
<li><h2 id="heading-include"><code>{% include %}</code></h2>
</li>
</ol>
<p>The <code>{% include %}</code> tag is used to include another template within the current template. This is useful for reusing components like headers and footers.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-html">{% include "header.html" %}
</code></pre>
<ol start="5">
<li><h2 id="heading-if-else-and-endif"><code>{% if %}</code>, <code>{% else %}</code>, and <code>{% endif %}</code></h2>
</li>
</ol>
<p>These tags are used for conditional statements, allowing you to render content based on certain conditions.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-html">{% if user.is_authenticated %}
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Welcome, {{ user.username }}!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
{% else %}
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Please log in.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
{% endif %}
</code></pre>
<ol start="6">
<li><h2 id="heading-for-and-endfor"><code>{% for %}</code> and <code>{% endfor %}</code></h2>
</li>
</ol>
<p>The <code>{% for %}</code> tag is used to iterate over a sequence of items. It takes a variable name and a sequence as arguments, and displays the content inside the tag for each item in the sequence. For example, the following code will display a items which are iterated:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
{% for item in items %}
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>{{ item }}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
{% endfor %}
<span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
</code></pre>
<ol start="7">
<li><h2 id="heading-with-and-endwith"><code>{% with %}</code> and <code>{% endwith %}</code></h2>
</li>
</ol>
<p>The <code>{% with %}</code> tag allows you to define a variable for use within a block, simplifying complex expressions.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-html">{% with total=price * quantity %}
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Total: {{ total }}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
{% endwith %}
</code></pre>
<ol start="8">
<li><h2 id="heading-url"><code>{% url %}</code></h2>
</li>
</ol>
<p>The <code>{% url %}</code> tag is used to generate a URL for a view. It takes a view name and a set of arguments as arguments, and generates a URL for the view with those arguments.</p>
<p>For example, the following code generates a URL for the <code>index</code> view with the <code>name</code> argument set to <code>'home'</code>:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"{% url 'index' name='home' %}"</span>&gt;</span>Go to the home page<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
</code></pre>
<p>This will generate a link to the home page with the <code>name</code> argument set to <code>'home'</code> in the views file.</p>
<ol start="9">
<li><h2 id="heading-macro-and-endmacro"><code>{% macro %}</code> and <code>{% endmacro %}</code></h2>
</li>
</ol>
<p>Macros are like functions in Jinja2, allowing you to define reusable snippets of code.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-html">{% macro render_item(item) %}
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>{{ item }}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
{% endmacro %}
</code></pre>
<ol start="10">
<li><h2 id="heading-call"><code>{% call %}</code></h2>
</li>
</ol>
<p>The <code>{% call %}</code> tag is used to call a macro with a block of content.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-html">{% call render_item() %}
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Special Item<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
{% endcall %}
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Jinja2 tags provide a flexible way to create dynamic and reusable templates. By understanding and utilizing these tags, you can build efficient and maintainable web applications. Whether you're extending templates, including components, or using control structures, Jinja2 offers a robust set of tools to enhance your web development workflow.</p>
]]></content:encoded></item><item><title><![CDATA[Step-by-Step Guide: Using Prettier and Husky for Automatic Code Formatting]]></title><description><![CDATA[Step 1: What is Prettier and Husky
Prettier is a code formatter that ensures a consistent style by parsing your code and reprinting it according to its own rules. Husky is a tool that helps you manage Git hooks easily, allowing you to automate tasks ...]]></description><link>https://blogs.lnbg.in/step-by-step-guide-using-prettier-and-husky-for-automatic-code-formatting</link><guid isPermaLink="true">https://blogs.lnbg.in/step-by-step-guide-using-prettier-and-husky-for-automatic-code-formatting</guid><category><![CDATA[husky]]></category><category><![CDATA[Prettier]]></category><category><![CDATA[Express]]></category><category><![CDATA[backend]]></category><category><![CDATA[Node.js]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[framework]]></category><category><![CDATA[indentation]]></category><dc:creator><![CDATA[Lakshminarayana]]></dc:creator><pubDate>Sun, 09 Feb 2025 10:23:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/oXlXu2qukGE/upload/e81b731c02b7c2f9410c421e354d71b8.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-step-1-what-is-prettier-and-husky">Step 1: What is Prettier and Husky</h2>
<p>Prettier is a <strong>code formatter</strong> that ensures a consistent style by <strong>parsing your code</strong> and reprinting it according to its own rules. Husky is a tool that helps you <strong>manage Git hooks</strong> easily, allowing you to automate tasks like code formatting before commits.</p>
<h2 id="heading-step-2-install-prettier-husky-and-lint-staged">Step 2: Install Prettier, Husky, and lint-staged</h2>
<p>First, install Prettier, Husky, and lint-staged as development dependencies in your project:</p>
<pre><code class="lang-bash">npm install --save-dev prettier husky lint-staged
</code></pre>
<h2 id="heading-step-3-configure-prettier">Step 3: Configure Prettier</h2>
<p>Create a <code>.prettierrc</code> file in the root of your project to configure Prettier according to your preferences. For example:</p>
<pre><code class="lang-json">{
    <span class="hljs-attr">"singleQuote"</span>: <span class="hljs-literal">false</span>,
    <span class="hljs-attr">"bracketSpacing"</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">"tabWidth"</span>: <span class="hljs-number">4</span>,
    <span class="hljs-attr">"semi"</span>: <span class="hljs-literal">true</span>
}
</code></pre>
<h2 id="heading-step-4-create-a-prettierignore-file">Step 4: Create a .prettierignore File</h2>
<p>To prevent Prettier from formatting certain files or directories, create a <code>.prettierignore</code> file in the root of your project. This file works similarly to a <code>.gitignore</code> file. Add the paths you want to exclude from formatting. For example:</p>
<pre><code class="lang-plaintext">node_modules
build
dist
</code></pre>
<p>This step ensures that Prettier ignores the specified files and directories during the formatting process.</p>
<h2 id="heading-step-5-add-a-pre-commit-hook">Step 5: Add a Pre-commit Hook</h2>
<p>Create a pre-commit hook that runs lint-staged to format only the staged files:</p>
<pre><code class="lang-bash">npx husky add .husky/pre-commit <span class="hljs-string">"npx lint-staged"</span>
</code></pre>
<p>This command will create a <code>.husky/pre-commit</code> file with the following content:</p>
<pre><code class="lang-bash"><span class="hljs-meta">#!/bin/sh</span>
. <span class="hljs-string">"<span class="hljs-subst">$(dirname <span class="hljs-string">"<span class="hljs-variable">$0</span>"</span>)</span>/_/husky.sh"</span>

npx lint-staged
</code></pre>
<h2 id="heading-step-6-configure-lint-staged">Step 6: Configure lint-staged</h2>
<p>Configure lint-staged to run Prettier on your staged files. Add the following configuration to your <code>package.json</code>:</p>
<pre><code class="lang-json">{
    <span class="hljs-attr">"husky"</span>: {
        <span class="hljs-attr">"hooks"</span>: {
            <span class="hljs-attr">"pre-commit"</span>: <span class="hljs-string">"lint-staged"</span>
        }
    },
    <span class="hljs-attr">"lint-staged"</span>: {
        <span class="hljs-attr">"src/**/*.{js,jsx,ts,tsx}"</span>: [
            <span class="hljs-string">"prettier --write"</span>,
            <span class="hljs-string">"git add"</span>
        ]
    }
}
</code></pre>
<p>This configuration tells lint-staged to format any staged file matching the specified extensions using Prettier.</p>
<h2 id="heading-step-7-commit-your-changes">Step 7: Commit Your Changes</h2>
<p>After setting up everything, you can commit your changes. When you run <code>git commit</code>, Husky will trigger the pre-commit hook, and lint-staged will format the staged files using Prettier. Here's an example of what the output might look like:</p>
<pre><code class="lang-bash">git add .
git commit -m <span class="hljs-string">"Add new feature"</span>

<span class="hljs-comment"># Example output:</span>
<span class="hljs-comment"># &gt; husky - pre-commit hook</span>
<span class="hljs-comment"># &gt; lint-staged</span>
<span class="hljs-comment"># ✔ Preparing...</span>
<span class="hljs-comment"># ✔ Running tasks...</span>
<span class="hljs-comment"># ✔ Applying modifications...</span>
<span class="hljs-comment"># ✔ Cleaning up...</span>
<span class="hljs-comment"># [main 1a2b3c4] Add new feature</span>
<span class="hljs-comment">#  2 files changed, 10 insertions(+), 2 deletions(-)</span>
</code></pre>
<h3 id="heading-conclusion">Conclusion</h3>
<p>By following these steps, you’ve set up an automated system that ensures your code is always formatted according to your Prettier configuration before every commit. This integration not only enforces consistency across your project but also reduces the manual effort required during code reviews.</p>
]]></content:encoded></item></channel></rss>