Web框架开发-开发图书管理页面

一、项目需求

    1.列出图书列表、出版社列表、作者列表
    2.点击作者,会列出其出版的图书列表
    3.点击出版社,会列出旗下图书列表
    4.可以创建、修改、删除 图书、作者、出版社
    
二、项目实现
bookms
|-- app01  # 项目应用
|   |-- views.py  # 视图层代码
|   |-- admin.py
|   |-- apps.py
|   |-- models.py # 模型层,定义数据库模型
|   |-- tests.py
|
|-- bookms  # 项目工程
|   |-- settings.py     # 项目的配置文件
|   |-- urls.py     # 路由层
|   |-- wsgi.py
|
|-- templates   # 项目模板
|   |-- addauthor.html  # 添加作者的模板
|   |-- addbook.html    # 添加图书的模板
|   |-- addpublish.html     # 添加出版社的模板
|   |-- author.html     # 作者的列表
|   |-- base.html       # 基础框架模板  
|   |-- books.html      # 图书的列表    
|   |-- changebook.html     # 编辑图书的模板    
|   |-- editauthor.html     # 编辑作者的模板    
|   |-- editpublish.html    # 编辑出版社的模板  
|   |-- index.html      # 登录首页  
|   |-- publish.html    # 出版社的列表  
|   |-- reg.html        # 注册页面  
|   |-- reg_succes.html     # 注册成功的页面
|
|-- manage.py   # 项目启动相关的

### 三、数据库设计
    class Book(models.Model):   # 必须要继承的
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=32)
    publishData = models.DateField()    # 出版日期
    authorlist = models.ManyToManyField(to="Author")
    price = models.DecimalField(max_digits=5, decimal_places=2)     # 一共5位,保留两位小数
    # 不用命名为publish_id,因为django为我们自动就加上了_id
    publish = models.ForeignKey(to="Publish", to_field="id", on_delete=models.CASCADE)

    def __str__(self):
        return self.title


    class Publish(models.Model):
        # 不写id的时候数据库会自动增加
        name = models.CharField(max_length=32)
        addr = models.CharField(max_length=32)
        email = models.EmailField()
    
        def __str__(self):
            return self.name


    class Author(models.Model):
        name = models.CharField(max_length=32)
        age = models.IntegerField()
        # 与AuthorDetail建立一对一的关系
    
        def __str__(self):
            return self.name
    

###  四、操作步骤
    1、先注册用户
    2、用注册的用户进行登录
    3、创建作者
    4、创建出版社
    5、新建图书信息
    6、点击作者姓名,跳转到该作者出版的图书列表
    7、点击出版社名称,跳转到该出版社出版的图书列表

五、实现效果
1、登录页面

  2、注册页面

  3、图书列表页面

  4、作者页面

  5、出版社页面

六、项目代码

views.py

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

from django.contrib import auth

from django.contrib.auth.decorators import login_required

from django.contrib.auth.models import User

from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage

from django.http import HttpResponse, HttpResponseRedirect

from django.shortcuts import render, redirect

# Create your views here.

from app01 import models

def index(request):

    return render(request, "index.html")

# 注册

def reg(request):

    if request.method == "POST":

        username = request.POST.get("username")

        password = request.POST.get("password")

        password1 = request.POST.get("password1")

        print(username, password, password1)

        if username != str(User.objects.filter(username=username).first()) and len(username) != 0:

            s = "注册成功"

            if password == password1 and len(password) != 0:  # 当密码与确认密码一致的时候,注册成功

                User.objects.create_user(username=username, password=password)

                return render(request, "reg_succes.html", {"username": username, "s": s})

            elif len(password) == 0:

                return render(request, "reg.html", {"s3": "密码不能为空!"})

            else:

                s1 = "两次输入的密码不一致"

                return render(request, "reg.html", {"s1": s1})

        elif len(username) == 0:

            return render(request, "reg.html", {"s2": "用户名不能为空!"})

        else:

            mess = "用户名已经存在!"

            return render(request, "reg.html", {"mess": mess})

    return render(request, "reg.html")

def reg_succes(request):

    return render(request, "reg_succes.html")

# 登录

def login(request):

    if request.method == "POST":

        username = request.POST.get("username")

        password = request.POST.get("password")

        print(username, password)

        user = auth.authenticate(username=username, password=password)  # 验证用户名和密码

        if user is not None and user.is_active:

            #  如果认证成功,就让登录

            auth.login(request, user)

            request.session['user'] = username  # 将session信息记录到浏览器

            response = HttpResponseRedirect("/books/")

            return response

        elif user is None:

            return render(request, "index.html", {"s1": "用户名不存在!"})

        else:

            s = "用户名或密码错误"

            return render(request, "index.html", {"s": s})

    return render(request, "index.html")

@login_required

# 新增书籍

def addbook(request):

    publish_list = models.Publish.objects.all()  # 查询出所有的出版社对象

    author_list = models.Author.objects.all()

    if request.method == "POST":

        title = request.POST.get("title")

        date = request.POST.get("date")

        price = request.POST.get("price")

        publish_id = request.POST.get("publish_id")

        authors_id_list = request.POST.getlist("authors_id_list")

        if title != str(models.Book.objects.filter(title=title).first()) and len(title) !=0:

            book_obj = models.Book.objects.create(title=title, publish_id=publish_id, publishData=date, price=price)

            book_obj.authorlist.add(*authors_id_list)

            return redirect("/books")

        elif len(title) == 0:

            return render(request, "addbook.html", {"s": "书籍名称不能为空!", "publish_list": publish_list,

                                                    "author_list": author_list})

        else:

            return render(request, "addbook.html", {"s1": "书籍名称已经存在!", "publish_list": publish_list,

                                                    "author_list": author_list})

    return render(request, "addbook.html", {"publish_list": publish_list, "author_list": author_list})

