{"id":3379,"date":"2025-01-08T23:10:07","date_gmt":"2025-01-08T23:10:07","guid":{"rendered":"https:\/\/kmfinfotech.com\/blogs\/from-zero-to-django-building-your-first-web-application-in-python\/"},"modified":"2025-01-08T23:10:07","modified_gmt":"2025-01-08T23:10:07","slug":"from-zero-to-django-building-your-first-web-application-in-python","status":"publish","type":"post","link":"https:\/\/kmfinfotech.com\/blogs\/from-zero-to-django-building-your-first-web-application-in-python\/","title":{"rendered":"From Zero to Django: Building Your First Web Application in Python"},"content":{"rendered":"<p><br \/>\n<\/p>\n<p>Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. If you&#8217;re new to web development and eager to create dynamic web applications, Django is a fantastic option. This guide will take you from a beginner to building your first Django web application.<\/p>\n<p><\/p>\n<h2>Table of Contents<\/h2>\n<p><\/p>\n<ul><\/p>\n<li><a href=\"#introduction\">1. Introduction<\/a><\/li>\n<p><\/p>\n<li><a href=\"#setting-up\">2. Setting Up Your Development Environment<\/a><\/li>\n<p><\/p>\n<li><a href=\"#creating-django-project\">3. Creating Your First Django Project<\/a><\/li>\n<p><\/p>\n<li><a href=\"#understanding-django-structure\">4. Understanding the Django Project Structure<\/a><\/li>\n<p><\/p>\n<li><a href=\"#creating-app\">5. Creating a Django App<\/a><\/li>\n<p><\/p>\n<li><a href=\"#building-models\">6. Building Models and Database Migrations<\/a><\/li>\n<p><\/p>\n<li><a href=\"#creating-views\">7. Creating Views and Templates<\/a><\/li>\n<p><\/p>\n<li><a href=\"#urls-and-routing\">8. URLs and Routing<\/a><\/li>\n<p><\/p>\n<li><a href=\"#static-and-media-files\">9. Static and Media Files<\/a><\/li>\n<p><\/p>\n<li><a href=\"#testing\">10. Testing Your Application<\/a><\/li>\n<p><\/p>\n<li><a href=\"#deployment\">11. Deployment and Production<\/a><\/li>\n<p><\/p>\n<li><a href=\"#conclusion\">12. Conclusion<\/a><\/li>\n<p>\n<\/ul>\n<p><\/p>\n<h2 id=\"introduction\">1. Introduction<\/h2>\n<p><\/p>\n<p>Django was designed to help developers take applications from concept to completion as swiftly as possible. This is achieved with built-in features that take care of the common web development tasks, such as user authentication, an administrative interface, and robust database management. In this guide, we&#8217;ll cover the basics of Django and walk through building a simple web application.<\/p>\n<p><\/p>\n<h2 id=\"setting-up\">2. Setting Up Your Development Environment<\/h2>\n<p><\/p>\n<p>Before diving into Django, you need to set up your development environment. Below are the steps to get started:<\/p>\n<p><\/p>\n<h3>Install Python<\/h3>\n<p><\/p>\n<p>Django is a Python framework, so ensure you have Python installed on your machine. Download it from the <a href=\"https:\/\/www.python.org\/downloads\/\" target=\"_blank\" rel=\"noopener\">official website<\/a>.<\/p>\n<p><\/p>\n<h3>Install Django<\/h3>\n<p><\/p>\n<p>Once Python is installed, you can install Django using pip, Python&#8217;s package manager. Open your terminal and run:<\/p>\n<p><\/p>\n<pre><code>pip install django<\/code><\/pre>\n<p><\/p>\n<h3>Verify the Installation<\/h3>\n<p><\/p>\n<p>To confirm that Django is installed correctly, run the following command:<\/p>\n<p><\/p>\n<pre><code>django-admin --version<\/code><\/pre>\n<p><\/p>\n<p>This command should display the installed version of Django.<\/p>\n<p><\/p>\n<h2 id=\"creating-django-project\">3. Creating Your First Django Project<\/h2>\n<p><\/p>\n<p>A Django project is a collection of settings and configurations for a particular web application. Let&#8217;s create your first project:<\/p>\n<p><\/p>\n<h3>Create a Project<\/h3>\n<p><\/p>\n<p>Run the following command in your terminal, replacing <code>myproject<\/code> with your desired project name:<\/p>\n<p><\/p>\n<pre><code>django-admin startproject myproject<\/code><\/pre>\n<p><\/p>\n<h3>Navigate into Your Project Directory<\/h3>\n<p><\/p>\n<pre><code>cd myproject<\/code><\/pre>\n<p><\/p>\n<h3>Run the Development Server<\/h3>\n<p><\/p>\n<p>You can start your Django application and see it in action by running:<\/p>\n<p><\/p>\n<pre><code>python manage.py runserver<\/code><\/pre>\n<p><\/p>\n<p>Open your web browser and go to <a href=\"http:\/\/127.0.0.1:8000\/\">http:\/\/127.0.0.1:8000\/<\/a>. You should see the Django welcome page!<\/p>\n<p><\/p>\n<h2 id=\"understanding-django-structure\">4. Understanding the Django Project Structure<\/h2>\n<p><\/p>\n<p>Your Django project will have a standard structure:<\/p>\n<p><\/p>\n<pre><br \/>\nmyproject\/<br \/>\n    manage.py<br \/>\n    myproject\/<br \/>\n        __init__.py<br \/>\n        settings.py<br \/>\n        urls.py<br \/>\n        wsgi.py<br \/>\n<\/pre>\n<p><\/p>\n<ul><\/p>\n<li><strong>manage.py<\/strong> &#8211; A command-line utility that lets you interact with your project.<\/li>\n<p><\/p>\n<li><strong>settings.py<\/strong> &#8211; Contains all configurations for your project.<\/li>\n<p><\/p>\n<li><strong>urls.py<\/strong> &#8211; Defines the URL patterns for your project.<\/li>\n<p><\/p>\n<li><strong>wsgi.py<\/strong> &#8211; Contains the WSGI configuration for your project.<\/li>\n<p>\n<\/ul>\n<p><\/p>\n<h2 id=\"creating-app\">5. Creating a Django App<\/h2>\n<p><\/p>\n<p>A Django project can contain multiple apps, which are designed to be modular. This allows for easier development and management. Let\u2019s create a simple app:<\/p>\n<p><\/p>\n<h3>Create a New App<\/h3>\n<p><\/p>\n<pre><code>python manage.py startapp myapp<\/code><\/pre>\n<p><\/p>\n<p>Your project now contains a new directory <code>myapp<\/code> with several files, including models, views, and tests.<\/p>\n<p><\/p>\n<h2 id=\"building-models\">6. Building Models and Database Migrations<\/h2>\n<p><\/p>\n<p>Models in Django are Python classes that define the fields and behaviors of the data you\u2019re storing. Django provides a powerful ORM (Object-Relational Mapping) system to interact with the database.<\/p>\n<p><\/p>\n<h3>Create Your First Model<\/h3>\n<p><\/p>\n<p>Edit <code>myapp\/models.py<\/code> to define a simple model:<\/p>\n<p><\/p>\n<pre><code>from django.db import models<br>class Item(models.Model):<br \/>\n    name = models.CharField(max_length=200)<br \/>\n    description = models.TextField()<br \/>\n    created_at = models.DateTimeField(auto_now_add=True)<br>def __str__(self):<br \/>\n        return self.name<\/code><\/pre>\n<p><\/p>\n<h3>Make Migrations<\/h3>\n<p><\/p>\n<p>To create the database schema for your model, run:<\/p>\n<p><\/p>\n<pre><code>python manage.py makemigrations<\/code><\/pre>\n<p><\/p>\n<h3>Apply Migrations<\/h3>\n<p><\/p>\n<p>Now, apply the migrations:<\/p>\n<p><\/p>\n<pre><code>python manage.py migrate<\/code><\/pre>\n<p><\/p>\n<h2 id=\"creating-views\">7. Creating Views and Templates<\/h2>\n<p><\/p>\n<p>With your models set up, the next step is to create views and templates to display data.<\/p>\n<p><\/p>\n<h3>Create a View<\/h3>\n<p><\/p>\n<p>Edit <code>myapp\/views.py<\/code> to add a view that retrieves all items:<\/p>\n<p><\/p>\n<pre><code>from django.shortcuts import render<br \/>\nfrom .models import Item<br>def item_list(request):<br \/>\n    items = Item.objects.all()<br \/>\n    return render(request, 'myapp\/item_list.html', {'items': items})<\/code><\/pre>\n<p><\/p>\n<h3>Create a Template<\/h3>\n<p><\/p>\n<p>Create a directory called <code>templates<\/code> inside your <code>myapp<\/code> folder, and then create another directory called <code>myapp<\/code> inside it. Now create the file <code>item_list.html<\/code>:<\/p>\n<p><\/p>\n<pre><code>&lt;!DOCTYPE html&gt;<br \/>\n&lt;html lang=\"en\"&gt;<br \/>\n&lt;head&gt;<br \/>\n    &lt;meta charset=\"UTF-8\"&gt;<br \/>\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;<br \/>\n    &lt;title&gt;Item List&lt;\/title&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n    &lt;h1&gt;Items&lt;\/h1&gt;<br \/>\n    &lt;ul&gt;<br \/>\n        {% for item in items %}<br \/>\n            &lt;li&gt;{{ item.name }}: {{ item.description }}&lt;\/li&gt;<br \/>\n        {% endfor %}<br \/>\n    &lt;\/ul&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/code><\/pre>\n<p><\/p>\n<h2 id=\"urls-and-routing\">8. URLs and Routing<\/h2>\n<p><\/p>\n<p>To access your view, you need to create a URL mapping. Open <code>myproject\/urls.py<\/code> and include your app\u2019s URLs:<\/p>\n<p><\/p>\n<pre><code>from django.contrib import admin<br \/>\nfrom django.urls import path<br \/>\nfrom myapp.views import item_list<br>urlpatterns = [<br \/>\n    path('admin\/', admin.site.urls),<br \/>\n    path('', item_list, name='item_list'),<br \/>\n]<\/code><\/pre>\n<p><\/p>\n<h2 id=\"static-and-media-files\">9. Static and Media Files<\/h2>\n<p><\/p>\n<p>Static files are assets such as stylesheets, JavaScript, and images. In Django, you can handle static files using the <code>STATIC_URL<\/code> setting in <code>settings.py<\/code>.<\/p>\n<p><\/p>\n<h3>Set Up Static Files<\/h3>\n<p><\/p>\n<p>Add a directory called <code>static<\/code> in your <code>myapp<\/code> folder for your CSS and JavaScript files. Ensure you modify your <code>settings.py<\/code> like so:<\/p>\n<p><\/p>\n<pre><code>STATIC_URL = '\/static\/'<\/code><\/pre>\n<p><\/p>\n<h2 id=\"testing\">10. Testing Your Application<\/h2>\n<p><\/p>\n<p>Testing is crucial in web development. Django includes a testing framework that allows you to create and run tests easily.<\/p>\n<p><\/p>\n<h3>Create a Simple Test<\/h3>\n<p><\/p>\n<p>Create a test in <code>myapp\/tests.py<\/code>:<\/p>\n<p><\/p>\n<pre><code>from django.test import TestCase<br \/>\nfrom .models import Item<br>class ItemModelTest(TestCase):<br \/>\n    def setUp(self):<br \/>\n        Item.objects.create(name=\"Test Item\", description=\"A description\")<br>def test_item_content(self):<br \/>\n        item = Item.objects.get(id=1)<br \/>\n        expected_object_name = f'{item.name}'<br \/>\n        self.assertEqual(expected_object_name, 'Test Item')<\/code><\/pre>\n<p><\/p>\n<h3>Run Your Tests<\/h3>\n<p><\/p>\n<p>Execute the following command to run your tests:<\/p>\n<p><\/p>\n<pre><code>python manage.py test myapp<\/code><\/pre>\n<p><\/p>\n<h2 id=\"deployment\">11. Deployment and Production<\/h2>\n<p><\/p>\n<p>Deploying a Django application to production involves more steps than simply running the development server. Ensure you follow best practices, such as:<\/p>\n<p><\/p>\n<ul><\/p>\n<li>Use a production-ready web server (like Gunicorn or uWSGI).<\/li>\n<p><\/p>\n<li>Configure your database securely.<\/li>\n<p><\/p>\n<li>Set <code>DEBUG = False<\/code> in your settings.<\/li>\n<p><\/p>\n<li>Utilize environment variables for sensitive information.<\/li>\n<p>\n<\/ul>\n<p><\/p>\n<p>For deployment, platforms like Heroku or DigitalOcean offer straightforward routes to get your Django application online.<\/p>\n<p><\/p>\n<h2 id=\"conclusion\">12. Conclusion<\/h2>\n<p><\/p>\n<p>Congratulations! You&#8217;ve built your first web application using Django. You started with the basics\u2014understanding the framework, setting up the environment, creating projects and apps, and finally deploying your application. While this tutorial provided a foundation, Django\u2019s extensive features offer endless possibilities for expanding your application.<\/p>\n<p><\/p>\n<p>As you continue to develop your skills, explore more complex topics such as user authentication, form handling, REST APIs, and integrating third-party packages. Django has a vibrant community and extensive documentation to help guide you through your learning journey.<\/p>\n<p><\/p>\n<p>Happy coding, and welcome to the great world of web development with Django!<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. If you&#8217;re new to web development and eager to create dynamic web applications, Django is a fantastic option. This guide will take you from a beginner to building your first Django web application. Table of Contents 1. Introduction 2. Setting [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":3380,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[58],"tags":[110,85,290,637,74],"class_list":["post-3379","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-application","tag-building","tag-django","tag-python","tag-web"],"_links":{"self":[{"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/posts\/3379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/comments?post=3379"}],"version-history":[{"count":0,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/posts\/3379\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/media\/3380"}],"wp:attachment":[{"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/media?parent=3379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/categories?post=3379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/tags?post=3379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}