Lesson 2: Rendering HTML Templates to Display a Simple Webpage in Django

Posted by

How to Display a Simple Webpage in Django – Lesson 2

How to Display a Simple Webpage in Django

Lesson 2: Rendering HTML Templates

In Lesson 1, we learned how to create a basic Django project and app. Now, in Lesson 2, we will learn how to display a simple webpage in Django by rendering HTML templates.

Step 1: Create a HTML Template

The first step is to create a HTML template for our webpage. Create a new folder called ‘templates’ in your app directory. Inside this folder, create a new HTML file called ‘index.html’.

        <!DOCTYPE html>
        <html>
        <head>
            <title>My First Django Webpage</title>
        </head>
        <body>
            <h1>Welcome to My First Django Webpage!</h1>
            <p>This is a simple webpage created using Django.</p>
        </body>
        </html>
    

Step 2: Update Views

Next, we need to update the views.py file in our app directory to render the HTML template we just created. Add the following code to your views.py file:

        from django.shortcuts import render
        
        def index(request):
            return render(request, 'index.html')
    

Step 3: Configure URL Mapping

Lastly, we need to configure URL mapping in our app’s urls.py file to connect the view function we just created to a specific URL. Add the following code to your urls.py file:

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

That’s it! Now when you navigate to the root URL of your Django project, you should see the simple webpage we created. Congratulations on successfully rendering HTML templates in Django!

0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@patrickmusyoka4623
1 month ago

You are a master…

@emmanuelmusembi3580
1 month ago

Cartoon😂😂😂