django弹出对话框_Django如何在视图中完成方法后执行alert()(弹出消息)

在Django视图change_password中,用户成功更改密码并保存到数据库后,想要实现一个弹出消息提示。解决方案是使用Django的消息功能(messages框架),设置消息级别为“INFO”,在视图中添加`messages.info(request, 'Your password has been changed successfully!')`,然后在模板中显示这些消息。" 126584966,15230567,DM数据库DMDSC集群安装与配置指南,"['数据库', '服务器', 'Linux']
摘要由CSDN通过智能技术生成

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

My method is

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?

解决方案

I think the best solution would be messages (docs)

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

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

View part:

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)

Template part:

{% if messages %}

{% for message in messages %}

{{ message }}

{% endfor %}

{% endif %}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值