配置路由urls
1.创建APP
在终端输入python manage.py startapp app名称
2.添加APP关联
3.在APP文件夹下新建urls.py
4. 写入代码
from django.urls import path
urlpatterns = [
(后续会填写)
]
展示html页面
1.配置路由路径
from django.urls import path
from . import views
urlpatterns = [
path('',views.home,name="home")
]
2.在views中编写home方法
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request,'home.html',{})
3.在APP目录下新建templates文件夹,在templates下新建home.html
创建公共html模板
1.在templates新建base.html,将复制好的代码粘贴上去
2.引入base.html
3.打包代码段
展示导航信息
在base.html中填写代码
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
接口介绍
jsonplaceholder
github
接口请求和解析
1.安装requests pip install requests
2.接口请求、解析
from django.shortcuts import render
# Create your views here.
def home(request):
import requests
import json
#接口请求
api_request = requests.get("https://api.github.com/users?since=0")
#接口解析
api = json.loads(api_request.content)
return render(request,'home.html',{"api":api})
3.在页面中展示