Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer View | Write the First Page
Django: First Dive

bookView

Now, we will write the Hello, World! page in the Django project.

First, we need to implement the hello_world in our new app. Open the app folder, find the views.py file, and open this file. Look at the file location:

The views.py file structure:

from django.shortcuts import render

# Create your views here.

Note

The suggested render function will be described later.

Views should return the HttpResponse object.

Now, let's write the hello_world_view function (view).

First, import the HTTP response object:

from django.http import HttpResponse

HttpResponse object should be imported from django.http module.

The HttpResponse object converts the Python string to HTML:

from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.

def hello_world_view(request):
    return HttpResponse("<h1>Hello, world!</h1>")

To make our view work with a request, it needs to be connected to a specific URL. Let's see how to do this in the next chapter.

question mark

What should views return?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 1

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Stel mij vragen over dit onderwerp

Vat dit hoofdstuk samen

Toon voorbeelden uit de praktijk

Awesome!

Completion rate improved to 3.45

bookView

Veeg om het menu te tonen

Now, we will write the Hello, World! page in the Django project.

First, we need to implement the hello_world in our new app. Open the app folder, find the views.py file, and open this file. Look at the file location:

The views.py file structure:

from django.shortcuts import render

# Create your views here.

Note

The suggested render function will be described later.

Views should return the HttpResponse object.

Now, let's write the hello_world_view function (view).

First, import the HTTP response object:

from django.http import HttpResponse

HttpResponse object should be imported from django.http module.

The HttpResponse object converts the Python string to HTML:

from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.

def hello_world_view(request):
    return HttpResponse("<h1>Hello, world!</h1>")

To make our view work with a request, it needs to be connected to a specific URL. Let's see how to do this in the next chapter.

question mark

What should views return?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 1
some-alt