{"id":20889,"date":"2025-12-30T21:37:35","date_gmt":"2025-12-30T21:37:35","guid":{"rendered":"https:\/\/kmfinfotech.com\/blogs\/getting-started-with-django-building-your-first-web-app\/"},"modified":"2025-12-30T21:37:35","modified_gmt":"2025-12-30T21:37:35","slug":"getting-started-with-django-building-your-first-web-app","status":"publish","type":"post","link":"https:\/\/kmfinfotech.com\/blogs\/getting-started-with-django-building-your-first-web-app\/","title":{"rendered":"Getting Started with Django: Building Your First Web App"},"content":{"rendered":"<p><br \/>\n<\/p>\n<header><\/header>\n<p><\/p>\n<section id=\"introduction\"><\/p>\n<h2>Introduction<\/h2>\n<p><\/p>\n<p>Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Whether you are a seasoned developer or a beginner, Django\u2019s extensive features make it a go-to choice for building robust web applications.<\/p>\n<p>\n    <\/section>\n<p><\/p>\n<section id=\"installation\"><\/p>\n<h2>Installation and Setup<\/h2>\n<p><\/p>\n<p>Before diving into building your first web app with Django, you need to have Python installed on your system. Django is compatible with Python 3.6 and above.<\/p>\n<p><\/p>\n<ol><\/p>\n<li><strong>Install Python:<\/strong> You can download the latest version of Python from the <a href=\"https:\/\/www.python.org\/\" target=\"_blank\" rel=\"noopener\">official website<\/a>.<\/li>\n<p><\/p>\n<li><strong>Install Django:<\/strong> Once Python is installed, you can use pip to install Django. Run the following command in your terminal:\n<pre><code>pip install django<\/code><\/pre>\n<p>\n            <\/li>\n<p>\n        <\/ol>\n<p>\n    <\/section>\n<p><\/p>\n<section id=\"create-project\"><\/p>\n<h2>Creating Your First Django Project<\/h2>\n<p><\/p>\n<p>With Django installed, you can create a new project by running the following command:<\/p>\n<p><\/p>\n<pre><code>django-admin startproject myproject<\/code><\/pre>\n<p><\/p>\n<p>This command will generate a new directory called <code>myproject<\/code> with the basic structure of a Django project.<\/p>\n<p>\n    <\/section>\n<p><\/p>\n<section id=\"understanding-structure\"><\/p>\n<h2>Understanding the Django Project Structure<\/h2>\n<p><\/p>\n<p>Your Django project will include several files and directories:<\/p>\n<p><\/p>\n<ul><\/p>\n<li><strong>manage.py:<\/strong> A command-line utility to interact with your project.<\/li>\n<p><\/p>\n<li><strong>myproject\/:<\/strong> The directory containing your project settings and configuration files.<\/li>\n<p>\n        <\/ul>\n<p>\n    <\/section>\n<p><\/p>\n<section id=\"create-app\"><\/p>\n<h2>Creating Your First Django App<\/h2>\n<p><\/p>\n<p>In Django, a project can consist of multiple apps that serve a specific purpose. To create an app, run:<\/p>\n<pre><code>python manage.py startapp myapp<\/code><\/pre>\n<p>\n        <\/p>\n<p>\n    <\/section>\n<p><\/p>\n<section id=\"app-configuration\"><\/p>\n<h2>Configuring the App<\/h2>\n<p><\/p>\n<p>After creating an app, you need to add it to your project. Edit the <code>settings.py<\/code> file in your project folder and add <code>'myapp',<\/code> to the <code>INSTALLED_APPS<\/code> list.<\/p>\n<p>\n    <\/section>\n<p><\/p>\n<section id=\"creating-views\"><\/p>\n<h2>Creating Views<\/h2>\n<p><\/p>\n<p>A view function is a Python function that takes a web request and returns a web response. To create a view, open the <code>views.py<\/code> file in your app directory and write your first view:<\/p>\n<p><\/p>\n<pre><code><br \/>\nfrom django.http import HttpResponse<br>def home(request):<br \/>\n    return HttpResponse(\"Hello, world!\")<br \/>\n        <\/code><\/pre>\n<p>\n    <\/section>\n<p><\/p>\n<section id=\"url-mapping\"><\/p>\n<h2>Mapping URLs<\/h2>\n<p><\/p>\n<p>To make your view accessible, you need to map it to a URL. Create a new file named <code>urls.py<\/code> in your app directory and add:<\/p>\n<p><\/p>\n<pre><code><br \/>\nfrom django.urls import path<br \/>\nfrom . import views<br>urlpatterns = [<br \/>\n    path('', views.home, name='home'),<br \/>\n]<br \/>\n        <\/code><\/pre>\n<p>\n    <\/section>\n<p><\/p>\n<section id=\"connecting-urls\"><\/p>\n<h2>Connecting URLs<\/h2>\n<p><\/p>\n<p>Include the app&#8217;s URLs in the project&#8217;s main URL configuration file, <code>myproject\/urls.py<\/code>, by including your app\u2019s URL config:<\/p>\n<p><\/p>\n<pre><code><br \/>\nfrom django.contrib import admin<br \/>\nfrom django.urls import include, path<br>urlpatterns = [<br \/>\n    path('admin\/', admin.site.urls),<br \/>\n    path('', include('myapp.urls')),<br \/>\n]<br \/>\n        <\/code><\/pre>\n<p>\n    <\/section>\n<p><\/p>\n<section id=\"running-server\"><\/p>\n<h2>Running the Development Server<\/h2>\n<p><\/p>\n<p>With the configuration complete, you can run the development server to see your app in action. Use the command:<\/p>\n<pre><code>python manage.py runserver<\/code><\/pre>\n<p>\n        <\/p>\n<p><\/p>\n<p>Visit <a href=\"http:\/\/localhost:8000\/\" target=\"_blank\" rel=\"noopener\">http:\/\/localhost:8000\/<\/a> in your browser to view the homepage.<\/p>\n<p>\n    <\/section>\n<p><\/p>\n<section id=\"working-with-models\"><\/p>\n<h2>Working with Models<\/h2>\n<p><\/p>\n<p>Models define the structure of your data. Create a model in the <code>models.py<\/code> file of your app:<\/p>\n<p><\/p>\n<pre><code><br \/>\nfrom 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 \/>\n        <\/code><\/pre>\n<p>\n    <\/section>\n<p><\/p>\n<section id=\"migrating-database\"><\/p>\n<h2>Migrating the Database<\/h2>\n<p><\/p>\n<p>Once your models are defined, apply them to the database:<\/p>\n<pre><code><br \/>\npython manage.py makemigrations<br \/>\npython manage.py migrate<br \/>\n            <\/code><\/pre>\n<p>\n        <\/p>\n<p>\n    <\/section>\n<p><\/p>\n<section id=\"admin-interface\"><\/p>\n<h2>Using the Django Admin Interface<\/h2>\n<p><\/p>\n<p>Django provides an admin interface for managing your project\u2019s data. Register your models in <code>admin.py<\/code>:<\/p>\n<pre><code><br \/>\nfrom django.contrib import admin<br \/>\nfrom .models import Post<br>admin.site.register(Post)<br \/>\n            <\/code><\/pre>\n<p>\n        <\/p>\n<p><\/p>\n<p>Access the admin by visiting <a href=\"http:\/\/localhost:8000\/admin\/\" target=\"_blank\" rel=\"noopener\">http:\/\/localhost:8000\/admin\/<\/a> and logging in with a superuser account.<\/p>\n<p>\n    <\/section>\n<p><\/p>\n<section id=\"conclusion\"><\/p>\n<h2>Conclusion<\/h2>\n<p><\/p>\n<p>Building your first web application with Django is an enlightening experience that introduces you to the core components of web development. Django\u2019s powerful framework facilitates the creation of complex, data-driven sites quickly and effortlessly. This guide covers the essential steps to get you started; as you grow more familiar with Django, explore features like form handling, user authentication, and deploying your applications.<\/p>\n<p>\n    <\/section>\n\n","protected":false},"excerpt":{"rendered":"<p>Introduction Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Whether you are a seasoned developer or a beginner, Django\u2019s extensive features make it a go-to choice for building robust web applications. Installation and Setup Before diving into building your first web app with Django, you need to have [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":20890,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[58],"tags":[75,85,290,286,74],"class_list":["post-20889","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-app","tag-building","tag-django","tag-started","tag-web"],"_links":{"self":[{"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/posts\/20889","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=20889"}],"version-history":[{"count":0,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/posts\/20889\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/media\/20890"}],"wp:attachment":[{"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/media?parent=20889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/categories?post=20889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/tags?post=20889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}