2012-07-09 55 views
2

一旦用戶退出該站點,它應該重定向到主頁,並在頁面頂部顯示「U已成功註銷」消息。任何人都可以幫助我在主頁上顯示信息?註銷時的django消息

+0

嘗試改變' settings.MESSAGE_STORAGE'設置爲''django.contrib.messages.storage.cookie.CookieStorage'',這有幫助嗎? – Tony 2012-07-09 11:27:10

+0

幫我1個例子 – Raji 2012-07-09 11:39:01

回答

1

嘗試使用會話。可以更簡單。

在註銷視圖中,在會話變量中設置一個條目,如session['just_logged_out'] = True,並在主頁視圖中檢查變量。

try: 
    just_logged_out = request.session.get('just_logged_out',False) 
except: 
    just_logged_out = False 

在模板中,你可以使用

{% if just_logged_out %} You are successfully logged out {% endif %} 
+0

感謝U sid。它的工作很好 – Raji 2012-07-09 12:54:24

2
+0

我在後保存後嘗試了代碼messages.add_message(request,messages.INFO,'Hello world。')。但消息不顯示 – Raji 2012-07-09 11:20:01

+0

您需要添加代碼才能在模板中顯示消息。閱讀我發佈的鏈接中的文檔。 – 2012-07-09 11:22:14

6

你可以使用與信息框架結合user_logged_out信號:

首先,確保消息的框架設置(https://docs.djangoproject.com/en/dev/ref/contrib/messages/) 。

然後包括該代碼的地方,將被稱爲(我傾向於把它放在receivers.py模塊中,然後從一個models.py文件中導入在已安裝的應用程序):

from django.contrib.auth.signals import user_logged_out 
from django.dispatch import receiver 
from django.contrib import messages 


@receiver(user_logged_out) 
def on_user_logged_out(sender, request, **kwargs): 
    messages.add_message(request, messages.INFO, 'Logged out.')