初学Django---图书英雄Django项目复盘

这个练习是看传智播客黑马的教学视频,跟着视频敲了一遍,然后试着复盘
结论是代码细节有些忘了,整体流程掌握不错

复盘思路

1.搭建虚拟运行环境:这个用pycharm可以很快完成,一个项目就是一个环境
2.安装Django,1.8.2 pip3 install django==1.8.2
3.创建项目, django-admin startproject xxxx
4.创建APP, python manage.py startapp test1
测试运行 python manage.py runserver
5.设计模型类
书籍:
btitle
bpub_date
英雄:
hname
hcontent
hgender
hbook 外键指向书籍
6.在setting中加入APP,test1
7.迁移备份 python manage.py makemigrations
8.迁移 python manage.py migrate
9.创建后台管理员账户 python manage.py createsuperuser
10.将model在admin.py中注册
注意 admin.site.register()
11.设计template模板
主页模板(书籍列表)
书籍内容(英雄列表)
英雄详细信息
12.在setting中添加模板路径
13.设计视图,映射模板
14.测试完成


再来一遍

1,搭建虚拟运行环境
pycharm创建项目j02

2.安装Django1.8.2
这里写图片描述

3.创建项目
这里写图片描述

这里写图片描述

4.创建APP

这里写图片描述

这里写图片描述

+++++++测试一下++++++++++

扫描二维码关注公众号,回复: 2589368 查看本文章
python manage.py runserver

这里写图片描述

5.创建模型类

这里写图片描述

6.修改配置信息

这里写图片描述

这里写图片描述

7.迁移备份

这里写图片描述

8.迁移

(venv) C:\Users\Administrator\Desktop\j02\django02>python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: messages, staticfiles
  Apply all migrations: sessions, contenttypes, bookherotest, auth, admin
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying bookherotest.0001_initial... OK
  Applying sessions.0001_initial... OK

9.创建后台管理员账户

(venv) C:\Users\Administrator\Desktop\j02\django02>python manage.py createsuperuser
Username (leave blank to use 'administrator'): admin
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.

10.注册模型类到后台Admin.py中

这里写图片描述

这里写图片描述

这里写图片描述

++添加部分书籍英雄信息到数据库

略,后台添加

这里写图片描述

这里写图片描述

11.编写模板

这里写图片描述

这里写图片描述
模板的内容
这里写图片描述
这里写图片描述
这里写图片描述

12.设计URL

这里写图片描述
这里应为r’^book/’
这里写图片描述

13.设计视图

from django.shortcuts import render
from .models import *
# Create your views here.

def index(request):
    books = BookInfo.objects.all()
    content = {'books':books}
    return render(request,'index.html',content)

def bookinfo(request,book_id):
    book = BookInfo.objects.get(pk=book_id)
    heros = book.heroinfo_set.all()
    content = {'id':book_id,
               'heros':heros}
    return render(request,'bookinfo.html',content)

def heroinfo(request,hero_id):
    hero = HeroInfo.objects.get(pk=hero_id)#失误写错的地方,谨记
    content ={'hero':hero,
              'id':hero_id}
    return render(request,'heroinfo.html',content)

在写视图时因为失误在heroinfo中写成了bookinfo
因为失误,一直以为URL正则的问题,找了一圈,没问题,后发现,谨记

测试完成

这里写图片描述

这里写图片描述

这里写图片描述


复盘的收获还是很大的,这个小项目做了三次,流程已搞清楚,具体细节待后续学习,再接再厉

猜你喜欢

转载自blog.csdn.net/q1694222672/article/details/81432013