{"id":14866,"date":"2025-05-23T00:58:17","date_gmt":"2025-05-23T00:58:17","guid":{"rendered":"https:\/\/kmfinfotech.com\/blogs\/getting-started-with-django-your-first-web-app\/"},"modified":"2025-05-23T00:58:17","modified_gmt":"2025-05-23T00:58:17","slug":"getting-started-with-django-your-first-web-app","status":"publish","type":"post","link":"https:\/\/kmfinfotech.com\/blogs\/getting-started-with-django-your-first-web-app\/","title":{"rendered":"Getting Started with Django: Your First Web App"},"content":{"rendered":"<p><br \/>\n<\/p>\n<p>Django is a powerful, high-level web framework for the Python programming language. It encourages rapid development and clean, pragmatic design. If you&#8217;re interested in learning how to build web applications using Django, this guide will help you create a simple web app from scratch. By the end of this article, you&#8217;ll have a basic understanding of how Django works and the knowledge to build more complex applications.<\/p>\n<p><\/p>\n<h2>Prerequisites<\/h2>\n<p><\/p>\n<p>Before diving into Django, make sure you have a basic understanding of Python. Additionally, you&#8217;ll need to have Python installed on your machine. You can download Python from the official Python website if it&#8217;s not already installed.<\/p>\n<p><\/p>\n<h2>Setting Up Your Environment<\/h2>\n<p><\/p>\n<p>To get started with Django, you&#8217;ll need to set up your development environment. This involves installing Django and creating a virtual environment.<\/p>\n<p><\/p>\n<h3>Installing Django<\/h3>\n<p><\/p>\n<pre class=\"code-block\"><code>pip install django<\/code><\/pre>\n<p><\/p>\n<p>After installing Django, confirm the installation by typing the following command:<\/p>\n<p><\/p>\n<pre class=\"code-block\"><code>django-admin --version<\/code><\/pre>\n<p><\/p>\n<h3>Creating a Virtual Environment<\/h3>\n<p><\/p>\n<p>A virtual environment is a self-contained directory that contains a Python installation for a particular version of Python, plus a number of additional packages that you can install. Creating a virtual environment is considered best practice as it keeps dependencies required by different projects in separate places.<\/p>\n<p><\/p>\n<pre class=\"code-block\"><code><br \/>\n    python -m venv myenv<br \/>\n    source myenv\/bin\/activate  # On Unix\/Mac<br \/>\n    myenv\\Scripts\\activate  # On Windows<br \/>\n    <\/code><\/pre>\n<p><\/p>\n<h2>Starting a New Django Project<\/h2>\n<p><\/p>\n<p>With Django installed and your virtual environment activated, you can start a new project. In Django, a project is a collection of configuration and apps for a particular website. Let&#8217;s create a new project called &#8220;myfirstproject&#8221;.<\/p>\n<p><\/p>\n<pre class=\"code-block\"><code>django-admin startproject myfirstproject<\/code><\/pre>\n<p><\/p>\n<p>This command creates a &#8220;myfirstproject&#8221; directory with the following structure:<\/p>\n<p><\/p>\n<pre class=\"code-block\"><code><br \/>\n    myfirstproject\/<br \/>\n        manage.py<br \/>\n        myfirstproject\/<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<p>The manage.py file is a command-line utility that lets you interact with your project. The second &#8220;myfirstproject&#8221; directory is the actual Python package for your project. It&#8217;s where settings, URLs, and WSGI configurations are stored.<\/p>\n<p><\/p>\n<h2>Running the Development Server<\/h2>\n<p><\/p>\n<p>Django comes with a lightweight web server for development and testing purposes. To start it, navigate to the &#8220;myfirstproject&#8221; directory containing the manage.py file and run:<\/p>\n<p><\/p>\n<pre class=\"code-block\"><code>python manage.py runserver<\/code><\/pre>\n<p><\/p>\n<p>You should see output indicating the server is running, typically on <a href=\"http:\/\/127.0.0.1:8000\/\" target=\"_blank\">http:\/\/127.0.0.1:8000\/<\/a>. Open this URL in your web browser, and you&#8217;ll see the Django welcome page.<\/p>\n<p><\/p>\n<h2>Creating a New App<\/h2>\n<p><\/p>\n<p>A Django app is a web application that does something \u2013 e.g., a blog, a database of public records, or a simple poll app. Create a new app named &#8220;hello&#8221; inside your project:<\/p>\n<p><\/p>\n<pre class=\"code-block\"><code>python manage.py startapp hello<\/code><\/pre>\n<p><\/p>\n<p>This command creates a new directory called &#8220;hello&#8221; with the following basic structure:<\/p>\n<p><\/p>\n<pre class=\"code-block\"><code><br \/>\n    hello\/<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>Writing Your First View<\/h2>\n<p><\/p>\n<p>In Django, a view is a Python function that receives a web request and returns a web response. First, edit the views.py file in your &#8220;hello&#8221; app to include a simple view:<\/p>\n<p><\/p>\n<pre class=\"code-block\"><code><br \/>\n    from django.http import HttpResponse<br>def hello_view(request):<br \/>\n        return HttpResponse(\"Hello, world!\")<br \/>\n    <\/code><\/pre>\n<p><\/p>\n<h2>Configuring URLs<\/h2>\n<p><\/p>\n<p>To make the view accessible, you&#8217;ll need to map a URL to it. Create a new file called urls.py in the &#8220;hello&#8221; app directory and include the following code:<\/p>\n<p><\/p>\n<pre class=\"code-block\"><code><br \/>\n    from django.urls import path<br \/>\n    from . import views<br>urlpatterns = [<br \/>\n        path('hello\/', views.hello_view, name='hello'),<br \/>\n    ]<br \/>\n    <\/code><\/pre>\n<p><\/p>\n<p>Next, include this URL pattern in the main project&#8217;s urls.py file:<\/p>\n<p><\/p>\n<pre class=\"code-block\"><code><br \/>\n    from django.contrib import admin<br \/>\n    from django.urls import include, path<br>urlpatterns = [<br \/>\n        path('admin\/', admin.site.urls),<br \/>\n        path('hello\/', include('hello.urls')),<br \/>\n    ]<br \/>\n    <\/code><\/pre>\n<p><\/p>\n<h2>Running Your Web App<\/h2>\n<p><\/p>\n<p>With the server running, visit <a href=\"http:\/\/127.0.0.1:8000\/hello\/\" target=\"_blank\">http:\/\/127.0.0.1:8000\/hello\/<\/a> in your web browser. You should see &#8220;Hello, world!&#8221; displayed on the page.<\/p>\n<p><\/p>\n<h2>Conclusion<\/h2>\n<p><\/p>\n<p>Congratulations! You&#8217;ve successfully created your first Django web app. This guide covered the basics of setting up a Django project, creating a simple app, and running a development server. From here, you can explore Django&#8217;s powerful features, such as its ORM for database operations, user authentication, admin interface, and more. Django&#8217;s comprehensive documentation is an excellent resource for deepening your knowledge and building more complex applications. Happy coding!<\/p>\n<p><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Django is a powerful, high-level web framework for the Python programming language. It encourages rapid development and clean, pragmatic design. If you&#8217;re interested in learning how to build web applications using Django, this guide will help you create a simple web app from scratch. By the end of this article, you&#8217;ll have a basic understanding [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":14867,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[58],"tags":[75,290,286,74],"class_list":["post-14866","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-app","tag-django","tag-started","tag-web"],"_links":{"self":[{"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/posts\/14866","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=14866"}],"version-history":[{"count":0,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/posts\/14866\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/media\/14867"}],"wp:attachment":[{"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/media?parent=14866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/categories?post=14866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/tags?post=14866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}