gpt4 book ai didi

mysql - 使用 django 和 mysql db 删除表中数据的步骤

转载 作者:行者123 更新时间:2023-11-30 23:21:52 30 4
gpt4 key购买 nike

我使用 Django 和 MySQL 作为数据库来执行添加、编辑和删除细节。我使用下面的代码执行了添加和编辑,但删除不起作用。

views.py:

import logging
import smtplib
from django.db import connection
from django.shortcuts import render
from django.http import HttpResponse
from polls.models import Poll
from django.template import Context, loader
from django.shortcuts import get_object_or_404, render_to_response
from myapp.models import Book
from django.shortcuts import render_to_response,redirect
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from django.template import RequestContext

log = logging.getLogger(__name__)

def index(request):

books = Book.objects.all()
context={'books':books}
return render(request,'index.html', context)

def addbook(request):
if request.POST:
book_name =request.POST['book_name']
author_name =request.POST['author_name']
publisher_name =request.POST['publisher_name']
book = Book(book_name=book_name, author_name=author_name, publisher_name=publisher_name)
book.save()
return render_to_response('book_detail.html', {'books': book},context_instance=RequestContext(request))
else:
return render_to_response('addbook.html',context_instance=RequestContext(request))

def book_detail(request):
return render(request, 'book_detail.html')

def editbook(request,book_id):
if request.POST:
book_name =request.POST['book_name']
author_name =request.POST['author_name']
publisher_name =request.POST['publisher_name']
books=Book.objects.filter(book_id=book_id).update(book_name=book_name, author_name=author_name, publisher_name=publisher_name)
return redirect('/index/')
else:
books = Book.objects.get(pk=book_id)
return render_to_response('editbook.html',{'books':books},context_instance=RequestContext(request))

def deletebook(request,book_id):
if request.POST:
book_name =request.POST['book_name']
author_name =request.POST['author_name']
publisher_name =request.POST['publisher_name']
books=Book.objects.filter(book_id=book_id).delete(book_name=book_name, author_name=author_name, publisher_name=publisher_name)
return redirect('/index/')
else:
books = Book.objects.get(pk=book_id)
return render_to_response('deletebook.html',{'books':books},context_instance=RequestContext(request))

urls.py:

from django.conf.urls import patterns, include, url
from DemoApp.views import index,addbook,editbook, book_detail
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
url('^$', index),
url('^index/$', index),
url('^addbook/$', addbook),
url('^book_detail/$', book_detail, 'book_summary'),
url('^editbook/(?P<book_id>\d+)/$',editbook) ,
url('^deletebook/(?P<book_id>\d+)/$',deletebook) ,

#url(r'^admin/', include(admin.site.urls)),

)

HTML 模板:

<html>
<head>
<title>{{ page_title }}</title>
</head>
<body>
<div align="center">
<table border="0" cellpadding='5' cellspacing='5'>
<tr>
<form action="/deletebook/{{ books.book_id}}/" method="POST"> {% csrf_token %}
<input type="text" name="book_name" value="{{books.book_name}}"></input><br>
<input type="text" name="author_name" value="{{books.author_name}}"></input><br>
<input type="text" name="publisher_name" value="{{books.publisher_name}}"></input><br>
<input type="submit" value="delete">
</form>
<p> Output: {{ output }} </p>
<p>{{ book_name }}</p>
</tr>
</table>
</div>
</body>
</html>

我在这里没有使用任何形式。我无法对特定行执行删除 操作。它给出错误信息。请检查此代码段并用正确的代码回复我。

我的要求是我想从表中删除选定的行。我正在使用 MySQL。

最佳答案

我不太了解django,但是清空表的SQL命令是:

TRUNCATE table table_name;

它会将自动增量重新设置为 1,如果您不想更改该值,请使用:

Delete * from table_name;

如果你想删除特定的行

Delete from table_name where conditions1, condition2

关于mysql - 使用 django 和 mysql db 删除表中数据的步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15150986/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com