def book_index(request):
#增加
# book = Book(name=‘西游记’,author=‘施’,price=100)
# book.save()
#查询数据
# book = Book.objects.filter(name=‘西游记’).first() #filter获取的是Queryset()对象,first取第一个值
# # book = Book.objects.get(id=1)
# print(“book===>”,book)
#删除数据
# book = Book.objects.get(id=4)
# book.delete()
#修改数据
book = Book.objects.get(pk=2)
book.price = 500
book.save()
return HttpResponse(‘图书添加成功’)
class Book(models.Model):
# name = models.CharField(max_length=50)
# pub_date = models.DateField()
# id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100,null=False)
author = models.CharField(max_length=100,null=False)
price = models.FloatField(null=False,default=0)
def str(self): #人类读的懂的模式显示
return “Book:({name},{author},{price})”.format(name=self.name,author=self.author,price=self.price)
views.py
path(‘book_index/’,views.book_index)