Skip to content
Snippets Groups Projects
Commit 79470600 authored by Art's avatar Art :lizard:
Browse files

Template tag bulma_form ensures that forms are only submitted once.

parent 3dbb95f6
No related branches found
No related tags found
No related merge requests found
$('[data-submit-only-once="true"]').submit(function(event){
$(this).find("button").addClass("is-loading");
$(this).find(":submit").attr("disabled","disabled");
});
......@@ -103,11 +103,12 @@ class BulmaFieldMarkup(object):
return cls.div_control(content)
def render_form_generics(context, rendered_fields, rendered_errors, submit_text, submit_class):
def render_form_generics(context, rendered_fields, rendered_errors, submit_text, submit_class, submit_only_once=True):
csrf_field = defaulttags.CsrfTokenNode().render(context)
submit_only_once = "true" if submit_only_once else "false"
return format_html(
"""
<form method="post">
<form method="post" data-submit-only-once="{submit_only_once}">
{rendered_errors}
{rendered_fields}
{csrf_field}
......
$('[data-submit-only-once="true"]').submit(function(event){
$(this).find("button").addClass("is-loading");
$(this).find(":submit").attr("disabled","disabled");
});
$('[data-submit-only-once="true"]').submit(function(event){$(this).find("button").addClass("is-loading");$(this).find(":submit").attr("disabled","disabled");});
\ No newline at end of file
......@@ -30,8 +30,10 @@
{% block script %}
{% if debug %}
<script type="text/javascript" src="{% static 'hshassets/js/jquery-3.2.1.js' %}"></script>
<script type="text/javascript" src="{% static 'hshassets/js/common.js' %}"></script>
{% else %}
<script type="text/javascript" src="{% static 'hshassets/js/jquery-3.2.1.min.js' %}"></script>
<script type="text/javascript" src="{% static 'hshassets/js/common.min.js' %}"></script>
{% endif %}
{% endblock %}
{% block post-script %}{% endblock %}
......
......@@ -43,13 +43,13 @@ def bulma_form_errors(form):
@register.simple_tag(takes_context=True)
def bulma_form(context, form, submit_text="OK", submit_class="button is-outlined"):
def bulma_form(context, form, submit_text="OK", submit_class="button is-outlined", submit_only_once=True):
"""Renders whole form, including errors, csrf and a submit button."""
fields = bulma_form_fields(form)
errors = bulma_form_errors(form)
return render_form_generics(context, fields, errors, submit_text, submit_class)
return render_form_generics(context, fields, errors, submit_text, submit_class, submit_only_once)
def render_layout(elements, form):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment