既存のデータを編集してデータベースに保存する方法を教えてもらえますか
def editbook(request,book_id):
if request.POST:
book = Book.objects.get(pk=book_id)
form = BookForm(request.POST,instance = book)
if form.is_valid():
form.save()
return redirect('/index/')
else:
book = Book.objects.get(pk = book_id)
form = BookForm(instance = book)
return render_to_response('editbook.html',{ 'form':form }, context_instance=RequestContext(request))
テンプレートは
<html>
<head>
<title>{{ page_title }}</title>
</head>
<body>
<div align="center">
<form action="/editbook/{{ form.book_id }}/" method="POST" class="BookForm"> {% csrf_token %}
<table>
<tr>
<td align="right">Book Name : </td>
<td align="left">{{ form.book_name }}
</tr>
<tr>
<td align="right">Author Name :</td>
<td align="left">{{ form.author_name }}</td>
</tr>
<tr>
<tr><td align="right">Publisher Name : </td>
<td align="left"> {{ form.publisher_name }}</td>
</tr>
</table>
<input type="submit" value="Save">
</form>
</div>
</body>
</html>
保存ボタンを押すと、データがデータベースに保存されません。ページが見つかりません(404)というエラーが表示されます。
urls.py は
from django.conf.urls import patterns, include, url
from DemoApp.views import index,addbook,editbook, book_detail,deletebook
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)),
)