Проект cs50. Я закончил это. приведенный ниже код работает для функции редактирования
class EntryForm(forms.Form):
title = forms.CharField(label="Entry Title")
content = forms.CharField(widget=forms.Textarea(), label="Entry Content")
def edit(request, title=None):
if request.method == "POST":
form = EntryForm(request.POST)
if not title:
if form.is_valid():
title = form.cleaned_data['title']
content = form.cleaned_data['content']
else:
return render(request, "encyclopedia/edit.html", {
"form": form
})
elif title == form.data['title']:
title = form.data['title']
content = form.data['content']
util.save_entry(title, content)
return HttpResponseRedirect(reverse('wiki:greet', args=[title]))
if not title:
return render(request, "encyclopedia/edit.html", {
"form": EntryForm()
})
data = {'title': title, 'content': util.get_entry(title)}
populated_form = EntryForm(initial=data)
return render(request, "encyclopedia/edit.html", {
"title": title,
"form": populated_form,
"edit": True
})