这是我用来预先填充update
的{{1}}视图。虽然这也行不通。
form
这是我的def update(request, id):
item = get_object_or_404(BookEntry, id=id)
if request.method=="POST":
form = UpdateForm(request.POST, instance=item)
if form.is_valid():
post=form.save(commit=False)
post.save()
return HttpResponseRedirect(reverse('bms:index'), id)
else:
form=EntryForm(instance=item)
return HttpResponseRedirect(reverse('bms:index'), id)
return render(request, 'index.html', {'form':form})
urls.py
这是我一次又一次地犯的错误:
/ bms /的NoReverseMatch 使用参数'('',)'找不到'update'的反转。尝试了1种模式:[u'bms / update /(?P [0-9] +)/ $']
我不确定是否以正确的方式传递网址。这就是我正在做的事情:
url(r'^update/(?P<id>[0-9]+)/$', views.update, name='update'),
当我看到追溯时,它在上面一行和这一行显示出错了:
<form action="{% url 'bms:update' book.id %}" id="updateform" name="updateform" method="POST">
这是我的return render(request, "bms/index.html", context)
观点:
index
我的def index(request):
context = {}
book_detail = BookEntry.objects.all()
context.update({
'book_detail': book_detail
})
response = {"status": False, "errors": []}
if request.is_ajax():
id = request.POST['id']
csrf_token = request.POST['csrfmiddlewaretoken']
response = {}
response['status'] = False
book = BookEntry.objects.filter(id=id).first()
context = {
"book": book,
"csrf_token": csrf_token
}
template = render_to_string("bms/index.html", context)
response['template'] = template
response['status'] = True
return HttpResponse(json.dumps(response), content_type="applicaton/json")
return render(request, "bms/index.html", context)
:
index.html