第一篇:https://mp.weixin.qq.com/s/S1...第一个django应用——一个简单的helloworld项目先决条件python3django==2.2django安装请看:Django快速安装注:django吧更适合项目在虚拟环境下运行,但是我们这里只是学习helloworld。为了减少其他因素的影响,我们只使用普通的Python环境(无所谓,你也可以使用虚拟环境,如果你会的话)来创建主工程创建并打开cmd输入命令:django-adminstartprojecthello_world运行命令会创建一个hello_world文件夹,里面有一些文件注意:hello_world是项目名,你可以自定义,不过你可以像我一样验证一下你可以打开cmd进入hello_world文件夹,然后运行项目pythonmanage.pyrunserver通常会这样显示:August10,2020-10:11:10Djangoversion2.2,usingsettings'hello_world.settings'Startingdevelopmentserverathttp://127.0.0.1:8000/QuittheserverwithCTRL-BREAK.可以在浏览器中打开:http://127.0.0.1:8000/如下:如果显示正常,说明成功!!!新建子关卡项目打开cmd,进入新建的项目目录(hello_world)。注意这个目录下有一个manage.py文件。这个非常重要。输入命令:pythonmanage.pystartappmy_app运行命令后,会新增一些文件:注意:my_app是子级项目名,可以自定义,但是可以像我一样增加或修改一些文件.接下来,我们需要添加或修改一些文件来形成我们的helloworld项目。因为我们创建的项目路径不一定相同,所以我会使用相对路径修改hello_world\hello_world\settings.py,在列表INSTALLED_APPS(关联项目)中添加my_app,添加文件hello_world\my_app\urls.py从django.ur写入内容lsimportpath,includefrom.importviewsurlpatterns=[path('',views.home,name='home')]modifyhello_world\my_app\views.pyaddhomefunctionfromdjango.shortcutsimportrender,HttpResponse#在这里创建你的视图.defhome(request):returnHttpResponse("helloworld")修改hello_world\hello_world\urls.py,添加urlpattern入口,指向我们刚刚创建的my_app应用唯一的urls文件,这里需要导入include模块。fromdjango.contribimportadminfromdjango.urlsimportpath,includeurlpatterns=[path('admin/',admin.site.urls),path("",include("my_app.urls"))]修改到这里就结束了我们运行再次打开项目,在浏览器中查看pythonmanage.pyrunserver是否打开:http://127.0.0.1:8000/如果返回helloworld,说明成功。至此一个简单的helloworld项目就结束了,关注我获取更多内容还是比较简单的
