{"id":131,"date":"2025-04-04T07:06:24","date_gmt":"2025-04-04T07:06:24","guid":{"rendered":"https:\/\/kingstatue.com\/?p=131"},"modified":"2025-04-04T07:06:24","modified_gmt":"2025-04-04T07:06:24","slug":"how-django-works","status":"publish","type":"post","link":"https:\/\/www.kingstatue.com\/?p=131","title":{"rendered":"How Django Works?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Django is a <strong>high-level Python web framework<\/strong> that follows the <strong>Model-View-Template (MVT)<\/strong> architectural pattern. It is designed to help developers build clean, secure and scalable web applications quickly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How Django Works?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Django handles <strong>HTTP requests<\/strong> and <strong>responses<\/strong> using the following process:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Client Sends Request:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A user enters a URL in the browser and sends request (e.g., https:\/\/example.com\/home).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Django URL Dispatcher:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Django&#8217;s URL dispatcher (urls.py) maps the request to the corresponding <strong>view function<\/strong><br>Django URL dispatcher (urls.py) checks to find a matching <strong>URL pattern<\/strong>.<br>If a match is found, Django <strong>calls the corresponding view function<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>View (function) Processes the Request<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It processes the request<br>The view retrieves data from the <strong>model<\/strong> (database) if needed.<br>Processes the logic<br>It passes the data to a <strong>template<\/strong> (HTML page, JSON data, etc).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Template Rendering:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The template dynamically generates the HTML content.<br>If the response requires an HTML page, Django loads the <strong>Template<\/strong> (.html file) and injects dynamic data into it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Response Sent to the User<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The generated response (HTML, JSON, etc.) is sent back to the user\u2019s browser.<br><strong>Django Project Directory Structure<\/strong><br>When you create a Django project using: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>django-admin startproject myproject<\/strong><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>A directory named myproject\/ is created with the following structure:<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>myproject\/\n\u2502\u2500\u2500 manage.py\n\u2502\u2500\u2500 templates (Django template files html files are stored in this directory)\n\u2502\u2500\u2500 db.sqlite3\n\u2502\u2500\u2500 myproject\/  # Main project package\n\u2502   \u2502\u2500\u2500 __init__.py\n\u2502   \u2502\u2500\u2500 settings.py\n\u2502   \u2502\u2500\u2500 urls.py\n\u2502   \u2502\u2500\u2500 asgi.py\n\u2502   \u2514\u2500\u2500 wsgi.py\n\n\u2514\u2500\u2500 app_name\/  (Created when running `startapp`)\n    \u2502\u2500\u2500 migrations\/\n    \u2502\u2500\u2500 __init__.py\n    \u2502\u2500\u2500 admin.py\n    \u2502\u2500\u2500 apps.py\n    \u2502\u2500\u2500 models.py\n    \u2502\u2500\u2500 tests.py\n    \u2502\u2500\u2500 views.py\n    \u2502\u2500\u2500 tests.py\n    \u2502\u2500\u2500 templates\/\n    \u2502\u2500\u2500 static\/\n    \u2514\u2500\u2500 urls.py  (Manually created for app-specific routes)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Explanation of Important Files<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. manage.py<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A command-line tool to <strong>run the Django project.<\/strong><\/li>\n\n\n\n<li>Used for running the server, creating apps, applying migrations, managing users, etc.<\/li>\n\n\n\n<li>Example commands:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>python manage.py runserver   # Start the development server\npython manage.py makemigrations   # Create migration files\npython manage.py migrate   # Apply migrations to the database\npython manage.py createsuperuser  # Create an admin user<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. myproject\/ (Main Project Folder)<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>a) __init__.py<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An empty file that tells Python to treat this directory as a package.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>module<\/strong> is simply a <strong>Python file (<code>.py<\/code>)<\/strong> that contains <strong>functions, classes, or variables<\/strong>. It allows code to be logically organized.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Create a file named <code>math_utils.py<\/code>:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># math_utils.py\n\ndef add(a, b):\n    return a + b\n\ndef subtract(a, b):\n    return a - b\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Using the Module in Another File<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># main.py\nimport math_utils\n\nprint(math_utils.add(5, 3))      # Output: 8\nprint(math_utils.subtract(5, 3)) # Output: 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>You can also import specific functions:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from math_utils import add\nprint(add(10, 20))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>package<\/strong> is a <strong>directory that contains a collection of modules and a special file named <code>__init__.py<\/code><\/strong> (can be empty). It allows you to organize related modules together.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_package\/\n\u2502\n\u251c\u2500\u2500 __init__.py\n\u251c\u2500\u2500 math_utils.py\n\u2514\u2500\u2500 string_utils.py\n\n<strong>math_utils.py<\/strong>\n\ndef multiply(a, b):\n    return a * b\n\n<strong>string_utils.py<\/strong>\n\ndef greet(name):\n    return f\"Hello, {name}!\"\n\n<strong>Using the Package in a Script<\/strong>\n\n# main.py\nfrom my_package import math_utils, string_utils\n\nprint(math_utils.multiply(3, 4))       # Output: 12\nprint(string_utils.greet(\"Krishna\"))   # Output: Hello, Krishna!\n\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>b) settings.py<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Main configuration file for the Django project.<\/li>\n\n\n\n<li>Contains settings for:\n<ul class=\"wp-block-list\">\n<li><strong>Database Configuration<\/strong> (DATABASES)<\/li>\n\n\n\n<li><strong>Installed Apps<\/strong> (INSTALLED_APPS)<\/li>\n\n\n\n<li><strong>Middleware<\/strong> (MIDDLEWARE)<\/li>\n\n\n\n<li><strong>Templates Configuration<\/strong> (TEMPLATES)<\/li>\n\n\n\n<li><strong>Static and Media Files<\/strong> (STATIC_URL, MEDIA_URL)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>c) urls.py<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Defines URL routing patterns for the project.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.contrib import admin\nfrom django.urls import path, include\nurlpatterns = &#91;\n    path('admin\/', admin.site.urls),\n    path('app1\/', include('app1.urls')),  # Include app1's URLs ]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>d) asgi.py &amp; wsgi.py<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>wsgi.py<\/strong>: Entry point for WSGI applications. Used for deploying Django projects on production servers. (WSGI servers e.g., Gunicorn, uWSGI))<\/li>\n\n\n\n<li><strong>asgi.py<\/strong>: Entry point for ASGI applications for async operations (WebSockets, real-time applications)..<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. db.sqlite3 (Database)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Default SQLite database file.<\/li>\n\n\n\n<li>Can be changed to <strong>PostgreSQL, MySQL, or other databases<\/strong> in settings.py.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. app1\/ (Django App)<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Each Django project can have multiple <strong>apps<\/strong>, each responsible for a specific functionality and an app has its own structure:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>migrations\/<\/strong> \u2192 Stores database migration files (generated by makemigrations)..<\/li>\n\n\n\n<li><strong>models.py<\/strong> \u2192 Defines the database models (tables) using Django ORM<\/li>\n\n\n\n<li><strong>views.py<\/strong> \u2192 Contains logic for handling user requests and handles the logic for rendering webpages.<\/li>\n\n\n\n<li><strong>urls.py<\/strong> (manually created) \u2192 Defines URLs specific to the app.<\/li>\n\n\n\n<li><strong>admin.py<\/strong> \u2192 Registers models for Django&#8217;s admin panel.<\/li>\n\n\n\n<li><strong>apps.py<\/strong> \u2192 Defines configuration for the app.<\/li>\n\n\n\n<li><strong>tests.py<\/strong> \u2192 Contains test cases for the app.<\/li>\n\n\n\n<li><strong>templates\/ (HTML Files)<\/strong> Stores HTML templates for rendering dynamic webpages.<\/li>\n\n\n\n<li><strong>static\/ (CSS, JS, Images)<\/strong> Stores static files like CSS, JavaScript, and images.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating a Simple Django App<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Create a Django project:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>django-admin startproject myshop\ncd myshop<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create an app:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>python manage.py startapp testapp<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Define a model (<code>models.py<\/code> in <code>myapp\/<\/code>)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.db import models\n\nclass Book(models.Model):\n    title = models.CharField(max_length=100)\n    author = models.CharField(max_length=50)\n    published_date = models.DateField()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create and apply migrations:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>python manage.py makemigrations myapp\npython manage.py migrate<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Define a view (<code>views.py<\/code> in <code>myapp\/<\/code>)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.http import HttpResponse\n\ndef home(request):\n    return HttpResponse(\"Hello, Django!\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Map URL to the view (<code>urls.py<\/code> in <code>myapp\/<\/code>)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.urls import path\nfrom .views import home\n\nurlpatterns = &#91;\n    path('', home),\n]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Include app URLs in the main project (<code>urls.py<\/code> in <code>myproject\/<\/code>)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.contrib import admin\nfrom django.urls import path, include\n\nurlpatterns = &#91;\n    path('admin\/', admin.site.urls),\n    path('', include('myapp.urls')),\n]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Run the server<\/strong>:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>python manage.py runserver<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Visit<\/strong> <code>http:\/\/127.0.0.1:8000\/<\/code> in your browser to see <strong>&#8220;Hello, Django!&#8221;<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Summary<\/strong><\/h3>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Django is a powerful and structured framework that simplifies web development with its built-in functionalities<\/li>\n\n\n\n<li><strong>Django<\/strong> follows the <strong>MVT (Model-View-Template)<\/strong> architecture.<\/li>\n\n\n\n<li>The <strong>project directory<\/strong> is neatly organized into::\n<ul class=\"wp-block-list\">\n<li>manage.py (Project management tool)<\/li>\n\n\n\n<li>settings.py (Configuration)<\/li>\n\n\n\n<li>urls.py (URL routing)<\/li>\n\n\n\n<li>models.py (Database models)<\/li>\n\n\n\n<li>views.py (Business logic)<\/li>\n\n\n\n<li>templates\/ (HTML files)<\/li>\n\n\n\n<li>static\/ (CSS, JS, images)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>A Django project can have <strong>multiple apps<\/strong>, each managing a specific functionality.<\/li>\n\n\n\n<li>The <strong>app-level directories<\/strong> (handling models, views, and templates). Understanding this structure helps in building and managing scalable Django applications efficiently.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Django is a high-level Python web framework that follows the Model-View-Template (MVT) architectural pattern. It is designed to help developers build clean, secure and scalable web applications quickly. How Django&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[42,48,55,59,74,79,81,82],"class_list":["post-131","post","type-post","status-publish","format-standard","hentry","category-django","tag-controller","tag-django","tag-model","tag-mvt","tag-sqlite","tag-template","tag-url","tag-view"],"_links":{"self":[{"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=\/wp\/v2\/posts\/131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=131"}],"version-history":[{"count":0,"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=\/wp\/v2\/posts\/131\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}