创建第一个Django项目

Alright fellows, so in this tutorial we’re going to create our very first django project. Here I am assuming that you all have installed Django 2 in your system. Django has a great way for us whenever you want to start a new project. It simply creates a new template for us then we can customize it however like to.

好的,各位,所以在本教程中,我们将创建第一个django项目。 在这里,我假设大家都已经在系统中安装了Django 2。 每当您要开始一个新项目时,Django对我们来说都是一种很好的方法。 它只是为我们创建了一个新模板,然后我们可以根据需要自定义它。

如何创建Django项目 (How to Create Django Project)

Open terminal or command prompt and navigate to the directory where you want to save your project. Let’s say if you want to save your project on Desktop then open terminal/command prompt and type:

打开终端或命令提示符,然后导航到要保存项目的目录。 假设您要在桌面上保存项目,然后打开终端/命令提示符并输入:

cd Desktop 

cd桌面

First Django Project 1

Now you are in your desktop directory, to create a new project type following command and hit enter:

现在,您在桌面目录中,按照以下命令创建新的项目类型,然后按Enter键:

django-admin startproject  project_name

django-admin startproject project_name

First Django Project 2

In above picture django-admin is a command that can do lot of other stuff for us, to see all the possible commands for django-admin just type below command and hit enter.

上图中django-admin是可以为我们做很多其他事情的命令,只需在命令下方键入并按Enter,即可查看django-admin的所有可能命令。

django-admin help

django-admin帮助

It will show you all options that we can use along with django-admin command. Start project is showing us that we’re going to create a new project and my_website is the name of our project, you can name it as you want.

它将显示您可以与django-admin命令一起使用的所有选项。 启动项目向我们显示了我们将要创建一个新项目,而my_website是我们项目的名称,您可以根据需要命名。

After this command, you can see that there is a directory created on your desktop with the name of your project.

执行此命令后,您可以看到在桌面上创建了一个目录,其中包含项目的名称。

First Django Project 3

Inside that folder you can see some Python files.

在该文件夹中,您可以看到一些Python文件。

First Django Project 4
First Django Project 5

Now we have our project. Let’s see that how we can run this little website.

现在我们有了我们的项目。 让我们看看如何运行这个小网站。

To do that let’s move back to the terminal and again navigate to Desktop if you are not already there. Now get into the directory of our project by typing following command:

为此,让我们回到终端,如果您还不在桌面,请再次导航到桌面。 现在,通过键入以下命令进入我们项目的目录:

cd project_name

cd project_name

First Django Project 6

We’re inside our project, now type ls command to see the files in our project.

我们在我们的项目中,现在键入ls命令以查看我们项目中的文件。

First Django Project 7

Here you can see that there is a file named as manage.py, which let’s us start our website and it will grab all the files from the directory my_website that is inside our project folder (you can see in above picture).

在这里,您可以看到有一个名为manage.py的文件让我们启动我们的网站,它将抓取项目文件夹中my_website目录中的所有文件(如上图所示)。

如何运行Django项目 (How to Run Django Project)

To run and test our project type the command below:

要运行和测试我们的项目,请输入以下命令:

python3 manage.py runserver

python3 manage.py运行服务器

First Django Project 8

If you have only Python 3 installed in your system then you can use python instead of python3 in above command.

如果您的系统中仅安装了Python 3,则可以在上面的命令中使用python代替python3

This command will start a little web-server on our computer (hosting our django project). See the output in above picture, there is a line saying “Starting development server at http://127.0.0.1:8000/”. So now copy this url http://127.0.0.1:8000/ and open it in any internet browser.

此命令将在我们的计算机上启动一个小的Web服务器(托管django项目)。 参见上图的输出,一行显示为“在http://127.0.0.1:8000/上启动开发服务器”。 因此,现在复制此URL http://127.0.0.1:8000/并在任何Internet浏览器中将其打开。

First Django Project 9

If all steps goes right then you’ll get the screen like above picture. If yes then Congratulation your website is up and running here in your local computer.

如果所有步骤都正确,那么您将获得如上图所示的屏幕。 如果是,那么恭喜您的网站已在本地计算机上正常运行。

Now in the link we have http://127.0.0.1:8000/, here 127.0.0.1 is localhost, that means your own computer and 8000 is port where our web-server is running.

现在在链接中有http://127.0.0.1:8000/,这里的127.0.0.1是localhost,这意味着您自己的计算机,而8000是运行Web服务器的端口。

To stop web-server just press ctrl+c (Control c) in terminal.

要停止Web服务器,只需在终端中按ctrl + c(控制c)。

That’s all for this tutorial. If you’re facing any problem in any steps mentioned above then please let us know in comments.

这就是本教程的全部内容。 如果您在上述任何步骤中遇到任何问题,请在评论中告知我们。

翻译自: https://www.thecrazyprogrammer.com/2018/09/first-django-project.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值