
1.7 PyCharm创建项目
除了在命令提示符窗口创建项目之外,还可以在PyCharm中创建项目。PyCharm必须为专业版才能创建与调试Django项目,社区版是不支持此功能的。打开PyCharm并在左上方单击File→New Project,创建新项目,如图1-6所示。

图1-6 PyCharm创建Django
项目创建后,可以看到目录结构多出了templates文件夹,该文件夹用于存放HTML模板文件,如图1-7所示。

图1-7 项目目录结构
接着创建App应用,可以在PyCharm的Terminal中输入创建指令,创建指令与命令提示符窗口中输入的指令是相同的,如图1-8所示。

图1-8 PyCharm创建App
完成项目和App的创建后,启动项目。如果项目是由PyCharm创建的,就直接单击“运行”按钮启动项目,如图1-9所示。

图1-9 PyCharm启动项目
如果项目是在命令提示符窗口创建的,想要在PyCharm启动项目,而PyCharm没有运行脚本,就需要对该项目创建运行脚本,如图1-10所示。

图1-10 创建运行脚本
单击图1-10中的Edit Configurations就会出现Run/Debug Configurations界面,单击该界面左上方的+并选择Django server,输入脚本名字,单击OK按钮即可创建运行脚本,如图1-11所示。

图1-11 创建运行脚本
注意
由于PyCharm自动为我们创建templates文件夹,并且在配置文件settings.py的TEMPLATES的DIRS添加templates文件夹的路径信息。从Django 3.1版本开始,配置文件settings.py的路径信息改用pathlib模块,Django 3.1之前版本使用os模块,因此PyChram添加templates文件夹的路径信息仍使用os模块。