django基础二——搭建简单的页面

接上文:按上文中目录及工程名称举例。工程名:day18,APP名:user

1.在templates下创建一个html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>练习</title>
</head>
<body>
<a href="https://www.baidu.com">baidu</a>

</body>
</html>

2.在user/views.py中写逻辑

from django.shortcuts import render,HttpResponse
def index(request):
    f=open(r'E:lianxiday18	emplatesindex.html')
    result=f.read()
    return HttpResponse(result)

3.把写好的方法加到day18/urls.py的urlpatterns这个list中

from day18.user import views      #这里要先引入views

urlpatterns = [
    path('admin/', admin.site.urls),#默认带的
    path('index/',views.index)   #注意这里views.index只写方法名
]

4.写完后请求启动服务,在工程目录下(day18):python manage.py runserver启动服务,然后访问127.0.0.1:8000/index  就可以访问对应的html。

总的来说,页面请求的逻辑写在views里,templates下放html文件,urls文件中配置

原文地址:https://www.cnblogs.com/hancece/p/11695275.html