詳細の追加、編集、削除を実行するためのデータベースとして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))
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を使用しています。