Django 删除单行数据

avatar
作者
筋斗云
阅读量:6

1,添加模型

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 delete_book1(request):     one_data = Book.objects.get(id=21)     one_data.delete()      return HttpResponse("id为18的数据删除成功!") 

3,添加路由地址

Test/app11/urls.py

from django.urls import path from . import views  urlpatterns = [        path('delete_book1/', views.delete_book1, name='delete_book1'), ]

4,访问页面

127.0.0.1:8000/app11/delete_book1/

 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!