Adds form errors

This commit is contained in:
Mouse Reeve 2021-12-15 13:20:05 -08:00
parent dcf5694b66
commit c8e038cd4e
2 changed files with 7 additions and 3 deletions

View file

@ -14,16 +14,19 @@
<div class="field">
<label class="label" for="id_url">{% trans "Name:" %}</label>
{{ file_link_form.name }}
{% include 'snippets/form_errors.html' with errors_list=file_link_form.name.errors id="desc_name" %}
</div>
<div class="columns">
<div class="column is-four-fifths">
<label class="label" for="id_url">{% trans "URL:" %}</label>
<input type="url" name="url" maxlength="255" class="input" required="" id="id_url" value="{% firstof file_link_form.url.value "" %}" placeholder="https://...">
<input type="url" name="url" maxlength="255" class="input" required="" id="id_url" value="{% firstof file_link_form.url.value "" %}" placeholder="https://..." aria-describedby="desc_name">
{% include 'snippets/form_errors.html' with errors_list=file_link_form.url.errors id="desc_url" %}
</div>
<div class="column is-one-fifth">
<label class="label" for="id_filetype">{% trans "File type:" %}</label>
<input type="text" name="filetype" maxlength="5" class="input" required="" id="id_filetype" value="{% firstof file_link_form.filetype.value "" %}" placeholder="PDF">
{% include 'snippets/form_errors.html' with errors_list=file_link_form.filetype.errors id="desc_filetype" %}
</div>
</div>

View file

@ -28,10 +28,11 @@ class FileLink(View):
return TemplateResponse(request, "book/file_link_page.html", data)
@transaction.atomic
def post(self, request, book_id):
def post(self, request, book_id, link_id=None):
"""Add a link to a copy of the book you can read"""
book = get_object_or_404(models.Book.objects.select_subclasses(), id=book_id)
form = forms.FileLinkForm(request.POST)
link = get_object_or_404(models.FileLink, id=link_id) if link_id else None
form = forms.FileLinkForm(request.POST, instance=link)
if not form.is_valid():
data = {"file_link_form": form, "book": book}
return TemplateResponse(request, "book/file_link_page.html", data)