Python Django | 教程 | 2.创建项目
创建项目的方式很多,这里演示两种:
- 命令行的方式
- PyCharm的图形化的方式
创建项目的方式,不论操作系统是Linux或者MS Windows,方法都是一样的。
命令行方式:创建项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
[root@oracle1 ~]# mkdir /django_data [root@oracle1 ~]# [root@oracle1 ~]# cd /django_data/ [root@oracle1 django_data]# [root@oracle1 django_data]# ls -ltr total 0 [root@oracle1 django_data]# [root@oracle1 django_data]# whereis django-admin django-admin: /usr/local/bin/django-admin.py /usr/local/bin/django-admin [root@oracle1 django_data]# [root@oracle1 django_data]# django-admin startproject django_datacenter [root@oracle1 django_data]# [root@oracle1 django_data]# ls -ltr total 0 drwxr-xr-x 3 root root 48 Jun 15 15:38 django_datacenter [root@oracle1 django_data]# [root@oracle1 django_data]# ls -ltr django_datacenter/ total 4 -rwxr-xr-x 1 root root 673 Jun 15 15:38 manage.py drwxr-xr-x 2 root root 89 Jun 15 15:38 django_datacenter [root@oracle1 django_data]# [root@oracle1 django_data]# tree django_datacenter/ django_datacenter/ ├── django_datacenter │ ├── asgi.py │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── manage.py 1 directory, 6 files [root@oracle1 django_data]# |
上面的命令成功的创建了一个Django的项目。
但是可以看到,它会在当前目录下新建一个与指定的Django项目名称一样的名称的目录,并且将项目相关的文件都写入这个新创建的目录中。
如果,你需要在django-admin创建项目的时候,就指定Django项目创建到哪里,那么可以按照下面的方法来做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
[root@oracle1 django_data]# pwd /django_data [root@oracle1 django_data]# [root@oracle1 django_data]# ls -ltr total 0 drwxr-xr-x 3 root root 48 Jun 15 15:38 django_datacenter [root@oracle1 django_data]# [root@oracle1 django_data]# mkdir django_create_project [root@oracle1 django_data]# [root@oracle1 django_data]# ls -ltr total 0 drwxr-xr-x 3 root root 48 Jun 15 15:38 django_datacenter drwxr-xr-x 2 root root 6 Jun 15 15:45 django_create_project [root@oracle1 django_data]# [root@oracle1 django_data]# ls -ltr django_create_project/ total 0 [root@oracle1 django_data]# [root@oracle1 django_data]# django-admin startproject djangoProject_1 django_create_project/ [root@oracle1 django_data]# [root@oracle1 django_data]# ls -ltr total 0 drwxr-xr-x 3 root root 48 Jun 15 15:38 django_datacenter drwxr-xr-x 3 root root 46 Jun 15 15:46 django_create_project [root@oracle1 django_data]# [root@oracle1 django_data]# ls -ltr django_create_project/ total 4 -rwxr-xr-x 1 root root 671 Jun 15 15:46 manage.py drwxr-xr-x 2 root root 89 Jun 15 15:46 djangoProject_1 [root@oracle1 django_data]# |
命令行方式:运行项目
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@oracle1 django_datacenter]# python3 manage.py runserver Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. June 15, 2021 - 08:06:10 Django version 3.2.4, using settings 'django_datacenter.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. |
可以看到,Django已经运行起来了,上面的告警:18 unapplied migration(s) … 暂时不用管。
浏览器访问:
http://127.0.0.1:8000

图形化方式:创建项目
图形化的方式,通过IDE工具PyCharm:




图形化方式:运行项目


打开浏览器,访问:
http://127.0.0.1:8000/
