Visual Studio Community 2019 Django入门(二) —— hello world!
一>新建Django Web项目
第1步:创建新项目
选择Django Web项目
第2步:配置新项目信息
如“项目名”、“位置”等。以“hello”项目名为例创建
第3步:按指引创建超级用户(非必要)
二>修改项目文件
第1步:绑定端口号(非必要)
按自己需求修改,修改完成记得保存。
第2步:修改hello/app中的文件 view.py
添加如下内容:
from django.http import HttpResponse
# Create your views here.
def hello(request):
return HttpResponse("hello world!");
def test(request):
return HttpResponse("test")
第3步,在hello/app中新增加文件 urls.py
添加文件内容为:
from django.urls import path
from hello import views
urlpatterns = [path('hello/',views.hello,name='hello'),
path('test/',views.test,name='test')]
第4步,修改hello/hello文件夹下的文件 urls.py
在urlpatterns 添加内容
path('index', views.hello),
其中,index为打开页面的名称,即在浏览器窗口输入:http://localhost:2021/index,可打开index页面
第5步,访问,启动调试
在地址后面追加index回车,得到了我们要展示的内容hello world!
三>调试异常
1)引发异常:‘staticfiles’ is not a registered tag library.
django在前端模板中使用{% load staticfiles %},而在django3.x中这部分做了修改
整个项目查找 {% load staticfiles %} ,或者直接在hello\app\templates\app\layout.html 中修改。
{% load staticfiles %} 修改为 {% load static %}
修改保存后,运行调试即恢复正常。
*其他位置可能还有,建议查找全部后都修改。
四>总结
以互联网搬运工的身份,同时根据自己的实践历程。以此作为记录,供后续参考。
如有错误的地方还请斧正
此文参考:
1)https://www.cnblogs.com/dwBurning/p/Django2.html
2)https://www.django.cn/course/show-5.html