# 查看图书列表

@login_required

def books(request, field_id=0, field_type='src'):

    '''

    图书列表有3种情况:

    点击查看图书列表(books)显示的的图书

    点击出版社(publishs)显示的图书

    点击作者(authors)显示的图书

    :param request:

    :param field_id

    :param field_type: /publishs /anthors

    :return:

    '''

    if field_type == 'publishs':

        book_list = models.Book.objects.filter(publish_id=field_id).all()

    elif field_type == 'authors':

        book_list = models.Book.objects.filter(authorlist__id=field_id).all()

    else:

        book_list = models.Book.objects.all()

    username = request.session.get('user')

    paginator = Paginator(book_list, 10)

    page = request.GET.get('page', 1)

    currentPage = int(page)

    try:

        book_list = paginator.page(page)

    except PageNotAnInteger:

        book_list = paginator.page(1)

    except EmptyPage:

        book_list = paginator.page(paginator.num_pages)

    return render(request, "books.html", {"user": username, "book_list": book_list, "paginator": paginator,

                                          "currentPage": currentPage})

# 编辑图书

@login_required

def changebook(request, id):

    edit_book_obj = models.Book.objects.filter(id=id).first()

    if request.method == "POST":

        title = request.POST.get("title")

        date = request.POST.get("date")

        price = request.POST.get("price")

        authors_id_list = request.POST.getlist("authors_id_list")

        publish_id = request.POST.get("publish_id")

        if len(title) != 0:

            models.Book.objects.filter(id=id).update(title=title, publishData=date, price=price, publish_id=publish_id)

            edit_book_obj.authorlist.set(authors_id_list)

            return redirect("/books")

        else:

            return render(request, "changebook.html", {"s": "书籍名称不能为空!"})

    publish_list = models.Publish.objects.all()

    author_list = models.Author.objects.all()

    return render(request, "changebook.html", {"edit_book_obj": edit_book_obj, "publish_list": publish_list,

                                               "author_list": author_list})

# 删除图书

@login_required

def delbook(request, id):

    models.Book.objects.filter(id=id).delete()

    return redirect("/books")

# 注销登录

@login_required

def logout(request):

    auth.logout(request)

    return redirect("/index")

@login_required

# 添加作者

def addauthor(request):

    if request.method == "POST":

        name = request.POST.get("name")

        age = request.POST.get("age")

        if name != (models.Author.objects.filter(name=name).first()) and len(name) != 0:

            models.Author.objects.create(name=name, age=age)

            return redirect("/authors/")

        elif len(name) == 0:

            return render(request, "addauthor.html", {"s": "作者姓名不能为空!"})

    return render(request, "addauthor.html")

# 编辑作者

def editauthor(request, id):

    author_obj = models.Author.objects.filter(id=id).first()

    if request.method == "POST":

        name = request.POST.get("name")

        age = request.POST.get("age")

        if name != (models.Author.objects.filter(name=name).first()) and len(name) != 0:

            models.Author.objects.filter(id=id).update(name=name, age=age)

            return redirect("/authors/")

        elif len(name) == 0:

            return render(request, "addauthor.html", {"s": "作者姓名不能为空!"})

    return render(request, "editauthor.html", {"author_obj": author_obj})

# 删除作者

def delauthor(request, id):

    models.Author.objects.filter(id=id).delete()

    return redirect("/authors/")

@login_required

def authors(request):

    author_list = models.Author.objects.all()

    return render(request, "author.html", locals())

@login_required

# 添加出版社

def addpublish(request):

    if request.method == "POST":

        name = request.POST.get("name")

        addr = request.POST.get("addr")

        email = request.POST.get("email")

        if name != (models.Publish.objects.filter(name=name).first()) and len(name) != 0:

            models.Publish.objects.create(name=name, addr=addr, email=email)

            return redirect("/publishs/")

        elif len(name) == 0:

            return render(request, "addpublish.html", {"s": "出版社名称不能为空!"})

        else:

            return render(request, "addpublish.html", {"s1": "出版社名称已经存在!"})

    return render(request, "addpublish.html")

# 查看出版社

@login_required

def publishs(request):

    pub_list = models.Publish.objects.all()

    return render(request, "publish.html", locals())

# 编辑出版社

def editpublish(request, id):

    pub_obj = models.Publish.objects.filter(id=id).first()

    if request.method == "POST":

        name = request.POST.get("name")

        addr = request.POST.get("addr")

        email = request.POST.get("email")

        if name != (models.Publish.objects.filter(name=name).first()) and len(name) != 0:

            models.Publish.objects.create(name=name, addr=addr, email=email)

            return redirect("/publishs/")

        elif len(name) == 0:

            return render(request, "editpublish.html", {"s": "出版社名称不能为空!"})

        else:

            return render(request, "editpublish.html", {"s1": "出版社名称已经存在!"})

    return render(request, "editpublish.html", {"pub_obj": pub_obj})

# 删除出版社

def delpublish(request, id):

    models.Publish.objects.filter(id=id).delete()

    return redirect("/publishs/")

  

models.py

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

from django.db import models

# Create your models here.

class Book(models.Model):   # 必须要继承的

    id = models.AutoField(primary_key=True)

    title = models.CharFie

  • 22
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值