[Answered ]-Subscription form with Django and jQuery

2👍

Your form errors are not shown because the form is only displayed when the user has clicked a button.

<script>
    $(document).ready(function(){
        $("#myBtn").click(function(){
            // You need to be triggering this when there was an error
            $("#myModal").modal();
        });
    });
</script>

A simple patch fix would be to pass an extra context variable to the template. Something like registration_failed = True or False.

Then modify your code like so:

# in views.py
else:
    print user_form.errors, profile_form.errors
    return render_to_response(
        'tentagarden/home.html',
        {'user_form': user_form, 'profile_form': profile_form, 'registered': registered, 'registration_failed': True},
        context)

And:

<!-- in your HTML -->
<script>
    $(document).ready(function(){
        $("#myBtn").click(function(){
            $("#myModal").modal();
        });
        // If the template is rendered with registration_failed == true
        // Then send a click to the button controlling the modal.
        if ({{registration_failed}}) {
            $("#myBtn").click();
        }
    });
</script>

Please note that this is not the best long-term solution to your problem, I think you need to refactor your code a lot. But this will fix the immediate issue.

Leave a comment