{"id":1211,"date":"2024-10-15T09:57:35","date_gmt":"2024-10-15T07:57:35","guid":{"rendered":"https:\/\/programmier-workshops.de\/blog\/?p=1211"},"modified":"2024-10-15T09:57:35","modified_gmt":"2024-10-15T07:57:35","slug":"lesson-understanding-django","status":"publish","type":"post","link":"https:\/\/programmier-workshops.de\/blog\/lesson-understanding-django\/","title":{"rendered":"Lesson: Understanding Django"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/programmier-workshops.de\/blog\/lesson-understanding-django\/#What_is_Django\" >What is Django?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/programmier-workshops.de\/blog\/lesson-understanding-django\/#Django_Architecture\" >Django Architecture<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/programmier-workshops.de\/blog\/lesson-understanding-django\/#Benefits_of_Django_Architecture\" >Benefits of Django Architecture<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/programmier-workshops.de\/blog\/lesson-understanding-django\/#Creating_a_New_Project_in_Django\" >Creating a New Project in Django<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/programmier-workshops.de\/blog\/lesson-understanding-django\/#Creating_Django_Apps\" >Creating Django Apps<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/programmier-workshops.de\/blog\/lesson-understanding-django\/#Configuring_the_Database\" >Configuring the Database<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_Django\"><\/span>What is Django?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Django is a free, open-source web application framework written in Python. A framework is essentially a collection of modules that simplify the development process. These modules are organized together, allowing developers to build applications or websites using pre-existing resources, instead of starting from scratch.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When developing a website, there are often common components that are required, such as user authentication (registration, login, and logout), an administration panel, forms, file uploads, etc. Frameworks are designed to prevent the need to reinvent these common elements and help reduce some of the overhead involved in building a new website. Django is one such framework.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">According to the official project website, Django is described as &#8220;a high-level Python web framework that encourages rapid development and clean, pragmatic design. It handles much of the hassle of web development, allowing you to focus on writing your application without needing to reinvent the wheel. It&#8217;s free and open-source.&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Django provides a vast array of modules that can be utilized independently. Fundamentally, frameworks exist to save developers time and effort, and Django is no exception.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Django_Architecture\"><\/span>Django Architecture<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Django follows the Model-View-Template (MVT) architectural pattern.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>M<\/strong> stands for Model<\/li>\n\n\n\n<li><strong>V<\/strong> stands for View<\/li>\n\n\n\n<li><strong>T<\/strong> stands for Template<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">MVT is similar to the Model-View-Controller (MVC) pattern. The key difference between MVT and MVC is that Django handles the controller&#8217;s responsibilities using templates. Specifically, the template file combines HTML and Django Template Language (DTL) to manage the controller&#8217;s role in MVC.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Benefits_of_Django_Architecture\"><\/span>Benefits of Django Architecture<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The Django framework is based on this architecture, which enables efficient communication between these three components without requiring complex code. This contributes to Django&#8217;s growing popularity. Some key advantages of Django&#8217;s architecture include:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Rapid Development<\/strong>: The separation of components in Django architecture allows multiple developers to work simultaneously on different parts of the application. This is one of Django\u2019s standout features.<\/li>\n\n\n\n<li><strong>Loosely Coupled<\/strong>: Django\u2019s components are interdependent only when necessary, which enhances the security of the overall website. For instance, the model file is stored on the server rather than on the web page.<\/li>\n\n\n\n<li><strong>Ease of Modification<\/strong>: Due to the distinct components in Django\u2019s architecture, changes in one part of the application do not affect other parts. This feature grants developers greater flexibility compared to other frameworks.<\/li>\n\n\n\n<li><strong>Security<\/strong>: When developing high-end web applications, security is a crucial consideration. Django addresses this concern by offering strong protection against threats such as click-jacking, cross-site scripting (XSS), and SQL injections. Additionally, user authentication features ensure secure management of user accounts and passwords.<\/li>\n\n\n\n<li><strong>Scalability<\/strong>: Scalability refers to an application\u2019s ability to perform well as its size or user base grows. Django excels in this area, with websites built using Django capable of handling multiple users simultaneously. Well-known websites such as Spotify, Netflix, YouTube, Mozilla, and Quora use Django for this reason.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_a_New_Project_in_Django\"><\/span>Creating a New Project in Django<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To create a new Django project, use the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">django-admin startproject myproject<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command generates the base folder structure for a Django project, which looks like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">myproject\/                  &lt;-- top-level folder\n |-- myproject\/             &lt;-- Django project folder\n |    |-- myproject\/\n |    |    |-- __init__.py\n |    |    |-- settings.py\n |    |    |-- urls.py\n |    |    |-- wsgi.py\n |    +-- manage.py<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>manage.py<\/strong>: A shortcut to the <code>django-admin<\/code> command-line utility. It is used to run management commands related to the project, such as running the development server, testing, and creating migrations.<\/li>\n\n\n\n<li><strong>init.py<\/strong>: This empty file designates the directory as a Python package.<\/li>\n\n\n\n<li><strong>settings.py<\/strong>: Contains all project configurations and will be frequently referenced.<\/li>\n\n\n\n<li><strong>urls.py<\/strong>: Responsible for mapping routes and paths within the project. For example, if you want to display content at <code>\/about\/<\/code>, you must map it in this file.<\/li>\n\n\n\n<li><strong>wsgi.py<\/strong>: A simple Web Server Gateway Interface (WSGI) used for deployment.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">To run the server, use the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python manage.py runserver<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Opening <code>http:\/\/127.0.0.1:8000<\/code> in a web browser will display a success page, indicating that the server is running correctly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_Django_Apps\"><\/span>Creating Django Apps<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A Django project can contain multiple apps. To create an app, use the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">django-admin startapp articles<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command generates the following structure:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">myproject\/\n |-- myproject\/\n |    |-- articles\/                &lt;-- new Django app\n |    |    |-- migrations\/\n |    |    |    +-- __init__.py\n |    |    |-- __init__.py\n |    |    |-- admin.py\n |    |    |-- apps.py\n |    |    |-- models.py\n |    |    |-- tests.py\n |    |    +-- views.py<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>migrations\/<\/strong>: Stores files that track database changes.<\/li>\n\n\n\n<li><strong>admin.py<\/strong>: Configures the built-in Django Admin app.<\/li>\n\n\n\n<li><strong>apps.py<\/strong>: Configures the app itself.<\/li>\n\n\n\n<li><strong>models.py<\/strong>: Defines the web application\u2019s entities, which Django automatically translates into database tables.<\/li>\n\n\n\n<li><strong>tests.py<\/strong>: Used for writing unit tests.<\/li>\n\n\n\n<li><strong>views.py<\/strong>: Handles the request-response cycle.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">To configure the project to use the app, open the <code>settings.py<\/code> file and find the <code>INSTALLED_APPS<\/code> block:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">INSTALLED_APPS = [\n    'django.contrib.admin',\n    'django.contrib.auth',\n    'django.contrib.contenttypes',\n    'django.contrib.sessions',\n    'django.contrib.messages',\n    'django.contrib.staticfiles',\n    'articles',\n]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">By adding <code>articles<\/code> to the <code>INSTALLED_APPS<\/code> section, the app is ready to be used in the project.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Configuring_the_Database\"><\/span>Configuring the Database<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">By default, Django uses SQLite as the database, but it can be customized to use MySQL or PostgreSQL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, to configure PostgreSQL, edit the <code>settings.py<\/code> file as follows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.postgresql_psycopg2',\n        'NAME': 'blog',\n        'USER': 'postgres',\n        'PASSWORD': '**********',\n        'HOST': 'localhost',\n        'PORT': 5432,\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With this configuration, your project will be connected to a PostgreSQL database and ready to run.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In conclusion, this guide covers the fundamental aspects of Django&#8217;s MVT pattern, project structure, and database configuration.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Django? Django is a free, open-source web application framework written in Python. A framework is essentially a collection of modules that simplify the development process. These modules are organized together, allowing developers to build applications or websites using pre-existing resources, instead of starting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,9,3,33],"tags":[],"class_list":["post-1211","post","type-post","status-publish","format-standard","hentry","category-coding4beginner","category-css","category-kurs","category-kurs-lesson"],"_links":{"self":[{"href":"https:\/\/programmier-workshops.de\/blog\/wp-json\/wp\/v2\/posts\/1211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/programmier-workshops.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/programmier-workshops.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/programmier-workshops.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/programmier-workshops.de\/blog\/wp-json\/wp\/v2\/comments?post=1211"}],"version-history":[{"count":1,"href":"https:\/\/programmier-workshops.de\/blog\/wp-json\/wp\/v2\/posts\/1211\/revisions"}],"predecessor-version":[{"id":1212,"href":"https:\/\/programmier-workshops.de\/blog\/wp-json\/wp\/v2\/posts\/1211\/revisions\/1212"}],"wp:attachment":[{"href":"https:\/\/programmier-workshops.de\/blog\/wp-json\/wp\/v2\/media?parent=1211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/programmier-workshops.de\/blog\/wp-json\/wp\/v2\/categories?post=1211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/programmier-workshops.de\/blog\/wp-json\/wp\/v2\/tags?post=1211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}