gpt4 book ai didi

Django 如何在 View 中完成方法后实现警报()(弹出消息)

转载 作者:行者123 更新时间:2023-12-03 15:22:07 26 4
gpt4 key购买 nike

我想在 view.py 中的方法完成后有一个 alert() 消息(就像在 javascript 中一样)

我的方法是

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)

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

最佳答案

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

message levels docs 中所述Django 建议使用“INFO”级别的消息与用户交流。

默认情况下,消息在 Django 中启用。如果我的例子不适合你,你应该检查 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 %}

您可以在特定 View 或通用模板(布局/标题)中粘贴按摩输出。

关于Django 如何在 View 中完成方法后实现警报()(弹出消息),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28240746/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com