Django如何在视图中完成方法后执行alert()(弹出消息) [英] Django How to implement alert()(popup message) after complete method in view

查看:4430
本文介绍了Django如何在视图中完成方法后执行alert()(弹出消息)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在view.py中有一个alert()消息(如javascript)after方法完成

I would like to have an alert() message (like in javascript) after method in view.py is complete

我的方法是

def change_password(request):
    dictData = getInitialVariable(request)

    in_username = request.POST['txt_username']
    in_password = request.POST['txt_password']
    in_new_password = request.POST['txt_new_password']


    user = authenticate(username=in_username, password=in_password)
    if user is not None:
        if user.is_active:
            u = User.objects.get(username=in_username)
            u.set_password(in_new_password)
            u.save()

            # Redirect to a success page.
            return HttpResponseRedirect('/profiles/'+in_username)

保存到数据库后弹出消息将显示。
我如何实现它?

After u is saved to database, the popup message will be shown. How could I implement it?

推荐答案

我认为最好的解决方案是消息( docs

I think the best solution would be messages (docs)

中所述消息级别文档 Django建议使用INFO级消息与用户进行通信。

As described in message levels docs Django suggests to use "INFO" level messages to communicate with users.

默认情况下,消息在Django中启用。如果我的示例不适合您,那么您应该检查启用邮件块

By default messages are enabled in Django. If my example doesn't work for you as it is you should check enable messages block

查看部分

from django.contrib import messages

def change_password(request):
   ...your stuff...

   messages.info(request, 'Your password has been changed successfully!')
   return HttpResponseRedirect('/profiles/'+in_username)

模板部分

{% if messages %}
<ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}

您可以在特定视图或一般模板中粘贴按摩输出(布局/标题)。

You can paste massage output in specific view or in general templates (layout/header).

这篇关于Django如何在视图中完成方法后执行alert()(弹出消息)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