1,添加模型
Test/app11/models.py
from django.db import models
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 add_book2(request):
# 使用 create 方法创建并保存 Book 实例
book = Book.objects.create(
title="Django 编程",
author="小强",
publication_date="2024-01-01",
price=99.99
)
print(book)
return HttpResponse("Book2 added successfully!")
3,添加路由
Test/app11/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('add_book2/', views.add_book2, name='add_book2'),
]