• Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Djangos Introduction to Hello World

Everything comes with Hello World. As Django, of course, that comes with its own Hello World, too. Today I’m going to show you several different ways to use Hello World to indicate that I’ve gone to Django

1. The use of HttpResponse

HttpResponse is an HttpRequest that comes with Django. It has a response function, and a corresponding request function, namely, HttpRequest

1.1 Opening the Project

Open our project in the VSC, and then, then, don’t know what to do.

Does it matter? It doesn’t matter!

I’ll tell you what to do, I told you what to do, it’s a favor; You didn’t do it. It’s an accident. What do you call it? It’s called the world! Any questions? No problem!

No problem then come and listen to me!

1.2 Creating an App

What is app? It’s the application, the little modules under your Web project.

You can understand it as:

You are reading a book

This book is a project

Each chapter of the book is an APP

His role is to simplify your project so that it doesn’t clutter up and make writing and maintenance seem effortless.

So how do you build an app? — Terminal input: python manage.py startApp App name

If you get an error, it means that your directory is not in the project’s main directory, which contains manage.py. Switch back.

And then the console doesn’t give you any feedback, you need to look at your project directory, and you’ll notice that there’s an extra folder, and the name is the name of your app.

Then you click on it and see the structure inside:

Migrations folder – Generates migration files, as discussed later

__init__.py — a file that makes folders into python recognizable packages

Admin.py – automatically generated and can be left alone

View.py – Where the view is written, the view can be understood as the back-end code of an HTML page

Models.py — a place to write code that interacts with the database

Apps.py — Register the app

Tests.py — Unit tests

Today we only use views.py and urls.py in our home directory

1.3 open views. Py

In this py file, import HttpResponse first

from django.http import HttpResponse

Copy the code

Then you start writing views. Views in Django are written by functions, but you can also use classes and functions

def Hello(request):

    return HttpResponse("Hello Woeld!")

Copy the code

And then there was no more

1.4 Configuring Routes

Open the urls.py file in your home directory and import the view we’ve written

from Test.view import Hello

Copy the code

Then add your path to the urlpatterns below

urlpatterns = [

    path('admin/', admin.site.urls),

    path('Test', Hello),

]

Copy the code

And then there was no more

1.5 You’re done

We enter Python manage.py runServer at the terminal in the project home directory

Signs of success are:

      

Watching for file changes with StatReloader

Performing system checks...



System check identified no issues (0 silenced).

September 25, 2021 - 11:46:01

Django Version 3.2.7, using Settings 'DjangoStudy. Settings'

Starting development server at http://127.0.0.1:8000/

Quit the server with CTRL-BREAK.



Copy the code

Then click on the http://127.0.0.1:8000/ in the browser’s address bar plus/Test here, which is http://127.0.0.1:8000/Test, enter, you can see your Hello World!

2. Using HTML

2.1 Creating the Templates folder

Create this folder and inside it create an HTML file with the following code:

<! DOCTYPEhtml>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="Width = device - width, initial - scale = 1.0">

    <title>Document</title>

</head>

<body>

    Hello World!

</body>

</html>

Copy the code

2.2 configuration Settings. Py

Open settings.py in your home directory, find line 60, and change it to:

'DIRS': [os.path.join(BASE_DIR, 'templates')].

Copy the code

Use the templates folder you created as the templates folder

2.3 Writing a View

Views.py in app folder

def Hello1(request):

    return render(request, '.. /templates/hello.html')

Copy the code

2.4 Configuring Routes

Open urls.py in your home directory

path('Hello1', Hello1)

Copy the code

2.5 You’re done

To start the project, type http://127.0.0.1/hello1 in your browser

You can see Hello World! the