简体   繁体   English

使用电子邮件登录 Django

[英]Django login using email

I want to login on website, using user's email.我想使用用户的电子邮件登录网站。

I have: models.py我有:models.py

class User(models.Model):
    email = models.EmailField()
    password = models.CharField(max_length=200)
    client_id = models.IntegerField()
    role = models.CharField(max_length=200)

So, as far as I understand it will be hard to create Customer User model, after migrations to db.因此,据我所知,在迁移到 db 之后,很难创建 Customer User 模型。

Can I just use this code to login, or I need to change user's model?我可以使用此代码登录,还是需要更改用户模型?

Ps It's not standart login user's model, can I logout using standart auth.logout? Ps 这不是标准登录用户的模型,我可以使用标准 auth.logout 退出吗?

urls:网址:

url(r'^admin/', admin.site.urls),
url(r'^main/$', MainPage, name="main"),
url(r'^login/$', UserLogin, name="login"),

url(r'^$', HomeView, name="home"),
url(r'^logout/$', Logout, name="logout"),

views.py视图.py

def UserLogin(request):
    email = request.POST.get('email',False)
    password =  request.POST.get('password',False)
    user = authenticate(email=email,password=password)
    if user is not None:
        return HttpResponseRedirect('Main.html')
    else:
       return HttpResponseRedirect('Index.html')

def Logout(request):
    auth.logout(request)
    return render_to_response('Login.html')

Html code: html代码:

 <form class="m-t" role="form" action="/login" method="post">

     <div class="form-group">
         <input type="email" class="form-control" placeholder="Username"  required="">
     </div>
     <div class="form-group">
          <input type="password" class="form-control" placeholder="Password" required="">
     </div>
     <button type="submit" class="btn btn-primary block full-width m-b">Login</button>

     <a href="password.html">
         <small>Forgot password?</small>
     </a>
     <a class="btn btn-sm btn-white btn-block" href="reg.html">Create an account</a>
 </form>

After running server with url .8000/login, every try program returns user = None, and redirects to Index.html.使用 url .8000/login 运行服务器后,每个 try 程序都返回 user = None,并重定向到 Index.html。

I thought, that I have this mistake because I'm already logged in, but now I'm not sure.我想,我有这个错误,因为我已经登录了,但现在我不确定。 Also, When I try to logout from Main.html page, using action = "/logout" it retuns (Page not found).另外,当我尝试从 Main.html 页面注销时,使用 action = "/logout" 它会返回(找不到页面)。

Django provide a authentication system by default, this manage everything related with this, it includes an authentication function to create login cookies, provide hashing password and more. Django 默认提供一个身份验证系统,它管理与此相关的一切,它包括一个身份验证功能来创建登录 cookie,提供散列密码等等。 My advice is use the actual, if you need to modify something there are many ways to extend it.我的建议是使用实际的,如果你需要修改一些东西,有很多方法可以扩展它。 I leave you a complete tutorial to do that , but this is my recommendation to use the email as a login我给你留下了一个完整的教程,但这是我的建议,使用电子邮件作为登录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM