<?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[Django]]></title><description><![CDATA[Django]]></description><link>https://djangopython.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 13 Oct 2026 12:40:30 GMT</lastBuildDate><atom:link href="https://djangopython.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[django example]]></title><description><![CDATA[✅ Step 1: Model (models.py)
This defines the structure of your database table.
pythonCopyEdit# models.py
from django.db import models

class Patient(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField()


✅ Step 2...]]></description><link>https://djangopython.hashnode.dev/django-example</link><guid isPermaLink="true">https://djangopython.hashnode.dev/django-example</guid><category><![CDATA[Django]]></category><dc:creator><![CDATA[Sahana S Acharya]]></dc:creator><pubDate>Mon, 14 Apr 2025 17:32:39 GMT</pubDate><content:encoded><![CDATA[<h3 id="heading-step-1-model-modelspyhttpmodelspy">✅ Step 1: <strong>Model (</strong><a target="_blank" href="http://models.py"><strong>models.py</strong></a><strong>)</strong></h3>
<p>This defines the structure of your database table.</p>
<pre><code class="lang-javascript">pythonCopyEdit# models.py
<span class="hljs-keyword">from</span> django.db <span class="hljs-keyword">import</span> models

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Patient</span>(<span class="hljs-title">models</span>.<span class="hljs-title">Model</span>):
    <span class="hljs-title">name</span> </span>= models.CharField(max_length=<span class="hljs-number">100</span>)
    email = models.EmailField()
</code></pre>
<hr />
<h3 id="heading-step-2-form-formspyhttpformspy">✅ Step 2: <strong>Form (</strong><a target="_blank" href="http://forms.py"><strong>forms.py</strong></a><strong>)</strong></h3>
<p>This helps validate input and map it to the model.</p>
<pre><code class="lang-javascript">pythonCopyEdit# forms.py
<span class="hljs-keyword">from</span> django <span class="hljs-keyword">import</span> forms
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Patient

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PatientForm</span>(<span class="hljs-title">forms</span>.<span class="hljs-title">ModelForm</span>):
    <span class="hljs-title">class</span> <span class="hljs-title">Meta</span>:
        <span class="hljs-title">model</span> </span>= Patient
        fields = [<span class="hljs-string">'name'</span>, <span class="hljs-string">'email'</span>]
</code></pre>
<hr />
<h3 id="heading-step-3-html-template-patientformhtml">✅ Step 3: <strong>HTML Template (patient_form.html)</strong></h3>
<p>This is the form the user fills out.</p>
<pre><code class="lang-javascript">htmlCopyEdit&lt;!-- patient_form.html --&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">method</span>=<span class="hljs-string">"POST"</span>&gt;</span>
  {% csrf_token %}
  {{ form.as_p }}
  <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span></span>
</code></pre>
<hr />
<h3 id="heading-step-4-view-viewspyhttpviewspy">✅ Step 4: <strong>View (</strong><a target="_blank" href="http://views.py"><strong>views.py</strong></a><strong>)</strong></h3>
<p>This processes the form and saves the data to the database.</p>
<pre><code class="lang-javascript">pythonCopyEdit# views.py
<span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render, redirect
<span class="hljs-keyword">from</span> .forms <span class="hljs-keyword">import</span> PatientForm

def register_patient(request):
    <span class="hljs-keyword">if</span> request.method == <span class="hljs-string">'POST'</span>:
        form = PatientForm(request.POST)
        <span class="hljs-keyword">if</span> form.is_valid():
            form.save()  # ✅ This saves the data to the database
            <span class="hljs-keyword">return</span> redirect(<span class="hljs-string">'success'</span>)  # or wherever you want
    <span class="hljs-attr">else</span>:
        form = PatientForm()
    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">'patient_form.html'</span>, {<span class="hljs-string">'form'</span>: form})
</code></pre>
<hr />
<h3 id="heading-step-5-url-urlspyhttpurlspy">✅ Step 5: <strong>URL (</strong><a target="_blank" href="http://urls.py"><strong>urls.py</strong></a><strong>)</strong></h3>
<p>Connects the view to a URL.</p>
<pre><code class="lang-javascript">pythonCopyEdit# urls.py
<span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> path
<span class="hljs-keyword">from</span> . import views

urlpatterns = [
    path(<span class="hljs-string">'register/'</span>, views.register_patient, name=<span class="hljs-string">'register'</span>),
]
</code></pre>
<hr />
<h3 id="heading-final-flow">Final Flow:</h3>
<ol>
<li><p>User opens <code>/register/</code> page → sees the form.</p>
</li>
<li><p>User fills the form and clicks <strong>Submit</strong>.</p>
</li>
<li><p>Django <strong>validates</strong> the form.</p>
</li>
<li><p>If valid → <a target="_blank" href="http://form.save"><code>form.save</code></a><code>()</code> saves data to the database table <code>Patient</code>.</p>
</li>
</ol>
<p>Now, what Django is doing here with <code>forms.Form</code> is helping you on the <strong>backend</strong> — to:</p>
<p>✅ Validate the data<br />✅ Easily connect the form to your database<br />✅ Show errors automatically if something’s wrong<br />✅ Reuse the form code and make it secure</p>
<hr />
<h3 id="heading-so-heres-the-difference">So here's the difference:</h3>
<p>🖼️ <strong>Your HTML form</strong> = What the user sees and fills (frontend)<br />🐍 <strong>Django Form (</strong><code>forms.Form</code>) = Python code that helps handle and validate that data (backend)</p>
<hr />
<h3 id="heading-for-example">For example:</h3>
<p>If your HTML form sends:</p>
<pre><code class="lang-javascript">&lt;input type=<span class="hljs-string">"text"</span> name=<span class="hljs-string">"first_name"</span> /&gt;
</code></pre>
<p>Django will catch it using:</p>
<pre><code class="lang-javascript">first_name = forms.CharField(max_length=<span class="hljs-number">100</span>)
</code></pre>
<p>Then Django can check:</p>
<ul>
<li><p>Is the first name empty? ❌</p>
</li>
<li><p>Is it too long? ❌</p>
</li>
<li><p>Is the phone number exactly 10 digits? ✅ or ❌</p>
</li>
<li><p>Do the passwords match? ✅ or ❌</p>
</li>
<li><p>Did the user agree to the terms? ✅</p>
</li>
</ul>
<p>\==================================================================</p>
<p>Sure! Here's a <strong>complete and simple working example</strong> that includes:</p>
<p>✅ Django model<br />✅ Django ModelForm<br />✅ View to handle form submission<br />✅ HTML file with manual <code>&lt;input&gt;</code> fields (not <code>{{</code> <a target="_blank" href="http://form.as"><code>form.as</code></a><code>_p }}</code>)<br />✅ How data flows to the database</p>
<hr />
<h3 id="heading-1-modelspyhttpmodelspy">🧩 1. <a target="_blank" href="http://models.py"><code>models.py</code></a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">from</span> django.db <span class="hljs-keyword">import</span> models

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Patient</span>(<span class="hljs-title">models</span>.<span class="hljs-title">Model</span>):
    <span class="hljs-title">first_name</span> </span>= models.CharField(max_length=<span class="hljs-number">100</span>)
    email = models.EmailField(unique=True)

    def __str__(self):
        <span class="hljs-keyword">return</span> self.first_name
</code></pre>
<hr />
<h3 id="heading-2-formspyhttpformspy">🧩 2. <a target="_blank" href="http://forms.py"><code>forms.py</code></a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">from</span> django <span class="hljs-keyword">import</span> forms
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Patient

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PatientForm</span>(<span class="hljs-title">forms</span>.<span class="hljs-title">ModelForm</span>):
    <span class="hljs-title">class</span> <span class="hljs-title">Meta</span>:
        <span class="hljs-title">model</span> </span>= Patient
        fields = [<span class="hljs-string">'first_name'</span>, <span class="hljs-string">'email'</span>]
</code></pre>
<hr />
<h3 id="heading-3-viewspyhttpviewspy">🧩 3. <a target="_blank" href="http://views.py"><code>views.py</code></a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render, redirect
<span class="hljs-keyword">from</span> .forms <span class="hljs-keyword">import</span> PatientForm

def register_patient(request):
    <span class="hljs-keyword">if</span> request.method == <span class="hljs-string">'POST'</span>:
        form = PatientForm(request.POST)
        <span class="hljs-keyword">if</span> form.is_valid():
            form.save()
            <span class="hljs-keyword">return</span> redirect(<span class="hljs-string">'success'</span>)  # You can define a simple success page
    <span class="hljs-attr">else</span>:
        form = PatientForm()
    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">'patient_form.html'</span>, {<span class="hljs-string">'form'</span>: form})
</code></pre>
<hr />
<h3 id="heading-4-urlspyhttpurlspy">🧩 4. <a target="_blank" href="http://urls.py"><code>urls.py</code></a></h3>
<p>Make sure to add this in your <a target="_blank" href="http://urls.py"><code>urls.py</code></a>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> path
<span class="hljs-keyword">from</span> . import views

urlpatterns = [
    path(<span class="hljs-string">'register/'</span>, views.register_patient, name=<span class="hljs-string">'register'</span>),
    path(<span class="hljs-string">'success/'</span>, views.success, name=<span class="hljs-string">'success'</span>),  # optional
]
</code></pre>
<p>Add a success view if needed:</p>
<pre><code class="lang-javascript">pythonCopyEditdef success(request):
    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">'success.html'</span>)
</code></pre>
<hr />
<h3 id="heading-5-patientformhtml-manual-input-tags">🧩 5. <code>patient_form.html</code> (manual input tags)</h3>
<pre><code class="lang-javascript">htmlCopyEdit&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Patient Registration&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;h2&gt;Register&lt;/h2&gt;

  &lt;form method="POST"&gt;
    {% csrf_token %}

    &lt;label&gt;First Name:&lt;/label&gt;
    &lt;input type="text" name="first_name" required&gt;&lt;br&gt;&lt;br&gt;

    &lt;label&gt;Email:&lt;/label&gt;
    &lt;input type="email" name="email" required&gt;&lt;br&gt;&lt;br&gt;

    &lt;button type="submit"&gt;Submit&lt;/button&gt;
  &lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<hr />
<h3 id="heading-flow-summary">✅ Flow Summary</h3>
<ol>
<li><p><strong>User fills the form</strong> → HTML sends data to view via <code>POST</code></p>
</li>
<li><p><strong>View</strong> gets form data → Validates → Saves using <a target="_blank" href="http://form.save"><code>form.save</code></a><code>()</code></p>
</li>
<li><p><strong>Data gets stored in the database</strong> using Django ORM</p>
</li>
<li><p><strong>Redirect to success page</strong> (optional)</p>
</li>
<li><p>\==========================================================</p>
<h3 id="heading-the-name-attribute-in-your-html-input">✅ The <code>name</code> attribute in your HTML input:</h3>
</li>
</ol>
<pre><code class="lang-javascript">&lt;input type=<span class="hljs-string">"text"</span> name=<span class="hljs-string">"first_name"</span>&gt;
</code></pre>
<p><strong>MUST match the field name in your Django</strong> <code>ModelForm</code> — which in turn must match the name in your <code>Model</code>.</p>
<hr />
<h3 id="heading-all-3-should-be-in-sync">🔁 All 3 should be in sync:</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Layer</td><td>Field Name</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Model</strong></td><td><code>first_name</code></td></tr>
<tr>
<td><strong>Form</strong></td><td><code>first_name</code></td></tr>
<tr>
<td><strong>HTML</strong></td><td><code>name="first_name"</code></td></tr>
</tbody>
</table>
</div><hr />
<h3 id="heading-why-this-matters">✅ Why this matters:</h3>
<p>When you submit the form:</p>
<ul>
<li><p>Django collects the data using the <code>name</code> values in the HTML.</p>
</li>
<li><p>It maps those to the <code>fields</code> in the <code>ModelForm</code>.</p>
</li>
<li><p>The <code>ModelForm</code> maps those to your database model and saves them.</p>
</li>
</ul>
<p>If the names don’t match, Django won’t be able to connect the dots.</p>
<p>\=========================================================</p>
<h3 id="heading-you-should-store-data-from-the-signup-page-in-the-database">✅ You should <strong>store data from the signup page</strong> in the database.</h3>
<h4 id="heading-why">Why?</h4>
<ul>
<li><p>The <strong>signup page</strong> is where a user registers their details (like name, email, password, etc.).</p>
</li>
<li><p>This data needs to be saved in your <strong>database</strong> so you can use it later to check if the user exists when they try to log in.</p>
</li>
</ul>
<h3 id="heading-you-should-not-store-data-from-the-login-page">❌ You should <strong>not store data from the login page</strong>.</h3>
<ul>
<li><p>The <strong>login page</strong> is only for checking if the entered email/password matches what's already stored in your database.</p>
</li>
<li><p>It just verifies, not stores.</p>
</li>
</ul>
<h3 id="heading-why-use-django-forms-if-we-are-only-taking-values-and-storing-them">🌟 Why use Django forms if we are only taking values and storing them?</h3>
<p>Because Django forms do more than just capture input — they:</p>
<hr />
<h3 id="heading-1-validate-input-automatically">✅ 1. <strong>Validate input automatically</strong></h3>
<p>For example:</p>
<ul>
<li><p>Is the email valid?</p>
</li>
<li><p>Is the phone number too long?</p>
</li>
<li><p>Did the user leave a required field empty?</p>
</li>
</ul>
<p>With Django forms, you don’t need to write all those checks yourself.</p>
<hr />
<h3 id="heading-2-make-rendering-forms-easier">✅ 2. <strong>Make rendering forms easier</strong></h3>
<p>Instead of manually writing lots of <code>&lt;input&gt;</code> tags and error messages in HTML, you just write this in the template:</p>
<pre><code class="lang-javascript">htmlCopyEdit{{ form.as_p }}
</code></pre>
<p>And Django takes care of rendering all form fields with labels, input tags, and error messages (if any).</p>
<hr />
<h3 id="heading-3-make-saving-to-the-database-easier">✅ 3. <strong>Make saving to the database easier</strong></h3>
<p>Using a <code>ModelForm</code>, you can directly save data to the database with just:</p>
<pre><code class="lang-javascript">pythonCopyEditform.save()
</code></pre>
<p>No need to write code to extract values and assign them to a model manually.</p>
<p>So, <code>'patient': patient</code> is basically telling Django to pass the <code>patient</code> object to the template, and you can use the key <code>'patient'</code> to access the patient data inside the template.</p>
]]></content:encoded></item><item><title><![CDATA[Creating apps in django]]></title><description><![CDATA[1. ✅ Created a new app named chai:
python manage.py startapp chai

2. ✅ Added 'chai' to INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
    ...,
    'chai',
]

This tells Django: “Hey, I have a new app called chai — include it.”

3. ✅ Created a vie...]]></description><link>https://djangopython.hashnode.dev/creating-apps-in-django</link><guid isPermaLink="true">https://djangopython.hashnode.dev/creating-apps-in-django</guid><category><![CDATA[Django]]></category><dc:creator><![CDATA[Sahana S Acharya]]></dc:creator><pubDate>Sun, 13 Apr 2025 07:51:48 GMT</pubDate><content:encoded><![CDATA[<h3 id="heading-1-created-a-new-app-named-chai">1. ✅ Created a new app named <code>chai</code>:</h3>
<pre><code class="lang-javascript">python manage.py startapp chai
</code></pre>
<h3 id="heading-2-added-chai-to-installedapps-in-settingspyhttpsettingspy">2. ✅ Added <code>'chai'</code> to <code>INSTALLED_APPS</code> in <a target="_blank" href="http://settings.py"><code>settings.py</code></a>:</h3>
<pre><code class="lang-javascript">INSTALLED_APPS = [
    ...,
    <span class="hljs-string">'chai'</span>,
]
</code></pre>
<p>This tells Django: “Hey, I have a new app called chai — include it.”</p>
<hr />
<h3 id="heading-3-created-a-view-in-chaiviewspyhttpviewspy">3. ✅ Created a <strong>view</strong> in <code>chai/</code><a target="_blank" href="http://views.py"><code>views.py</code></a>:</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render

def all_chai(request):
    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">'chai/all_chai.html'</span>)
</code></pre>
<p>This will return an HTML page (<code>all_chai.html</code>) from the <code>templates/chai/</code> folder.</p>
<hr />
<h3 id="heading-4-set-up-routing">4. ✅ Set up routing</h3>
<h4 id="heading-in-projecturlspyhttpurlspy-main-project">In <code>project/</code><a target="_blank" href="http://urls.py"><code>urls.py</code></a> (main project):</h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">from</span> django.contrib <span class="hljs-keyword">import</span> admin
<span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> path, include

urlpatterns = [
    path(<span class="hljs-string">'admin/'</span>, admin.site.urls),
    path(<span class="hljs-string">'chai/'</span>, include(<span class="hljs-string">'chai.urls'</span>)),  # ✅ Connected chai app
]
</code></pre>
<h4 id="heading-in-chaiurlspyhttpurlspy">In <code>chai/</code><a target="_blank" href="http://urls.py"><code>urls.py</code></a>:</h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> path
<span class="hljs-keyword">from</span> . import views

urlpatterns = [
    path(<span class="hljs-string">''</span>, views.all_chai, name=<span class="hljs-string">'all_home'</span>),  # http:<span class="hljs-comment">//localhost:8000/chai/</span>
    # path(<span class="hljs-string">'order/'</span>, views.order_chai, name=<span class="hljs-string">'order'</span>), ← You can add more views like <span class="hljs-built_in">this</span> later
]
</code></pre>
<hr />
<h3 id="heading-5-created-a-template-chaitemplateschaiallchaihtml">5. ✅ Created a template: <code>chai/templates/chai/all_chai.html</code></h3>
<pre><code class="lang-javascript">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;title&gt;All Chai&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;All types of available chai&lt;/h1&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>✅ The folder name <code>chai/templates/chai/</code> is correct — Django will automatically look for it when <code>APP_DIRS=True</code> in settings.</p>
<hr />
<h2 id="heading-final-checkpoint-what-should-work-now">🔍 Final Checkpoint (What should work now)</h2>
<p>When you run:</p>
<pre><code class="lang-javascript">python manage.py runserver
</code></pre>
<p>and visit:</p>
<pre><code class="lang-javascript">http:<span class="hljs-comment">//localhost:8000/chai/</span>
</code></pre>
<p>➡️ You should see your page with:</p>
<pre><code class="lang-javascript">All types <span class="hljs-keyword">of</span> available chai
</code></pre>
<h2 id="heading-purpose-of-layouthtml">✅ Purpose of <code>layout.html</code></h2>
<p>This is your <strong>base layout</strong>, which means:</p>
<ul>
<li><p>All other HTML pages will <strong>extend it</strong></p>
</li>
<li><p>You won’t have to repeat the same <code>&lt;html&gt;</code>, <code>&lt;head&gt;</code>, <code>&lt;nav&gt;</code>, etc.</p>
</li>
<li><p>Only unique content (like title and body) changes using <code>{% block %}</code></p>
</li>
</ul>
<hr />
<h2 id="heading-example-layouthtml">✅ Example: <code>layout.html</code></h2>
<p>Already perfect 👇</p>
<pre><code class="lang-javascript">{% load <span class="hljs-keyword">static</span> %}
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;title&gt;{% block title %}Default value{% endblock %}&lt;/title&gt;
    &lt;link rel="stylesheet" href="{% static 'style.css' %}"&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;nav&gt;This is navbar&lt;/nav&gt;
    {% block content %}{% endblock %}
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<hr />
<h2 id="heading-example-indexhtml">✅ Example: <code>index.html</code></h2>
<pre><code class="lang-javascript">{% <span class="hljs-keyword">extends</span> <span class="hljs-string">"layout.html"</span> %}

{% block title %}Home Page{% endblock %}

{% block content %}
&lt;h1&gt;Chai aur Code | Home Page&lt;/h1&gt;
{% endblock %}
</code></pre>
<hr />
<h2 id="heading-now-in-the-chai-app-do-the-same">✅ Now in the <code>chai</code> app: Do the Same!</h2>
<p>Let’s say you have this file:</p>
<p>📁 <code>chai/templates/chai/all_chai.html</code></p>
<pre><code class="lang-javascript">{% <span class="hljs-keyword">extends</span> <span class="hljs-string">"layout.html"</span> %}

{% block title %}All Chai{% endblock %}

{% block content %}
&lt;h1&gt;All types <span class="hljs-keyword">of</span> available chai 🍵&lt;/h1&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Masala Chai<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>Ginger Chai<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>Tandoori Chai<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>Green Tea<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>
{% endblock %}
</code></pre>
<p>➡️ It’ll look the same with navbar and CSS from <code>layout.html</code>, but its own content.</p>
]]></content:encoded></item><item><title><![CDATA[🌐 What is Django?]]></title><description><![CDATA[Django is a Python-based web framework that helps you build web applications quickly and with clean, maintainable code. It’s like a toolset that gives you everything you need to make a website: handle URLs, work with databases, manage users, etc.

No...]]></description><link>https://djangopython.hashnode.dev/what-is-django</link><guid isPermaLink="true">https://djangopython.hashnode.dev/what-is-django</guid><category><![CDATA[Django]]></category><dc:creator><![CDATA[Sahana S Acharya]]></dc:creator><pubDate>Sat, 12 Apr 2025 08:49:09 GMT</pubDate><content:encoded><![CDATA[<p>Django is a <strong>Python-based web framework</strong> that helps you build web applications quickly and with clean, maintainable code. It’s like a toolset that gives you everything you need to make a website: handle URLs, work with databases, manage users, etc.</p>
<hr />
<p>Now let’s understand your code <strong>step-by-step</strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">from</span> django.http <span class="hljs-keyword">import</span> HttpResponse
</code></pre>
<ul>
<li><p>✅ We’re importing <strong>HttpResponse</strong> from Django’s HTTP module.</p>
</li>
<li><p>✅ This is used to <strong>send back a response</strong> (like text, HTML, or data) to the browser when someone visits a URL.</p>
</li>
</ul>
<hr />
<pre><code class="lang-javascript">def home(request):
    <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">"hello world i am in home page"</span>)
</code></pre>
<ul>
<li><p>✅ This is a <strong>function-based view</strong> called <code>home</code>.</p>
</li>
<li><p>✅ It takes one parameter: <code>request</code> (which contains info like browser type, user info, GET/POST data, etc.)</p>
</li>
<li><p>✅ <code>return HttpResponse(...)</code> sends back a simple plain text response: <code>"hello world i am in home page"</code></p>
</li>
</ul>
<p>So if someone visits the URL linked to this view, they’ll see:</p>
<pre><code class="lang-javascript">hello world i am <span class="hljs-keyword">in</span> home page
</code></pre>
<hr />
<h3 id="heading-what-else-do-we-need-for-this-to-work">🧭 What else do we need for this to work?</h3>
<p>To <strong>see this page in the browser</strong>, we need to:</p>
<ol>
<li><strong>Map a URL</strong> to this view in <a target="_blank" href="http://urls.py"><code>urls.py</code></a></li>
</ol>
<p>Example <a target="_blank" href="http://urls.py"><code>urls.py</code></a>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> path
<span class="hljs-keyword">from</span> . import views  # assuming <span class="hljs-built_in">this</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">is</span> <span class="hljs-title">in</span> <span class="hljs-title">views</span>.<span class="hljs-title">py</span>

<span class="hljs-title">urlpatterns</span> = [
    <span class="hljs-title">path</span>(<span class="hljs-params"><span class="hljs-string">''</span>, views.home</span>),  # <span class="hljs-title">shows</span> <span class="hljs-title">home</span> <span class="hljs-title">page</span> <span class="hljs-title">on</span> <span class="hljs-title">root</span> <span class="hljs-title">URL</span>
]</span>
</code></pre>
<p>🧭 Example flow:</p>
<pre><code class="lang-javascript">&lt;!-- home.html --&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/about/"</span>&gt;</span>Go to About Page<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span></span>
</code></pre>
<p>In <a target="_blank" href="http://urls.py"><code>urls.py</code></a>, you map <code>/about/</code> to a Django view:</p>
<pre><code class="lang-javascript">path(<span class="hljs-string">'about/'</span>, views.about),
</code></pre>
<p>So clicking the link sends a <strong>GET request</strong> to Django → Django runs the <code>about</code> view → returns <code>about.html</code>.</p>
<h2 id="heading-from-djangoshortcuts-import-render">🧩 <code>from django.shortcuts import render</code></h2>
<p>This line means:</p>
<blockquote>
<p>"Import the <code>render</code> function from Django so we can send HTML templates (like <code>.html</code> files) to the browser."</p>
</blockquote>
<hr />
<h2 id="heading-why-use-render">🛠️ Why use <code>render()</code>?</h2>
<p>You use <code>render()</code> when:</p>
<ul>
<li><p>You want to return a <strong>full HTML page</strong> (not just text like <code>HttpResponse</code>).</p>
</li>
<li><p>You want to <strong>connect Django to your HTML templates</strong>.</p>
</li>
</ul>
<hr />
<h3 id="heading-example">🔁 Example:</h3>
<p><a target="_blank" href="http://views.py"><strong>views.py</strong></a></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render

def home(request):
    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">'home.html'</span>)  # Looks <span class="hljs-keyword">for</span> home.html <span class="hljs-keyword">in</span> templates folder
</code></pre>
<h2 id="heading-urlspyhttpurlspy-explained">🔍 <a target="_blank" href="http://urls.py"><code>urls.py</code></a> Explained</h2>
<pre><code class="lang-javascript"><span class="hljs-keyword">from</span> . import views
</code></pre>
<p>✅ This imports the <a target="_blank" href="http://views.py"><code>views.py</code></a> file <strong>from the same directory</strong> (your app folder), so you can use the functions like <code>home</code>, <code>about</code>, <code>contact</code>.</p>
<hr />
<h3 id="heading-your-url-patterns">🔽 Your URL patterns:</h3>
<pre><code class="lang-javascript">urlpatterns = [
    path(<span class="hljs-string">'admin/'</span>, admin.site.urls),
</code></pre>
<p>✅ This is the default Django admin route — lets you go to the admin panel at <a target="_blank" href="http://localhost:8000/admin"><code>http://localhost:8000/admin</code></a>.</p>
<hr />
<pre><code class="lang-javascript">    path(<span class="hljs-string">''</span>, views.home, name=<span class="hljs-string">'home'</span>),
</code></pre>
<p>✅ This means:</p>
<ul>
<li><p>If someone visits the <strong>root URL</strong> (<a target="_blank" href="http://localhost:8000/"><code>http://localhost:8000/</code></a>), call the <code>home()</code> function in <a target="_blank" href="http://views.py"><code>views.py</code></a>.</p>
</li>
<li><p>You gave it a <code>name='home'</code> — useful if you want to refer to it inside templates using <code>{% url 'home' %}</code>.</p>
</li>
</ul>
<hr />
<pre><code class="lang-javascript">    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>✅ These handle:</p>
<ul>
<li><p><a target="_blank" href="http://localhost:8000/about/"><code>http://localhost:8000/about/</code></a> → <code>about()</code> view</p>
</li>
<li><p><a target="_blank" href="http://localhost:8000/contact/"><code>http://localhost:8000/contact/</code></a> → <code>contact()</code> view</p>
</li>
</ul>
<h3 id="heading-what-dirs-templates-means">📁 What <code>'DIRS': ['templates']</code> Means</h3>
<p>You're telling Django:</p>
<blockquote>
<p>“Hey Django, look for HTML files inside a folder called <code>templates</code> that is in the <strong>project root</strong>.”</p>
</blockquote>
<hr />
<h3 id="heading-in-the-html">In the HTML:</h3>
<pre><code class="lang-javascript">{% load <span class="hljs-keyword">static</span> %}
</code></pre>
<p>✅ This tells Django to <strong>load the static tag</strong>, so you can use static files like CSS, images, or JavaScript.</p>
<pre><code class="lang-javascript">&lt;link rel=<span class="hljs-string">"stylesheet"</span> href=<span class="hljs-string">"{% static 'style.css' %}"</span>&gt;
</code></pre>
<p>✅ This loads <code>style.css</code> from your <code>static</code> folder. ✅✅✅</p>
<hr />
<h3 id="heading-2-in-settingspyhttpsettingspy">2. In <a target="_blank" href="http://settings.py"><code>settings.py</code></a>:</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> os

STATIC_URL = <span class="hljs-string">'static/'</span>
STATICFILES_DIRS = [os.path.join(BASE_DIR, <span class="hljs-string">'static'</span>)]
</code></pre>
<p>✅ You're telling Django:</p>
<blockquote>
<p>"All my static files (CSS, images, JS) are in a folder called <code>static</code> inside the main project directory."</p>
</blockquote>
<hr />
<h3 id="heading-your-project-folder-structure-should-look-like">✅ Your Project Folder Structure Should Look Like:</h3>
<pre><code class="lang-javascript">arduinoCopyEditmyproject/
├── manage.py
├── myapp/
│   └── views.py
├── <span class="hljs-keyword">static</span>/
│   └── style.css   ← ✅ Your CSS file
├── templates/
│   └── home.html   ← ✅ Your template
├── myproject/
│   └── settings.py
</code></pre>
<hr />
<h2 id="heading-what-happens">🔁 What Happens:</h2>
<p>When you run your server (<code>python</code> <a target="_blank" href="http://manage.py"><code>manage.py</code></a> <code>runserver</code>) and open the homepage:</p>
<ul>
<li><p>Django looks at <code>home.html</code></p>
</li>
<li><p>It sees <code>{% static 'style.css' %}</code> and replaces it with something like <code>/static/style.css</code></p>
</li>
<li><p>It serves the CSS from <code>static/style.css</code></p>
</li>
<li><p>Your button and styles show up 🎨</p>
</li>
</ul>
<hr />
<h2 id="heading-your-button-works-too">✅ Your Button Works Too!</h2>
<pre><code class="lang-javascript">htmlCopyEdit&lt;a href=<span class="hljs-string">"{% url 'contact' %}"</span>&gt;
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>Click Here<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>
&lt;/a&gt;
</code></pre>
<p>This will send you to the <code>/contact/</code> URL — and Django will call the <code>contact()</code> view.</p>
<hr />
]]></content:encoded></item></channel></rss>