跟着《 python 编程从入门到实践》练习 Django,遇到如下问题:创建了一个 Topic 表单,有一个 Entry 表单用外键链接到 Topic,在试图函数里定义了修改 Entry 的函数如下: def edit_entry(request, entry_id):
"""修改条目"""
entry = Entry.objects.get(id=entry_id)
topic = entry.topic
if request.method != 'POST':
form = EntryForm(instance=entry)
else:
form = EntryForm(instance=entry, data=request.POST.get('fields'))
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('xs:topic', args=[topic.id]))
context = {'topic': topic, 'form': form, 'entry': entry}
return render(request, 'xs/edit_entry.html', context)
现在我想修改 Topic,跟着上面的函数定义了下面这个函数:
def edit_topic(request, topic_id):
"""修改主题"""
topic = Topic.objects.get(id=topic_id)
if request.method != 'POST':
form = TopicForm(instance=topic)
else:
form = TopicForm(instance=topic, data=request.POST.get('fields'))
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('xs:topics'))
context = {'topic': topic, 'form': form}
return render(request, 'xs/edit_topic.html', context)
这就出问题了,第二个 if 语句(if form.is_valid())似乎没有执行,导致没有跳转至 topics.html 页面
1
helloworld12 2018-08-09 01:17:42 +08:00
多 print 打印 日志
|
2
georgema1982 2018-08-09 07:54:15 +08:00
我目测不是没执行,而是 form.is_valid()是 False,所以它又回到了 edit_topic.html
|
3
getlost OP @helloworld12 谢谢指教,form.is_valid()是 false
|
4
getlost OP @georgema1982 确实是这样,难道不允许修改嘛?
|