后台数据
数据模型只有图书、英雄:
后台管理界面
后台管理界面--图书列表
后台管理界面--英雄列表
模板templates
在templates/booktest中添加模板文件booklist.html、herolist.html
booklist.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图书列表</title>
</head>
<body>
<ul>
{%for book in booklist%}
<!--添加超链接-->
<li><a href="/book_id={{book.id}}/">{{book.title}}</a></li>
{%endfor%}
</ul>
</body>
</html>
herolist.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>英雄列表</title>
</head>
<body>
<!--添加一个返回首页的超链接-->
<a href="/">首页</a>
<ul>
<!--循环函数显示英雄列表-->
{%for hero in herolist%}
<li>{{hero.name}}</li>
{%endfor%}
</ul>
</body>
</html>
视图views
views.py:
from django.shortcuts import render
from django.http import HttpResponse
from .models import *
# Create your views here.
def booklist(request):
list = BookInfo.objects.all()
context = {'booklist': list}
return render(request, 'booktest/booklist.html', context)
def herolist(request, id):
list = BookInfo.objects.get(id=id).heroinfo_set.all()
context = {'herolist': list}
return render(request, 'booktest/herolist.html', context)
url
在booktest/urls中配置url
from django.urls import re_path
from . import views
urlpatterns = [
re_path('^$', views.booklist),
re_path('^book_id=(\d+)/$', views.herolist),
]
后台不挂断运行服务器
- nohup: 不挂断
- &: 后台
- manage.log 2>&1:输入日志到manage.log
nohup python manage.py runserver 0.0.0.0:8000 >manage.log 2>&1 &
网友评论