如何在表单发布后在django中显示javascript警报?

时间:2013-11-26 09:46:12

标签: javascript python django

我想显示带有“找不到记录”

消息的javascript提醒

我该如何实施:

 if no_record_check==0:
        alert('No record found') // here
        return render('action')

2 个答案:

答案 0 :(得分:0)

从视图中传递no_record_check作为模板上下文变量。例如:

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

然后,在您的模板中:

<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>

答案 1 :(得分:0)

警报很烦人,而是显示错误消息,但您需要在上下文中传递no_record_check,以便稍后检查是否需要显示错误消息:

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

然后在模板action.html中(显示错误栏的引导方式):

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