1. Create a model class:
from django.db import models
# Create your models here.
class BookInfo(models.Model):
'''图书模型类'''
title = models.CharField(max_length=20)
pub_date = models.DateField()
read = models.IntegerField(default=0)
class HeroInfo(models.Model):
# 关系属性,建立图书类和英雄类一对多关系
book = models.ForeignKey('BookInfo')
name = models.CharField(max_length=5)
# 性别采用布尔字段,默认Flase为男
gender = models.BooleanField(default=False)
comment = models.CharField(max_length=120)
2. Generate migrate files and perform the migration generated table:
python manage.py makemigrations
python manage.py migrate
3. By the operation model type data table:
python manage.py shell
Import the model class:
from booktest.models import BookInfo,HeroInfo
Data manipulation:
>>> b = BookInfo() # 定义一个BookInfo类的对象
>>> b.title='天龙八部' # 给title属性赋值
>>> from datetime import date
>>> b.pub_date = date(1990,10,11)
>>> b.save() # 执行这部才会将数据保存进数据库
>>> b2 = BookInfo()
>>> b2.title = '射雕英雄传'
>>> b2.pub_date = date(1991,1,1)
>>> b2.save()
>>> h = HeroInfo()
>>> h.name = '郭靖'
>>> h.gender = False
>>> h.comment = '主角,武功盖世'
>>> h.book = b2
>>> h.save()
>>> h2 = HeroInfo()
>>> h2.name = '乔峰'
>>> h2.gender = False
>>> h2.book = b
>>> h2.save()
>>> h2.comment = '男主角,厉害'
>>> h2.save
<bound method Model.save of <HeroInfo: HeroInfo object>>
>>> h2.save()
Finally, we can see that data has been added to the database
4. The view function is defined:
from django.shortcuts import render
from booktest import models
# Create your views here.
def show_books(request):
'''显示图书详细信息'''
# 1.从M中获取图书信息
books_info = models.BookInfo.objects.all() # 返回一个对象列表
# 2.返回数据
return render(request, 'booktest/show_books.html', {'books': books_info})
def detail(request, bid):
# 根据bid查找图书信息
book_info = models.BookInfo.objects.get(id=bid)
# 查询出book_info图书中所有的英雄人物信息
heros_info = book_info.heroinfo_set.all()
# 返回数据
return render(request, 'booktest/detail.html', {'book': book_info, 'heros': heros_info})
Note: The second line so I think from booktest.models import import BookInfo, but Django will complain can not find the models, so I directly into the models. This is because pycharm path problem, the follow-up to find a solution.
5. Configuration urls:
from django.conf.urls import url
from booktest import views
urlpatterns = [
url(r'^books$', views.show_books),
url(r'^books/(\d+)$', views.detail), # 利用正则表达式分组后,django会把分组的内容当做参数传进视图函数中
]
NOTE: This packet for regular expressions, such django will contents to the view function, where this parameter is passed to the bid
6. Create a template folder, define books.html and detail.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>{{ book.title }}</h1>
英雄信息如下:<br>
<ul>
{% for hero in heros %}
<li>{{ hero.name }}---{{ hero.comment }}</li>
{% empty %}
<li>当heros这个列表为空时执行这条语句</li>
{% endfor %}
</ul>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul>
<!-- 这里返回的是对象,要想得到标题就 对象.标题 -->
{% for i in books %}
<li><a href="/books/{{ i.id }}">{{ i.title }}</a> </li>
{% endfor %}
</ul>
</body>
</html>
Summary: When a browser requests a page from Django web server, the first project will be to urls to match, and then to match urls applications, perform the appropriate view function after a successful match, call the template file in the view function Finally, return to the browser to display the content.