使用django-email-as-username如何向用戶返回消息?

[英]using django-email-as-username how can I return with a message to the user?


I am trying to use django-email-as-username and I can't find a way to return to the login page with a message for the user in case the data provided by the user is wrong.

我正在嘗試使用django-email-as-username,如果用戶提供的數據錯誤,我找不到返回登錄頁面的方法,其中包含用戶的消息。

this my urls.py

這是我的urls.py

url(r'^login$', 'django.contrib.auth.views.login', {'authentication_form': EmailAuthenticationForm}, name='login'),

this is my views.py

這是我的views.py

def login_user(request):  
    if request.method == 'POST':
        email = request.POST.get('email')
        password = request.POST.get('password')
        if email and password:
            user = authenticate(email=email, password=password)
            if user:
                return HttpResponseRedirect('/')
            else:
                message = 'No such user'
        else:
            message = 'both email and password are required!'
    else:
        message = 'wrong method!'

2 个解决方案

#1


1  

I think you've made an error in that you're providing your own login_user view in your views.py module, but linking to Django's django.contrib.auth.views.login in your URL conf.

我認為您在views.py模塊中提供了自己的login_user視圖,但在URL conf中鏈接到Django的django.contrib.auth.views.login時出錯了。

You should probably just use django.contrib.auth.views.login as the login view.

您應該只使用django.contrib.auth.views.login作為登錄視圖。

You'll also need to provide a registration/login.html template. You'll be able to get at the error information, as the form is passed through to the context. You can use the admin's login template as an example.

您還需要提供registration / login.html模板。當表單傳遞給上下文時,您將能夠獲取錯誤信息。您可以使用admin的登錄模板作為示例。

#2


0  

In case of authentication failure, you have to render/redirect your own login page with {{ message }}

如果身份驗證失敗,您必須使用{{message}}呈現/重定向您自己的登錄頁面


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/02/19/72a2337c184509d6d8a40f216e236efe.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com