Create A Hello World! Application Using Django Framework

Create A Hello World! Application Using Django Framework

Table of contents

Check out my previous blog about the Introduction to Django Framework.

Django is a high-level web framework that is designed to make it easy to build web applications quickly and efficiently.

It provides many useful features out of the box, such as an ORM (Object-Relational Mapping) for working with databases, a templating system for building user interfaces, and built-in support for handling user authentication and security.

In this blog, We are going to create a “Hello, World!” application, we used some of these features, such as defining a simple view function that returns an HTTP response, defining a URL pattern that maps to that view function, and running the Django development server to serve our application locally.

While the “Hello, World!” application is a very basic example, it can serve as a starting point for building more complex web applications using Django.

With Django’s rich feature set, it is possible to build web applications of all sizes and complexities, from simple blogs to large-scale e-commerce websites and social networks.

[Note: Use Virtual Environment for Good. 👍]

Steps

  1. Install Django: If you haven’t installed Django, use the following command to install it via pip:

     pip install Django
    
  2. Create a Django project: Use the following command to create a new Django project:

     django-admin startproject myproject
    

This will create a new Django project in a directory called “myproject”.

  1. Create a Django app: Use the following command to create a new Django app (django application)within the project:

     cd myproject
     python manage.py startapp myapp
    

    This will create a new Django app within the project called “myapp”.

  2. Add the app to the project: In the file myproject/settings.py, add the name of the app myapp to the INSTALLED_APPS list:

     INSTALLED_APPS = [
         # Add this line in your settings.py file inside myproject folder
         'myapp',
         # ...
     ]
    
  3. Create a view: In the file myapp/views.py, add the following code:

     from django.http import HttpResponse
    
     def hello(request):
         return HttpResponse("Hello, World!")
    

    This code defines a simple view called hello, that returns an HTTP response with the text “Hello, World!”.

  4. Create a URL: In this create a URL route file myapp/urls.py, add the following code:

     from django.urls import path
     from . import views
    
     urlpatterns = [
         path('hello/', views.hello, name='hello'),
     ]
    

    This code defines a URL pattern that maps the URL path /hello/ to the hello view we defined earlier.

  5. Add myapp URL: In this, for accessing our application URLs we have to add the following line to the myproject/urls.py file

     from django.contrib import admin
     from django.urls import path, include # <-- 1. add this 
    
     urlpatterns = [
    
         path('admin/', admin.site.urls),
    
         # 2. add this line too
         path('', include('myapp.urls')),
    
     ]
    

Project Hierarchy/Structure

  1. Run the development server: Use the following command to run the development server:

     python manage.py runserver
    

    This will start the Django development server, which you can access by opening a web browser.

  2. Hit this URL: Open this URL in the browser to see results.

    http://127.0.0.1:8000/hello/

    You should see the text Hello, World! displayed in the browser.

    That’s it! You have created a “Hello, World!” web page using Django.


Thank you for taking the time to read this blog about Create A Hello World! Application Using Django Framework. I hope that you found the information presented here useful and informative.

If you have any questions or comments about the information presented in this blog, please feel free to reach out. Thank you again for reading!

Resources

Django Framework Documentation

Python Documentation

Introduction to Django Framework