In the last section, we learned how to start a service and get your colleagues to your Django default page. Next, we’ll learn how to understand and manipulate a custom page that your colleagues can access.

You probably already know the hidden Easter egg in baidu’s home page, which can be seen in f12’s Console. It is always changed, and now it is this:

I remember when I first discovered it many years ago, the Easter egg was: “How does a web page come to the user, a…. How is… , a… How does. Send it to us.”

So the question we’ll discuss in this section is how an HTML web page can be rendered to you and your colleagues’ browsers using Django.

The first thing to understand is that a web page is made up of the following parts:

1. HTML template: Equivalent to a tree trunk

2. Specific data: dynamic leaves

3. HTML static language: it is the language of tree trunk and branches

4. Js scripting language: it is the script in which we see various actions on the page

5. CSS style: This is a decorative method to beautify the tree trunks and leaves

Ok, let’s start by sorting out the logic of how users visit your home page:

  1. The user opens a browser and enters the url

  2. Your Django service receives this URL

  3. Your Django service finds the corresponding background function based on this URL

  4. Once you find the background function, you see that it does one thing: it returns an HTML home page template, plus the original data. Package to your browser

  5. Your browser receives the HTML template and data and presents it to you as a complete web page.

Ok, let’s put it into practice in the project according to the above process:

The first step is to write a mapping between your URL and a background function in a Django service, so that Django knows how to find the corresponding background function based on the URL.

This mapping is written in urls.py, which sounds fancy, but is actually a list, and each element is a mapping. Each element calls a library function, passing it two values, the first being your URL and the second the name of your background function.

The format is as follows:

[

Library functions (‘ your URL 1’, ‘your background function name 1’),

Library functions (‘ your URL 2’, ‘your background function name 2’),

]

This is known as the URL distribution Routing manager.

Go ahead and find the urls.py file

It has automatically generated a map for you, and you can write subsequent ones in its format.

However, our background functions are written in views.py, so we can create others ourselves. So here we import all of views.py:

Add a new element to the urlPattens list below:

Url (r’^welcome/$’,welocme), # go to the home page

Here, welcom is marked in red, but this is normal because we haven’t written the welcome function yet, so it’s strange that it’s not red. Don’t click on anything other than PyCharm, or Django will detect your file changes and restart automatically. If the code is marked red in the restart cutscene, the service will be terminated.

We must now go directly to the views.py file to write the welcome function.

Note that the parameter must include a request(you can change it yourself), which must exist as long as it is a function mapped to urls.py. It contains all of the requested information, such as the requestor’s IP, login user name, HTTP request, and so on.

To make it simple, I just output a string.

Now we go back to urls.py and find that it is no longer marked red, indicating that the mapping has been successful.

In front of this writing method, is the regular matching writing method, we are interested in the follow-up can be studied, here the new advice to copy the gourd gourd. Do not write/must write /$.

Let’s now open the web page and type in the url: 127.0.0.1:8000/welcome to see what it looks like:

Web page error, do not panic, this is normal. It’s saying, you didn’t return something to the browser to show a thread?

But there is print in the background function, and this print is printed in your console, so why don’t you check it?

This output proves that our link has actually been opened. Don’t worry about the error, it also means that you didn’t return a page or something to the browser.

So what we’re going to do is we’re going to return something to the browser, and we’re going to do a simple one, which is return a sentence.

Import two important packages for the front-end return.

from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render
Copy the code

The HttpResponse function is called to return a string, as well as json strings that are returned later. HttpResponseRedirect is used to redirect to other urls. Render is used to return the HTML page and the initial data of the page.

After writing, we click on other places besides PyCharm, such as wechat, QQ and webpage. Pycharm will then start checking your code, and when pyCharm finds that you have made changes, it will restart Django to make your changes work.

After seeing the console output another white text that was only available during startup, there are no other error messages, indicating that the restart is successful and your changes should take effect. Go to the url again or refresh:

http://127.0.0.1:8000/welcome/

What’s on display?

See if this is successfully demonstrated? Our copy!

Well, that’s all for today, and the next section starts with making a web page and calling the render function to return it.

Like a friend to share and watch, thanks ~

This article uses the article synchronization assistant to synchronize