登录完成后如何输出?Django的 [英] How can you output something when login is completed? Django

查看:60
本文介绍了登录完成后如何输出?Django的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个表单,当单击登录按钮时,它将显示登录成功消息.这是我要的,当登录"单击按钮,用户将被重定向,并且在重定向的页面(即主页)中,它应显示该消息.您如何在Django中做到这一点?

I'm trying to build a form that when the login button is clicked, it displays a login succesful message. Here is the thing, I want that when the "login" button is clicked, the user gets redirected and in the redirected page (which is the home page), it should show the message. How can you do this in Django?

我尝试做:

{% if request.user.is_authenticated %}

但是此代码的问题在于,即使您重新加载页面,该消息也会每次出现.

But the problem with this code is that the message appears each time, even when you reload the page.

推荐答案

一种解决方法是使用成功消息.

One way to approach this could be to use the success message.

from django.contrib import messages

def my_view(request):
   ...
   if form.is_valid():
      ....
      messages.success(request, 'Success message.')

这篇关于登录完成后如何输出?Django的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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