[Answer]-How to display javascript alert in django after Form post?

0👍

Pass no_record_check as template context variable from the view. For example:

def get_context_data(self, **kwargs):
    context = super(YourViewClass, self).get_context_data(**kwargs)
    context['no_record_check'] = int(<your code>)
    return context

Then, in your template:

<script type="text/javascript">
var no_record_check = {{ no_record_check }};  // no_record_check **must** be an integer
if (no_record_check == 0) {
    alert('No record found');
}
</script>
👤jpic

1👍

Alerts are annoying show an error message instead, but you need to pass no_record_check in context to check later if you need to display the error message or not:

return render('action.html', {'no_record_check': no_record_check})

Then in template action.html (bootstrap way of displaying error bar):

{% if no_record_check == 0 %}
   <div class="alert alert-error">
        <strong>No Record Found</strong>
    </div>
{% endif %}

Leave a comment