---
# System prepended metadata

title: PID-1115

---

In Django, You can write  "Hello World!" in different ways. Let's explore the two common ways to achieve this simple program [1][2][4]:
1. Function-Based View:
    Let's create a function-based view in Django, to display "Hello, World!" on the webpage.
    Program code:
    ```python
    # views.py
    from django.http import HttpResponse
    #import the files 
    # create the function hello_world
    def hello_world(request):
        return HttpResponse("Hello, World!")

    # urls.py
    from django.urls import path
    from . import views

    #set the urlpatterns
    urlpatterns = [
        path('hello/', views.hello_world, name='hello_world'),
    ]
    ```
2. Class-Based view:
    Program code:
    ```python
    # views.py
    from django.http import HttpResponse
    from django.views import View

    # create the class HelloworldView

    class HelloWorldView(View):
    # define the function get inside the class
        def get(self, request):
            return HttpResponse("Hello, World!")

    # urls.py
    from django.urls import path
    from . import views

    urlpatterns = [
        path('hello/', views.HelloWorldView.as_view(), name='hello_world'),
    ]
    ```    
    Explaination of code:
     1. The code initiates by importing the essential libraries: `HttpResponse` from `django.http` and View from `django.views`.
	2. A class named `HelloWorldView` is defined. This class is a subclass of the `View` class. The overridden get method within this class ensures that an HTTP response is generated, delivering the message "Hello, World!".
	3. Within the `urls.py` file, a URL pattern is configured. This pattern establishes a mapping between the URL /hello/ and the `HelloWorldView` class-based view.
	4. When a user accesses the URL /hello/, Django's routing mechanism triggers the invocation of the get method within the `HelloWorldView` class. Consequently, an HTTP response containing the "Hello, World!" message is displayed as the output to the user.