自学内容网 自学内容网

Django 新增数据 save()方法

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_book(request):
    new_book = Book(
        title="Python 编程",
        author="John Doe",
        publication_date="2023-01-01",
        price=99.99
    )
    new_book.save()
    return HttpResponse("Book added successfully!")

 

 3,添加路由

Test/app11/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('add_book/', views.add_book, name='add_book'),
]

 

4,访问页面

127.0.0.1:8000/app11/add_book/


原文地址:https://blog.csdn.net/qq_26086231/article/details/140278710

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!