{"id":2750,"date":"2025-01-06T02:13:18","date_gmt":"2025-01-06T02:13:18","guid":{"rendered":"https:\/\/kmfinfotech.com\/blogs\/building-your-first-web-app-with-django-a-step-by-step-guide\/"},"modified":"2025-01-06T02:13:18","modified_gmt":"2025-01-06T02:13:18","slug":"building-your-first-web-app-with-django-a-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/kmfinfotech.com\/blogs\/building-your-first-web-app-with-django-a-step-by-step-guide\/","title":{"rendered":"Building Your First Web App with Django: A Step-by-Step Guide"},"content":{"rendered":"<p><br \/>\n<\/p>\n<div class=\"container\"><\/p>\n<h2>Introduction<\/h2>\n<p><\/p>\n<p>Django is an open-source web framework that allows developers to build dynamic and scalable web applications quickly and efficiently. With its &#8220;batteries-included&#8221; philosophy, Django provides a robust set of tools and libraries to streamline many aspects of web development.<\/p>\n<p><\/p>\n<p>This guide will walk you through the process of building your first web application using Django. By the end of this tutorial, you will have a solid understanding of some core Django concepts and how to deploy your application.<\/p>\n<p><\/p>\n<h2>Prerequisites<\/h2>\n<p><\/p>\n<p>Before we start, make sure you have the following prerequisites:<\/p>\n<p><\/p>\n<ul><\/p>\n<li>Basic understanding of Python programming<\/li>\n<p><\/p>\n<li>Python installed on your computer (version 3.6 or later)<\/li>\n<p><\/p>\n<li>Understanding of HTML\/CSS basics<\/li>\n<p><\/p>\n<li>A code editor (like VSCode, PyCharm, etc.)<\/li>\n<p>\n        <\/ul>\n<p><\/p>\n<h2>Step 1: Setting Up Your Development Environment<\/h2>\n<p><\/p>\n<p>The first step is to set up your development environment. For this, we will use <code>venv<\/code>, which is Python&#8217;s built-in module for creating isolated environments.<\/p>\n<p><\/p>\n<h3>Creating a Virtual Environment<\/h3>\n<p><\/p>\n<pre><code>python -m venv myprojectenv<\/code><\/pre>\n<p><\/p>\n<p>Activate your virtual environment:<\/p>\n<p><\/p>\n<h4>On Windows:<\/h4>\n<p><\/p>\n<pre><code>myprojectenv\\Scripts\\activate<\/code><\/pre>\n<p><\/p>\n<h4>On macOS\/Linux:<\/h4>\n<p><\/p>\n<pre><code>source myprojectenv\/bin\/activate<\/code><\/pre>\n<p><\/p>\n<h3>Installing Django<\/h3>\n<p><\/p>\n<p>Now that your virtual environment is activated, install Django using pip:<\/p>\n<p><\/p>\n<pre><code>pip install django<\/code><\/pre>\n<p><\/p>\n<h2>Step 2: Creating a New Django Project<\/h2>\n<p><\/p>\n<p>With Django installed, it&#8217;s time to create your first project.<\/p>\n<p><\/p>\n<pre><code>django-admin startproject myproject<\/code><\/pre>\n<p><\/p>\n<p>This command creates a new directory called <code>myproject<\/code> with the following structure:<\/p>\n<p><\/p>\n<pre><code><br \/>\nmyproject\/<br \/>\n    manage.py<br \/>\n    myproject\/<br \/>\n        __init__.py<br \/>\n        settings.py<br \/>\n        urls.py<br \/>\n        asgi.py<br \/>\n        wsgi.py<br \/>\n        <\/code><\/pre>\n<p><\/p>\n<h2>Step 3: Running the Development Server<\/h2>\n<p><\/p>\n<p>Change into the project directory and run the development server:<\/p>\n<p><\/p>\n<pre><code>cd myproject<\/code><\/pre>\n<p><\/p>\n<pre><code>python manage.py runserver<\/code><\/pre>\n<p><\/p>\n<p>Open your browser and go to <code>http:\/\/127.0.0.1:8000<\/code> to see your new Django project in action!<\/p>\n<p><\/p>\n<h2>Step 4: Creating a Django App<\/h2>\n<p><\/p>\n<p>Django projects can contain multiple applications. Let&#8217;s create a new app called <code>blog<\/code>:<\/p>\n<p><\/p>\n<pre><code>python manage.py startapp blog<\/code><\/pre>\n<p><\/p>\n<p>This will create a new directory structure like this:<\/p>\n<p><\/p>\n<pre><code><br \/>\nblog\/<br \/>\n    migrations\/<br \/>\n    __init__.py<br \/>\n    admin.py<br \/>\n    apps.py<br \/>\n    models.py<br \/>\n    tests.py<br \/>\n    views.py<br \/>\n<\/code><\/pre>\n<p><\/p>\n<h2>Step 5: Defining Models<\/h2>\n<p><\/p>\n<p>Now, let&#8217;s define a simple model for our blog posts. Open <code>models.py<\/code> in the <code>blog<\/code> directory and add the following code:<\/p>\n<p><\/p>\n<pre><code>from django.db import models<br>class Post(models.Model):<br \/>\n    title = models.CharField(max_length=100)<br \/>\n    content = models.TextField()<br \/>\n    created_at = models.DateTimeField(auto_now_add=True)<br>def __str__(self):<br \/>\n        return self.title<br \/>\n<\/code><\/pre>\n<p><\/p>\n<h2>Step 6: Registering the Model with Admin<\/h2>\n<p><\/p>\n<p>To manage your blog posts via the Django admin interface, you will need to register your model. Open <code>admin.py<\/code> in the <code>blog<\/code> directory and add:<\/p>\n<p><\/p>\n<pre><code>from django.contrib import admin<br \/>\nfrom .models import Post<br>admin.site.register(Post)<br \/>\n<\/code><\/pre>\n<p><\/p>\n<h2>Step 7: Configuring the App<\/h2>\n<p><\/p>\n<p>Next, you need to let your project know about the new app. Open <code>settings.py<\/code> in the <code>myproject<\/code> directory and add <code>'blog',<\/code> to the <code>INSTALLED_APPS<\/code> list:<\/p>\n<p><\/p>\n<pre><code>INSTALLED_APPS = [<br \/>\n    ...<br \/>\n    'blog',<br \/>\n]<br \/>\n<\/code><\/pre>\n<p><\/p>\n<h2>Step 8: Running Migrations<\/h2>\n<p><\/p>\n<p>We need to create the database tables for our new model. Run the following commands:<\/p>\n<p><\/p>\n<pre><code>python manage.py makemigrations<\/code><\/pre>\n<p><\/p>\n<pre><code>python manage.py migrate<\/code><\/pre>\n<p><\/p>\n<h2>Step 9: Creating a Superuser<\/h2>\n<p><\/p>\n<p>To access the Django admin site, you need to create a superuser:<\/p>\n<p><\/p>\n<pre><code>python manage.py createsuperuser<\/code><\/pre>\n<p><\/p>\n<p>Follow the prompts to set a username, email, and password.<\/p>\n<p><\/p>\n<h2>Step 10: Running the Server Again<\/h2>\n<p><\/p>\n<p>Now that you&#8217;ve created your superuser, run the development server again:<\/p>\n<p><\/p>\n<pre><code>python manage.py runserver<\/code><\/pre>\n<p><\/p>\n<p>Visit <code>http:\/\/127.0.0.1:8000\/admin<\/code> and log in with the credentials you just created. You should see your <code>Post<\/code> model available for you to manage!<\/p>\n<p><\/p>\n<h2>Step 11: Creating Views and Templates<\/h2>\n<p><\/p>\n<p>Next, let&#8217;s create views and templates to display our blog posts on the front end.<\/p>\n<p><\/p>\n<h3>Creating a View<\/h3>\n<p><\/p>\n<p>Open <code>views.py<\/code> in the <code>blog<\/code> directory and add the following code:<\/p>\n<p><\/p>\n<pre><code>from django.shortcuts import render<br \/>\nfrom .models import Post<br>def post_list(request):<br \/>\n    posts = Post.objects.all()<br \/>\n    return render(request, 'blog\/post_list.html', {'posts': posts})<br \/>\n<\/code><\/pre>\n<p><\/p>\n<h3>Creating a Template<\/h3>\n<p><\/p>\n<p>Create a directory called <code>templates<\/code> inside the <code>blog<\/code> directory, and then create another directory named <code>blog<\/code> inside that. Finally, create a file called <code>post_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;title&gt;Blog Posts&lt;\/title&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n    &lt;h1&gt;Blog Posts&lt;\/h1&gt;<br \/>\n    &lt;ul&gt;<br \/>\n        {% for post in posts %}<br \/>\n            &lt;li&gt;&lt;a href=\"#\"&gt;{{ post.title }}&lt;\/a&gt; - {{ post.created_at }}&lt;\/li&gt;<br \/>\n        {% endfor %}<br \/>\n    &lt;\/ul&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\n<\/code><\/pre>\n<p><\/p>\n<h2>Step 12: Updating URLs<\/h2>\n<p><\/p>\n<p>Next, map your view to a URL. Open <code>urls.py<\/code> in the <code>myproject<\/code> directory and set up URL routing:<\/p>\n<p><\/p>\n<pre><code>from django.contrib import admin<br \/>\nfrom django.urls import path<br \/>\nfrom blog.views import post_list<br>urlpatterns = [<br \/>\n    path('admin\/', admin.site.urls),<br \/>\n    path('', post_list, name='post_list'),<br \/>\n]<br \/>\n<\/code><\/pre>\n<p><\/p>\n<h2>Step 13: Test Your Web App<\/h2>\n<p><\/p>\n<p>Now that everything is set up, visit <code>http:\/\/127.0.0.1:8000<\/code> to see the list of blog posts!<\/p>\n<p><\/p>\n<h2>Step 14: Deploying Your Application<\/h2>\n<p><\/p>\n<p>Finally, once you are happy with your application, you may want to deploy it. There are several options for deploying Django applications:<\/p>\n<p><\/p>\n<ul><\/p>\n<li>Using Platform-as-a-Service (PaaS) providers like Heroku<\/li>\n<p><\/p>\n<li>Using a cloud service like AWS or Google Cloud<\/li>\n<p><\/p>\n<li>Using shared hosting that supports Django<\/li>\n<p>\n        <\/ul>\n<p><\/p>\n<p>For simplicity, we recommend starting with Heroku:<\/p>\n<p><\/p>\n<ol><\/p>\n<li>Install the Heroku CLI.<\/li>\n<p><\/p>\n<li>Create a new Heroku app using <code>heroku create<\/code>.<\/li>\n<p><\/p>\n<li>Check your requirements with <code>pip freeze > requirements.txt<\/code>.<\/li>\n<p><\/p>\n<li>Run migrations on Heroku by using <code>heroku run python manage.py migrate<\/code>.<\/li>\n<p><\/p>\n<li>Push your code to Heroku using <code>git push heroku master<\/code>.<\/li>\n<p>\n        <\/ol>\n<p><\/p>\n<h2>Conclusion<\/h2>\n<p><\/p>\n<p>Congratulations! You\u2019ve built your first web application using Django. You learned how to set up your environment, create models, views, templates, and even deploy your app. Django&#8217;s modular nature and built-in features make it a powerful choice for developing web applications.<\/p>\n<p><\/p>\n<p>As you continue to explore Django, consider diving deeper into features such as user authentication, REST APIs, and additional third-party libraries that can enhance your projects. With practice, you can leverage Django to create complex and dynamic web applications that cater to various user needs.<\/p>\n<p><\/p>\n<p>Keep building, keep learning, and enjoy the journey of web development!<\/p>\n<p>\n    <\/div>\n<p><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Introduction Django is an open-source web framework that allows developers to build dynamic and scalable web applications quickly and efficiently. With its &#8220;batteries-included&#8221; philosophy, Django provides a robust set of tools and libraries to streamline many aspects of web development. This guide will walk you through the process of building your first web application using [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2751,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[132],"tags":[75,85,290,88,175,74],"class_list":["post-2750","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mobile-app","tag-app","tag-building","tag-django","tag-guide","tag-stepbystep","tag-web"],"_links":{"self":[{"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/posts\/2750","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=2750"}],"version-history":[{"count":0,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/posts\/2750\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/media\/2751"}],"wp:attachment":[{"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/media?parent=2750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/categories?post=2750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/tags?post=2750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}