Displaying "Hello, World!" in Django

One of the simplest yet most satisfying milestones when learning a new web framework is displaying "Hello, World!" on your web page. In Django, this involves creating a view that returns a response, mapping a URL to that view, and running the development server to see the result. This tutorial will guide you through each step to display your first "Hello, World!" in Django.

Step 1: Create a New Django App (Optional)

If you haven’t already created a Django app within your project, now is a good time to do so. An app is where you’ll write the code that handles different functionalities in your project.

To create a new app called hello, run the following command from your project’s root directory:

python manage.py startapp hello

 Terminal showing the command to create a new Django app named "hello."

Step 2: Define the "Hello, World!" View

A view in Django is a function or class that takes a web request and returns a web response. To display "Hello, World!", you'll create a simple view in your app’s views.py file.

  1. Open the views.py file in the hello app directory.
  2. Add the following code to create a view that returns "Hello, World!":
from django.http import HttpResponse

def hello_world(request):
    return HttpResponse("Hello, World!")

Explanation:

  • HttpResponse: This is a class provided by Django that returns an HTTP response with the text "Hello, World!".
  • hello_world function: This function takes a request object as an argument and returns an HttpResponse object.

A views.py file with the hello_world view function highlighted.

Step 3: Map the View to a URL

Next, you need to map a URL to your hello_world view so that when someone visits a specific URL in their browser, Django knows to call that view.

  1. In your hello app directory, create a new file named urls.py if it doesn’t already exist.
  2. Add the following code to urls.py to map a URL to the hello_world view:
from django.urls import path
from . import views

urlpatterns = [
    path('hello/', views.hello_world, name='hello_world'),
]

Explanation:

  • path(): This function is used to define a URL pattern. It maps the URL 'hello/' to the hello_world view.
  • name='hello_world': Naming your URL pattern makes it easier to reference in templates and other parts of your project.

A urls.py file with the URL pattern for the hello_world view highlighted.

Step 4: Include the App URLs in the Project

To make the hello/ URL accessible, you need to include your app’s urls.py in the project’s main urls.py file.

  1. Open the main urls.py file located in the inner project directory (the one with settings.py).
  2. Add an include statement to reference your app’s URLs:
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('hello.urls')),  # Include the hello app's URLs
]

Explanation:

  • include(): This function allows you to include URL patterns from your app’s urls.py in the main project’s urls.py.
  • path('', include('hello.urls')): This line means that any URLs defined in hello.urls will be accessible from the root of the project’s URL structure.

The main urls.py file with the include statement highlighted.

Step 5: Run the Development Server

With everything set up, it’s time to run the Django development server and see your "Hello, World!" in action.

  1. Run the following command from your project’s root directory:
python manage.py runserver

Image: Terminal showing the command to run the Django development server.

  1. Open your web browser and go to http://127.0.0.1:8000/hello/.

You should see the text "Hello, World!" displayed on the screen!

Web browser displaying "Hello, World!" from the Django app.

Conclusion

Congratulations! You’ve just displayed your first "Hello, World!" using Django. This simple example demonstrates the core flow of how Django handles requests and responses: from defining a view, mapping it to a URL, and running the server to see the output. With this foundational knowledge, you’re now ready to start building more complex views and applications.