1,添加模型
Test/app11/models.py
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
pub_date = models.DateTimeField('date published')
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
publication_date = models.DateField()
price = models.DecimalField(max_digits=5, decimal_places=2)
def __str__(self):
return self.title
2,添加视图函数
Test/app11/views.py
from django.http import HttpResponse
from app11.models import Book # 导入你的模型
def updata_book(request):
# 假设我们要更新标题为 "My Book Title" 的书籍的作者和价格
book = Book.objects.get(title="测试Django 编程") # 获取该书籍实例
# 更新书籍的作者和价格
book.author = "小红"
book.price = 88.88
# 保存更改
book.save()
return HttpResponse("Book updata successfully!")
3,路由地址
Test/app11/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('updata_book/', views.updata_book, name='updata_book'),
]
4,访问页面
http://127.0.0.1:8000/app11/updata_book/