学习笔记,仅供参考



文章目录




Django框架开发



创建项目的指令



创建项目的指令:

django-admin startproject 项目名称
#比如
django-admin startproject mywebsite1



  • 举个例子

我们先进入一个指定的文件夹,再输出如下命令:

F:\MyStudio\PythonStudio\goatbishop.project01\Django>django-admin startproject mywebsite1

查看该文件夹下的文件:

F:\MyStudio\PythonStudio\goatbishop.project01\Django>dir /o
驱动器 F 中的卷是 山羊卷
卷的序列号是 3C68-9CA1

F:\MyStudio\PythonStudio\goatbishop.project01\Django 的目录

2020/06/13 11:15 <DIR> .
2020/06/13 11:15 <DIR> ..
2020/06/13 11:15 <DIR> mywebsite1
0 个文件 0 字节
3 个目录 20,644,282,368 可用字节

可以看到我们多了一个mywebsite1文件夹,进入mywebsite1文件夹,查看文件夹下内容:

F:\MyStudio\PythonStudio\goatbishop.project01\Django>cd ./mywebsite1

F:\MyStudio\PythonStudio\goatbishop.project01\Django\mywebsite1>dir
驱动器 F 中的卷是 山羊卷
卷的序列号是 3C68-9CA1

F:\MyStudio\PythonStudio\goatbishop.project01\Django\mywebsite1 的目录

2020/06/13 11:15 <DIR> .
2020/06/13 11:15 <DIR> ..
2020/06/13 11:15 651 manage.py
2020/06/13 11:15 <DIR> mywebsite1
1 个文件 651 字节
3 个目录 20,644,278,272 可用字节

我们看到mywebsite1文件夹下还有一个mywebsite1文件夹,并且多了一个manage.py

现在,我们执行下面的命令:

F:\MyStudio\PythonStudio\goatbishop.project01\Django\mywebsite1>python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 10, in main
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 16, in main
) from exc
ImportError: Couldn't import Django. Are you sure it's installed and available on you
r PYTHONPATH environment variable? Did you forget to activate a virtual environment?

报错了!

按照提示信息,我们现在配置一下环境变量,将下面两条路径加到环境变量中去:

#diango的路径
F:\software\Anaconda\Lib\site-packages\django;
#python的scripts路径
F:\software\Anaconda\Scripts;



配置完成后,我们再次运行下面的代码:

F:\MyStudio\PythonStudio\goatbishop.project01\Django\mywebsite1>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 17 unapplied migration(s). Your project may not work properly until you appl
y the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
June 13, 2020 - 12:48:32
Django version 2.2.13, using settings 'mywebsite1.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

在结果中,我们看到下面这行字:

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

它表示,我们已经在http://127.0.0.1:8000/地址上开始了一个开发的服务。

现在,我们打开浏览器,输入http://127.0.0.1:8000/:

Django(part2)--创建项目与Django目录结构_django



成功了!这说明,我们的服务器就已经搭建好了。



我们停止这个服务(Ctrl+c),再运行下面这行代码,改变服务器绑定的端口号为5000:

F:\MyStudio\PythonStudio\goatbishop.project01\Django\mywebsite1>python manage.py runserver 5000
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 17 unapplied migration(s). Your project may not work properly until you appl
y the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
June 13, 2020 - 13:39:57
Django version 2.2.13, using settings 'mywebsite1.settings'
Starting development server at http://127.0.0.1:5000/
Quit the server with CTRL-BREAK.

现在,我们在浏览器中打开http://127.0.0.1:5000/:

Django(part2)--创建项目与Django目录结构_django_02

OK!



Django目录结构



Django(part2)--创建项目与Django目录结构_配置文件_03



  • manage.py
  • 此文件是项目管理的主程序,在开发阶段用于管理整个项目的开发运行的调试。当manage.py启动时,他会调用mywebsite1包里的内容。
  • manage.py包含项目管理的子命令,如:
  • ​python manage.py runserver​​ 启动服务
  • ​python manage.py startapp​​ 创建应用
  • ​python manage.py migrate​​ 数据库迁移

  • mywebsite1项目包文件夹
  • 项目包的主文件夹(默认与项目名称一致)
  • ​__init__.py​
  • 项目初始化文件,服务器启动时自动运行
  • ​wsgi.py​
  • WEB服务网关接口的配置文件,仅部署项目时使用
  • ​urls.py​
  • 项目的基础路由配置文件,所有的动态路径必须先走该文件进行匹配
  • ​settings.py​
  • Django的配置文件,此配置文件中的一些全局变量将为Django框架的运行传递参数
  • 启动服务时,会自动调用该文件
  • 此配置文件中也可以定义一些自定义的变量用于作用全局作用域的数据传递