Ensure transient cover_url
field value gets persisted across form renders
Given this field doesn't map to an `Edition` model field it lost its values when re-rendering the form. It worked only when the form was valid and rendered as part of the confirmation screen, which is due to the context data value being set in `add_authors` which was only getting called after the form validation. I've opted to pull it out into a separate new function that gets called before form validation.
This commit is contained in:
parent
2bd94b1332
commit
1c6548a0ad
2 changed files with 18 additions and 1 deletions
|
@ -43,6 +43,7 @@ class EditBook(View):
|
|||
form = forms.EditionForm(request.POST, request.FILES, instance=book)
|
||||
|
||||
data = {"book": book, "form": form}
|
||||
ensure_transient_values_persist(request, data)
|
||||
if not form.is_valid():
|
||||
return TemplateResponse(request, "book/edit/edit_book.html", data)
|
||||
|
||||
|
@ -101,6 +102,8 @@ class CreateBook(View):
|
|||
"authors": authors,
|
||||
}
|
||||
|
||||
ensure_transient_values_persist(request, data)
|
||||
|
||||
if not form.is_valid():
|
||||
return TemplateResponse(request, "book/edit/edit_book.html", data)
|
||||
|
||||
|
@ -136,6 +139,11 @@ class CreateBook(View):
|
|||
return redirect(f"/book/{book.id}")
|
||||
|
||||
|
||||
def ensure_transient_values_persist(request, data):
|
||||
"""ensure that values of transient form fields persist when re-rendering the form"""
|
||||
data["cover_url"] = request.POST.get("cover-url")
|
||||
|
||||
|
||||
def add_authors(request, data):
|
||||
"""helper for adding authors"""
|
||||
add_author = [author for author in request.POST.getlist("add_author") if author]
|
||||
|
@ -150,7 +158,6 @@ def add_authors(request, data):
|
|||
data["confirm_mode"] = True
|
||||
# this isn't preserved because it isn't part of the form obj
|
||||
data["remove_authors"] = request.POST.getlist("remove_authors")
|
||||
data["cover_url"] = request.POST.get("cover-url")
|
||||
|
||||
for author in add_author:
|
||||
# filter out empty author fields
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue