Django自定义网页

In this tutorial, we’re going to create our first custom webpage in django. The main goal of this article for you to understand the whole flow of information in django website like if someone asks for specific url then how do we route them into the correct places and ultimately give them back some HTML.

在本教程中,我们将在Django中创建第一个自定义网页。 本文的主要目的是让您了解django网站中的整个信息流,例如,如果有人要求特定的URL,那么我们如何将它们路由到正确的位置并最终给他们一些HTML。

So before starting this article I am assuming that you’ve started the local server using python3 manage.py runserver command in project directory.

因此,在开始本文之前,我假设您已经在项目目录中使用python3 manage.py runserver命令启动了本地服务器。

We’ve seen that whenever we create a project and run it in browser then django’s default page shows up.

我们已经看到,只要创建项目并在浏览器中运行它,就会显示django的默认页面。

Django自定义网页

That’s isn’t our creation right.

那不是我们的创作权利。

So let’s see how to create our own webpage in django.

因此,让我们看看如何在Django中创建自己的网页。

In previous articles, we’ve seen that anytime someone is looking for a URL on our website it comes to this “urls.py”.

在以前的文章中,我们已经看到,只要有人在我们的网站上查找URL,就会涉及到该“ urls.py”。

Currently we’ve path of admin/ in the list urlpatterns. That means when user goes to our website (currently = http://127.0.0.1:8000/)  and add a /admin in the url, user will be redirected to admin page of our django website.

目前,我们在列表urlpatterns中有admin /路径 这意味着当用户访问我们的网站(当前= http://127.0.0.1:8000/)并在URL中添加/ admin时,用户将被重定向到我们django网站的管理页面。

Django自定义网页

Note: That domain-name/admin (currently domain-name is http://127.0.0.1:8000/) is going to help us eventually work with the database but we don’t really need to worry about that right now.

注意:域名/管理员(当前域名为http://127.0.0.1:8000/)将帮助我们最终使用数据库,但是我们现在不必真正担心这一点。

如何更改现有网页的URL (How to Change URL of Existing Webpages)

Let’s say we want to change the URL for our admin page then we can modify our urls.py file like this:

假设我们要更改管理页面的URL,然后可以像这样修改urls.py文件:

Django自定义网页

Now open your web browser and open domain-name/admin (http://127.0.0.1:8000/admin)

现在打开您的Web浏览器并打开域名/ admin(http://127.0.0.1:8000/admin)

The result will be an error showing that page can not be found or something like this because we have changed the address of our admin page. So our new address is doman-name/mypage (http://127.0.0.1:8000/mypage). Open this address and the result will be:

结果将是显示无法找到该页面的错误或类似的信息,因为我们已更改了管理页面的地址。 因此,我们的新地址是doman-name / mypage(http://127.0.0.1:8000/mypage)。 打开此地址,结果将是:

Django自定义网页

That how we can change any existing web-page’s URL.

这样我们就可以更改任何现有网页的URL。

Note: In django, anytime you make a change to a file, it is going to auto-reload the server for you. So we don’t have to manually go back to stop and start the server to pick-up the new changes.

注意:在django中,只要您对文件进行更改,它将自动为您重新加载服务器。 因此,我们不必手动返回以停止并启动服务器来接管新的更改。

在Django中创建自己的自定义网页 (Creating Our Own Custom Webpage in Django)

Now our task is to make our own custom webpage so let’s do it.

现在我们的任务是创建自己的自定义网页,让我们开始吧。

Firstly, we don’t need that admin page so delete that path entirely.

首先,我们不需要该管理页面,因此请完全删除该路径。

Django自定义网页

In fact we can delete the first line about admin in our urls.py as we don’t need admin anymore.

实际上,我们可以在urls.py中删除有关admin的第一行,因为我们不再需要admin。

Django自定义网页

Now let’s say we’re going to add a path for our own page. Let’s say homepage. So when someone comes to the homepage of our website then we’ll show them our own custom homepage instead of that default django template page.

现在,我们要为自己的页面添加路径。 假设是首页。 因此,当有人访问我们网站的主页时,我们将向他们显示我们自己的自定义主页,而不是默认的django模板页面。

To do this, open your urls.py and add a new path. Basically if someone comes to our homepage means they don’t need to any thing extra our domain-name (http://127.0.0.1:8000/), so we’ll put a empty string in path, like this:

为此,请打开urls.py并添加新路径。 基本上,如果有人访问了我们的主页,则意味着他们不需要我们域名(http://127.0.0.1:8000/)以外的任何内容,因此我们将在路径中放入一个空字符串,如下所示:

Django自定义网页

Now in path put a comma after the empty string and after comma (,) we’ll add another thing that will show that when someone comes to our homepage then where we want to send the user to.

现在,在路径中,在空字符串后面加上逗号(,)之后,我们将添加另一件事,这表明当有人到达我们的主页时,我们要将用户发送到该位置。

Here we have to create a new file called views.py which essentially allows us to send back some information.

在这里,我们必须创建一个名为views.py的新文件,该文件实际上使我们能够发送回一些信息。

So we’ll create a new file in same directory where our urls.py exists. Now we’ve a new file called views.py here:

因此,我们将在urls.py存在的同一目录中创建一个新文件。 现在,我们在这里有了一个名为views.py的新文件:

Django自定义网页

To use views.py into our urls.py, we’ve to import views.py in urls.py file. So open urls.py and add the following line:

要将views.py用于我们的urls.py中,我们必须在urls.py文件中导入views.py 。 因此,打开urls.py并添加以下行:

Django自定义网页

In above picture dot (.) means current directory.

上图中的点(。)表示当前目录。

Now add a function to call in our path into urls.py.

现在添加一个函数以在我们的路径中调用urls.py。

Django自定义网页

It is showing that if someone goes to our homepage then call the function ‘home’ which is located in our views.py, but we don’t have any function called home in our views.py yet.

这表明,如果有人访问了我们的主页,则将其称为“ home”函数,该函数位于我们的views.py中,但我们的views.py中还没有任何名为home的函数。

So let’s create it. Open views.py and add a new function called home.

因此,让我们创建它。 打开views.py并添加一个名为home的新功能

Here we’ve to pass request parameter in home function, anytime someone is coming for URL of our website it sends this request object which basically says that what’s the url they looking for and some more advanced information like some cookies and what browser they are using. So that type of information comes through this request object.

在这里,我们必须在home函数中传递请求参数,每当有人访问我们网站的URL时,它都会发送该请求对象,该对象基本上说出他们正在寻找的url是什么,以及一些更高级的信息,例如cookie和正在使用的浏览器。 因此,这种类型的信息通过此请求对象来。

Then we’re returning something back to the user using return keyword But we can’t return a simple string back from our function, we’ve to give back an HTTP response. So in order to do that we’ve to use function HttpResponse(string) and to use HttpsResponse(string) we’ve to import some package using

然后,我们使用return关键字将某些内容返回给用户。但是我们无法从函数中返回简单的字符串,我们必须返回HTTP响应。 因此,为了做到这一点,我们必须使用功能HttpResponse(string)和使用HttpsResponse(string),我们必须使用

from django.http import HttpResponse

从django.http导入HttpResponse

Now save this file and reload your website. The output will be:

现在保存此文件并重新加载您的网站。 输出将是:

Django自定义网页

Congratulations, we have our own creation on our homepage.

恭喜,我们在首页上创建了自己的作品。

Django网站中的信息流 (Flow of Information in Our Django Website)

If somebody is opening our website’s homepage (http://127.0.0.1:8000/) then they will be redirected to our urls.py file. Urls.py check the entered url by the user. As there is no extra string after the domain-name in above example. So in urlpatterns, it will check for the empty string and as we’ve one path having an empty string, it will be redirected to the function written in path with empty string, that is views.home in our case. Now that home function in views.py is finally returning some HttpsResponse that is ‘hello’. So user have the information that he/she requested for.

如果有人打开我们网站的主页(http://127.0.0.1:8000/),那么他们将被重定向到我们的urls.py文件。 Urls.py检查用户输入的网址。 由于在上面的示例中,域名之后没有多余的字符串。 因此,在urlpatterns中,它将检查空字符串,并且当我们有一个路径具有空字符串时,它将被重定向到以空字符串的路径编写的函数,在本例中为views.home 。 现在, views.py中的home函数最终返回了一些“ hello”的 HttpsResponse 因此,用户可以获得他/她要求的信息。

That’s how the flow of information works in django.

这就是django中信息流的工作方式。

在Django中创建多个网页 (Creating Multiple Webpages in Django)

As we make one custom webpage, we can also add some more web-pages having unique addresses assigned to them.

当我们制作一个自定义网页时,我们还可以添加一些具有唯一地址的网页。

Open urls.py  and add a new path like:

打开urls.py并添加一个新路径,例如:

Django自定义网页

and open your views.py and create a new function for page1 .

并打开您的views.py并为page1创建一个新函数。

Django自定义网页

That’s all.

就这样。

Now go to domain-name/page1 (http://127.0.0.1:8000/page1and here is your page1:

现在转到domainname / page1( http://127.0.0.1:8000/page1 ) ,这是您的page1:

That’s how we can create multiple pages in our website.

这就是我们可以在我们的网站中创建多个页面的方式。

Actually that string we’re returning is some HTML so we can also use HTML tags in it like:

实际上,我们返回的字符串是一些HTML,因此我们也可以在其中使用HTML标记,例如:

Django自定义网页

and refresh http://127.0.0.1:8000/page1

并刷新http://127.0.0.1:8000/page1

Django自定义网页

结论 (Conclusion)

After reading and using this tutorial you’ll learn how to add a custom URL to any page, adding custom webpage in django website, creating multiple web-pages in django and the most important thing that is flow of information in django website.

阅读并使用了本教程之后,您将学习如何将自定义URL添加到任何页面,在django网站中添加自定义网页,在django中创建多个网页以及最重要的是django网站中的信息流。

Comment down below if you have any queries.

如果您有任何疑问,请在下面注释掉。

翻译自: https://www.thecrazyprogrammer.com/2018/10/django-custom-webpage.html