我使用 django 和 MySQL 作为数据库来执行添加、编辑和删除详细信息。我使用下面的代码执行了添加和编辑,但删除不起作用。
视图.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))
网址.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。