[{"data":1,"prerenderedAt":124},["ShallowReactive",2],{"tutorial-django-tutorial-for-beginners-part-1":3},{"id":4,"meta":5,"title":21,"author":22,"body":26,"introduction":122,"category":123},170,{"type":6,"detail_url":7,"html_url":8,"slug":9,"show_in_menus":10,"seo_title":11,"search_description":11,"first_published_at":12,"alias_of":13,"parent":14},"tutorial.TutorialPage","https:\u002F\u002Fwww.staging.doprax.rocks\u002Fd\u002Fapi\u002Fv2\u002Fpages\u002F170\u002F","https:\u002F\u002Fwww.staging.doprax.rocks\u002Fd\u002Fdoprax-tutorials\u002Fdjango-tutorial-for-beginners-part-1\u002F","django-tutorial-for-beginners-part-1",false,"","2025-02-18T07:32:59.126562Z",null,{"id":15,"meta":16,"title":20},158,{"type":17,"detail_url":18,"html_url":19},"tutorial.TutorialIndexPage","https:\u002F\u002Fwww.staging.doprax.rocks\u002Fd\u002Fapi\u002Fv2\u002Fpages\u002F158\u002F","https:\u002F\u002Fwww.staging.doprax.rocks\u002Fd\u002Fdoprax-tutorials\u002F","Tutorials","Django Tutorial for Beginners [Step by Step] – Part 1: Creating a Django Project & Building a Simple View",{"id":23,"username":24,"full_name":25},2,"juls","Juliet Ofoegbu",[27,32,37,43,48,53,57,63,68,73,78,82,87,93,99,104,108,114,118],{"type":28,"value":29,"id":31},"section",{"title":11,"content":30,"code":11,"image":11},"\u003Cp data-block-key=\"sl999\">\u003Cb>Who is this tutorial for?\u003C\u002Fb>\u003C\u002Fp>\u003Cp data-block-key=\"al3jc\">This step-by-step guide is designed for absolute beginners who want to learn Django from the ground up. The concepts are broken down into 10 parts to make learning easier. If you're new to web development, it may seem overwhelming at first, but mastering the basics through practice will set you on the right path.\u003C\u002Fp>\u003Cp data-block-key=\"c30ij\">For an advanced guide on deploying a Django project with MySQL and Nginx using Docker, check out our article on: \u003Cb>Deploying a Django Project with Docker, Nginx, and MySQL\u003C\u002Fb>\u003C\u002Fp>","42c8fb84-7307-47d9-8b2c-8d2f85a26b6a",{"type":28,"value":33,"id":36},{"title":34,"content":35,"code":11,"image":11},"What Is Django?","\u003Cp data-block-key=\"brgsp\">Django is a Python-based web framework designed to help developers build powerful web applications quickly and efficiently. It supports both Python 2 and 3 and is named after the legendary jazz guitarist \u003Ca href=\"https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FDjango_(web_framework)#:~:text=The%20framework%20was%20named%20after,maintain%20Django%20in%20the%20future.\">Django Reinhardt\u003C\u002Fa>.\u003C\u002Fp>\u003Cp data-block-key=\"8do1b\">A framework is essentially a collection or toolbox of pre-built components that help streamline development by preventing repetitive coding for similar tasks. It's like reusable code bundled in a reusable (but often inflexible) architecture.\u003C\u002Fp>\u003Cp data-block-key=\"b9fk6\">Django provides an organized structure, making it easier to build scalable applications with built-in tools for:\u003C\u002Fp>\u003Cul>\u003Cli data-block-key=\"745pk\">\u003Cb>Database management (ORM)\u003C\u002Fb>\u003C\u002Fli>\u003Cli data-block-key=\"crib8\">\u003Cb>Templating\u003C\u002Fb>\u003C\u002Fli>\u003Cli data-block-key=\"dvbvu\">\u003Cb>Form validation\u003C\u002Fb>\u003C\u002Fli>\u003Cli data-block-key=\"8lsr7\">\u003Cb>Session handling\u003C\u002Fb>\u003C\u002Fli>\u003Cli data-block-key=\"46oje\">\u003Cb>Authentication &amp; security\u003C\u002Fb>\u003C\u002Fli>\u003Cli data-block-key=\"8vif8\">\u003Cb>Internationalization\u003C\u002Fb>\u003C\u002Fli>\u003Cli data-block-key=\"cno9g\">\u003Cb>Admin panel &amp; API access\u003C\u002Fb>\u003C\u002Fli>\u003C\u002Ful>\u003Cp data-block-key=\"53vl0\">One of Django’s biggest strengths is its \u003Cb>comprehensive documentation\u003C\u002Fb>, which makes it easy to learn and explore advanced topics as you progress.\u003C\u002Fp>\u003Cp data-block-key=\"9ierf\">Now, let's get started with creating a Django project.\u003C\u002Fp>","580741b5-5d05-47bb-b73e-35192b9e13c6",{"type":28,"value":38,"id":42},{"title":39,"content":40,"code":41,"image":11},"Create and activate virtual environment","\u003Cp data-block-key=\"0oecf\">First, what is a virtual environment?\u003C\u002Fp>\u003Cp data-block-key=\"6qg86\">A virtual environment is an isolated space where you can install and manage packages separately for different projects. This allows you to work on multiple projects without conflicts between their dependencies.\u003C\u002Fp>\u003Cp data-block-key=\"9cko1\">With a virtual environment, you are able to create a virtual and isolated environment for testing and production of different projects.\u003C\u002Fp>\u003Cp data-block-key=\"arp49\">For example:\u003C\u002Fp>\u003Cul>\u003Cli data-block-key=\"brcsb\">\u003Cb>Project A\u003C\u002Fb> (created years ago) uses \u003Cb>Django 1.5\u003C\u002Fb>.\u003C\u002Fli>\u003Cli data-block-key=\"r42j\">\u003Cb>Project B\u003C\u002Fb> (a new project) uses a newer \u003Cb>Django version\u003C\u002Fb>.\u003C\u002Fli>\u003C\u002Ful>\u003Cp data-block-key=\"9rs95\">Without a virtual environment, managing these two projects on the same system could cause version conflicts. By using virtual environments, any changes or package installations in one project will not affect other projects.\u003C\u002Fp>\u003Cp data-block-key=\"cu34n\">For example, if you install and use a specific Python package and libraries in one environment, it doesn’t affect other environments.\u003C\u002Fp>\u003Cp data-block-key=\"ehao\">This helps keep your development environment clean and organized.\u003C\u002Fp>\u003Cp data-block-key=\"6e35n\">To get started, open a terminal and install the virtualenv package using this command:\u003C\u002Fp>","sudo apt-get install virtualenv","75abf9ff-4af7-4f85-95b5-795b584d7501",{"type":28,"value":44,"id":47},{"title":11,"content":45,"code":46,"image":11},"\u003Cp data-block-key=\"z6x8c\">Next, create a virtual environment using \u003Cb>virtualenv\u003C\u002Fb>. In this example, we will name it \u003Cb>\"myenv\"\u003C\u002Fb> and set Python 3 as the default version:\u003C\u002Fp>","virtualenv myenv -p python3","9a4d1455-f85c-40e7-9fdb-b2dad75bb8d4",{"type":28,"value":49,"id":52},{"title":11,"content":50,"code":51,"image":11},"\u003Cp data-block-key=\"z6x8c\">Once created, activate the virtual environment with:\u003C\u002Fp>","source .\u002Fmyenv\u002Fbin\u002Factivate","560d013e-f368-4c8f-be66-a12e2b6dd561",{"type":28,"value":54,"id":56},{"title":11,"content":55,"code":11,"image":11},"\u003Cp data-block-key=\"z6x8c\">After activation, you’ll notice that your terminal prompt changes to \u003Cb>(myenv)\u003C\u002Fb>, indicating that the virtual environment is now active.\u003C\u002Fp>","878932ce-491a-4074-b7c7-e65526d64497",{"type":28,"value":58,"id":62},{"title":59,"content":60,"code":61,"image":11},"Installing Django and Creating a Project","\u003Cp data-block-key=\"2510v\">First, install Django using \u003Cb>pip\u003C\u002Fb>:\u003C\u002Fp>","pip install django","dc1e7683-d47f-4b27-a7a9-76fff23a3010",{"type":28,"value":64,"id":67},{"title":11,"content":65,"code":66,"image":11},"\u003Cp data-block-key=\"2510v\">This will install Django along with its required dependencies.\u003C\u002Fp>\u003Ch3 data-block-key=\"5um92\">\u003Cb>Creating Your First Django Project\u003C\u002Fb>\u003C\u002Fh3>\u003Cp data-block-key=\"3jseh\">Now, let’s create a new Django project called \u003Cb>musiclibrary\u003C\u002Fb>:\u003C\u002Fp>","django-admin startproject musiclibrary","7d0e1469-7bb2-4412-9607-f37822a63949",{"type":28,"value":69,"id":72},{"title":11,"content":70,"code":71,"image":11},"\u003Cp data-block-key=\"2510v\">This command creates a directory named \u003Cb>musiclibrary\u003C\u002Fb> containing the necessary project files. Navigate into the project folder:\u003C\u002Fp>","cd musiclibrary","b484ad67-8619-44c9-aff3-320dd67adcda",{"type":28,"value":74,"id":77},{"title":11,"content":75,"code":76,"image":11},"\u003Ch3 data-block-key=\"0pi4u\">Understanding Django Project Structure\u003C\u002Fh3>\u003Cp data-block-key=\"79vke\">On Django, any website that is to be developed is called a \u003Cb>project\u003C\u002Fb>. And a project is a collection of apps. A \u003Cb>Django app\u003C\u002Fb> is a self-contained module that handles a specific functionality, such as authentication, payments, or blog posts.\u003C\u002Fp>\u003Cp data-block-key=\"7btb6\">For example, if you build an e-commerce website, the \u003Cb>project\u003C\u002Fb> is the entire website, while different functionalities (e.g., payments, products, accounts) are separate \u003Cb>apps\u003C\u002Fb>. Keeping apps modular makes your codebase more organized and reusable.\u003C\u002Fp>\u003Cp data-block-key=\"emsh5\">\u003C\u002Fp>\u003Cp data-block-key=\"5nmql\">Django follows the \u003Cb>Model-View-Template (MVT)\u003C\u002Fb> architectural pattern, which consists of:\u003C\u002Fp>\u003Cul>\u003Cli data-block-key=\"c8dqs\">\u003Cb>Model:\u003C\u002Fb> Defines the structure and behavior of the database (e.g., tables, fields, relationships).\u003C\u002Fli>\u003Cli data-block-key=\"fasbc\">\u003Cb>View:\u003C\u002Fb> Handles the logic, processes user requests, and interacts with the model.\u003C\u002Fli>\u003Cli data-block-key=\"504ki\">\u003Cb>Template:\u003C\u002Fb> Defines the front-end structure (HTML files) used to render data dynamically.\u003C\u002Fli>\u003C\u002Ful>\u003Cp data-block-key=\"csro2\">\u003C\u002Fp>\u003Cimg alt=\"Django-MVT-Model-Views-Template-Doprax\" class=\"richtext-image left\" height=\"281\" src=\"\u002Fd\u002Fmedia\u002Fimages\u002FDjango-MVT-Model-Views-Template-Doprax.width-500.png\" width=\"500\">\u003Cp data-block-key=\"cqbpc\">\u003C\u002Fp>\u003Cp data-block-key=\"6bsvr\">This separation makes Django applications well-structured, scalable, and easy to maintain.\u003C\u002Fp>\u003Cp data-block-key=\"2g4ll\">When you create a new Django project, it generates a set of essential files and directories.\u003C\u002Fp>","musiclibrary\r\n--manage.py\r\n--musiclibrary\r\n----__init__.py\r\n----manage.py\r\n----settings.py\r\n----urls.py\r\n----wsgi.py","2ef896de-8124-44d0-973b-85a9aa7b18f7",{"type":28,"value":79,"id":81},{"title":11,"content":80,"code":11,"image":11},"\u003Cp data-block-key=\"e19ga\">Let's explore each of them:\u003C\u002Fp>\u003Col>\u003Cli data-block-key=\"4piht\">\u003Cb>manage.py:\u003C\u002Fb> A command-line utility that helps manage your Django project. You use it to run the development server, apply migrations, create apps, and more. You should never edit or add anything to this file.\u003C\u002Fli>\u003Cli data-block-key=\"b6i72\">\u003Cb>musiclibrary\u002F (Inner Directory):\u003C\u002Fb> This is the core of your Django project. It shares the same name as your project but serves a different role. It contains:\u003Cul>\u003Cli data-block-key=\"7nf9a\">\u003Cb>__init__.py:\u003C\u002Fb> A file required by Python. This file tells python to view this directory (musiclibrary folder) as a Python package. This file is an empty file, and usually you will not add anything inside it. \u003C\u002Fli>\u003Cli data-block-key=\"b4ask\">\u003Cb>settings.py:\u003C\u002Fb> Contains all configurations for your project, including database settings, installed apps, middleware, and static files.\u003C\u002Fli>\u003Cli data-block-key=\"ci8h6\">\u003Cb>urls.py:\u003C\u002Fb> Defines URL routing for your project. It is like a table of content for the Django website. It will direct the user requests to an appropriate view function to handle them. \u003C\u002Fli>\u003Cli data-block-key=\"epnd9\">\u003Cb>wsgi.py\u003C\u002Fb>: The entry point for WSGI (Web Server Gateway Interface) applications, used for deploying Django projects and to help your Django application communicate with the webserver. We'll discuss more on this in future sections dedicated to the deployment of Django projects. \u003C\u002Fli>\u003C\u002Ful>\u003C\u002Fli>\u003C\u002Fol>\u003Cp data-block-key=\"32j94\">This structure forms the foundation of every Django project and provides the necessary components to build and scale web applications effectively.\u003C\u002Fp>","794906fa-96d1-4f71-b10a-b82a6eb7f251",{"type":28,"value":83,"id":86},{"title":84,"content":85,"code":11,"image":11},"Create a view.py file","\u003Cp data-block-key=\"worar\">In Django’s Model-View-Template (MVT) architecture, the \u003Cb>View\u003C\u002Fb> serves as the link between the \u003Cb>Model\u003C\u002Fb> (database) and the \u003Cb>Template\u003C\u002Fb> (HTML UI). It contains the logic that determines how data is processed and presented to users.\u003C\u002Fp>\u003Cp data-block-key=\"2l5ji\">\u003Cb>What is a Django view?\u003C\u002Fb>\u003C\u002Fp>\u003Cp data-block-key=\"et9lt\">A Django view is a Python function (or class) that receives a web request and returns an HTTP response. The response could be:\u003C\u002Fp>\u003Cul>\u003Cli data-block-key=\"9dgb9\">Raw data (e.g., JSON, XML)\u003C\u002Fli>\u003Cli data-block-key=\"4pcm7\">An HTML template with dynamic content\u003C\u002Fli>\u003Cli data-block-key=\"743mv\">A redirect to another URL\u003C\u002Fli>\u003C\u002Ful>\u003Cp data-block-key=\"9va7b\">By default, Django does not generate a views.py file when you create a new project. You must manually create it inside your main app’s directory.\u003C\u002Fp>\u003Cp data-block-key=\"5i9tl\">Navigate to your \u003Cb>inner musiclibrary folder\u003C\u002Fb> (the main app) and create a file named views.py. Inside this file, you will create functions that handle user requests. Functions in views.py take a web request and return an HTTP response. They may require information from the models to handle the request.\u003C\u002Fp>\u003Ch4 data-block-key=\"7ugbc\">\u003Cb>How views work in Django\u003C\u002Fb>\u003C\u002Fh4>\u003Col>\u003Cli data-block-key=\"70had\">A user visits a webpage.\u003C\u002Fli>\u003Cli data-block-key=\"4sq58\">The browser sends a request to Django’s backend.\u003C\u002Fli>\u003Cli data-block-key=\"7f9os\">Django routes the request to the correct view function based on URL patterns.\u003C\u002Fli>\u003Cli data-block-key=\"bvtmf\">The view processes the request, fetches or manipulates data, and returns an HTTP response.\u003C\u002Fli>\u003Cli data-block-key=\"2kk89\">If needed, the response is passed to a template, which renders HTML before sending it back to the user.\u003C\u002Fli>\u003C\u002Fol>\u003Cp data-block-key=\"9dqng\">Views are the core logic layer of Django applications that handles data retrieval, processing, and generating of responses.\u003C\u002Fp>","813f3aaf-dd33-407a-9d90-f0122a6adcaa",{"type":28,"value":88,"id":92},{"title":89,"content":90,"code":91,"image":11},"Write our first Hello World!","\u003Cp data-block-key=\"tw4nn\">To understand how views work in Django, let's start by creating a simple \u003Cb>\"Hello, World!\"\u003C\u002Fb> view.\u003C\u002Fp>\u003Cp data-block-key=\"1gss\">Every request made to a Django application is handled by a specific view that determines the response.\u003C\u002Fp>\u003Cp data-block-key=\"b9m3p\">For example, when a user clicks on the \u003Cb>\"About\"\u003C\u002Fb> link in \u003Cb>doprax.com\u003C\u002Fb>, the browser sends a request to the server. Django's URL dispatcher directs this request to the correct view function, which then returns an appropriate response. This response could be an HTML webpage, a redirect to another URL, or even a 404 error page.\u003C\u002Fp>\u003Cp data-block-key=\"ar41\">To define a function, we need to use the \u003Cb>def\u003C\u002Fb> keyword. Def is a Python-registered keyword for defining functions.\u003C\u002Fp>\u003Cp data-block-key=\"bmkcl\">You don't have to put too much thought into choosing a name for your function, but it’s always a good idea to name it something meaningful to make your code more legible for other developers. \u003C\u002Fp>\u003Ch4 data-block-key=\"c95mu\">\u003Cb>Writing your first Django view (\"Hello, World!\")\u003C\u002Fb>\u003C\u002Fh4>\u003Cp data-block-key=\"egrp3\">Open the views.py file in your main app (musiclibrary), and add the following code:\u003C\u002Fp>","from django.shortcuts import render\r\nfrom django.http import HttpResponse\r\n\r\n      \r\ndef hello_world(request):\r\n   return HttpResponse(\"Hello World!\")","e362e38d-0661-4c02-b398-bced2464ad39",{"type":28,"value":94,"id":98},{"title":95,"content":96,"code":97,"image":11},"URLs in Django","\u003Cp data-block-key=\"rxpor\">URL stands for \u003Cb>Uniform Resource Locator. \u003C\u002Fb>As mentioned before, the URL is like the table of contents of your website. Every page on the Internet needs its own address. It means whenever you create a view, it needs to be mapped to a URL or address. Each view is called by a URL written in the \u003Ci>urls.py\u003C\u002Fi> file inside the main app folder. The default \u003Ci>urls.py\u003C\u002Fi> file inside the main app folder looks something like this.\u003C\u002Fp>","from django.contrib import admin\r\nfrom django.urls import path\r\n\r\nurlpatterns = [\r\n    path('admin\u002F', admin.site.urls),\r\n]","64fec68d-0b87-4576-91fa-aaf2f5153abc",{"type":28,"value":100,"id":103},{"title":11,"content":101,"code":102,"image":11},"\u003Cp data-block-key=\"nzgzj\">The important part of this snippet is the \u003Ci>URL pattern\u003C\u002Fi>, which is a Python list object. Each item in this list (only one in the above example) is a mapping between an address of a page and a view function that handles it. \u003C\u002Fp>\u003Cp data-block-key=\"cku81\">Let’s add our first URL to the URL patterns list. It should be something like this:\u003C\u002Fp>","from django.contrib import admin\r\nfrom django.urls import path\r\nfrom .import views\r\n\r\nurlpatterns = [\r\n    path('say-hello\u002F', views.hello_world),\r\n    path('admin\u002F', admin.site.urls),\r\n\r\n]","4680dd33-bf05-49f0-8edd-7502961a6aad",{"type":28,"value":105,"id":107},{"title":11,"content":106,"code":11,"image":11},"\u003Cp data-block-key=\"nzgzj\">Notice that we have imported views in the third line so that we could reference the hello_world function that we have in the view.py file. We are telling Django that when a request comes to the server and the requested URL is “\u002Fsay-hello\u002F” (for example, \u003Ca href=\"http:\u002F\u002Fwww.mydomain.com\u002Fsay-hello\u002F)\">http:\u002F\u002Fwww.mydomain.com\u002Fsay-hello\u002F),\u003C\u002Fa> you should call the hello_world function to handle it. \u003C\u002Fp>","3d7ab6fe-2612-44de-868a-2d1deae1896a",{"type":28,"value":109,"id":113},{"title":110,"content":111,"code":112,"image":11},"Run local development server","\u003Cp data-block-key=\"8vgi1\">Let's review what we've done so far:\u003C\u002Fp>\u003Cul>\u003Cli data-block-key=\"9e6dc\">Installed virtualenv, created, and activated a virtual environment.\u003C\u002Fli>\u003Cli data-block-key=\"2pie8\">Installed Django using pip.\u003C\u002Fli>\u003Cli data-block-key=\"3c0nr\">Created our first Django project called \u003Cb>musiclibrary\u003C\u002Fb>.\u003C\u002Fli>\u003Cli data-block-key=\"9otbt\">Defined our first view function (hello_world) and mapped it to a URL (\u002Fsay-hello\u002F).\u003C\u002Fli>\u003C\u002Ful>\u003Cp data-block-key=\"6qem4\">Now, it's time to run our project and view it in a browser.\u003C\u002Fp>\u003Cp data-block-key=\"fjenr\">Before a request reaches our Django project, it needs to be handled by a web server. Thankfully, Django comes with a built-in development server, which provides everything we need for local testing.\u003C\u002Fp>\u003Cp data-block-key=\"a58o3\">Remember the manage.py file in our project folder? Now it’s time to use it.\u003C\u002Fp>\u003Cp data-block-key=\"84h56\">Make sure that your virtual environment (myenv) is activated. Then make sure you are at the root of the project (cd ~\u002Fmusiclibrary\u002F).\u003C\u002Fp>\u003Cp data-block-key=\"dpq0j\">Now enter this command to start the Django local development server:\u003C\u002Fp>","python manage.py runserver","8af5539c-2718-4fe1-a91c-db2e505903e8",{"type":28,"value":115,"id":117},{"title":11,"content":116,"code":11,"image":11},"\u003Cp data-block-key=\"79844\">It will run the development server and by default, it will be reachable using your localhost on port 8000. Open a browser and type this URL or Just click this link (\u003Ca href=\"http:\u002F\u002F127.0.0.1:8000\u002Fsay-hello\u002F)\">http:\u002F\u002F127.0.0.1:8000\u002Fsay-hello\u002F)\u003C\u002Fa>\u003C\u002Fp>","3b3bb7ef-752a-44db-9f5e-272f5292337f",{"type":28,"value":119,"id":121},{"title":11,"content":120,"code":11,"image":11},"\u003Cp data-block-key=\"79844\">Congratulations. You just created your first Django-powered website!\u003C\u002Fp>\u003Cp data-block-key=\"12tsr\">This is the end of Part 1.\u003C\u002Fp>\u003Cp data-block-key=\"esp21\">During this 10-part tutorial, we will gradually add more functionality to our Django website and deploy it to the cloud. At the end of this Django tutorial for beginners, hopefully, you will move from being a beginner to being an intermediate Django user.\u003C\u002Fp>\u003Cp data-block-key=\"87pm4\">Click on the next tutorial to continue learning.\u003C\u002Fp>\u003Cp data-block-key=\"92upm\">If you wish to join our community of self-starters learning new skills and creating projects from scratch, join our \u003Ca href=\"https:\u002F\u002Fdiscord.gg\u002F7KeRZPNZ\">Discord channel\u003C\u002Fa>.\u003C\u002Fp>\u003Cp data-block-key=\"20rmq\">\u003Cb>Next:\u003C\u002Fb>\u003C\u002Fp>\u003Cp data-block-key=\"7bbcr\">\u003Ca>Django Tutorial for Beginners [Step by Step] – Part 2: Django Template\u003C\u002Fa>.\u003C\u002Fp>","ead0ba1d-b8d5-44ce-bf28-38d0d2059bc1","\u003Cp data-block-key=\"q47ft\">In this tutorial, you will learn how to create a Django project, set up a simple view, and return a basic response.\u003C\u002Fp>","all",1792480210554]