简体   繁体   中英

Django login using email

I want to login on website, using user's email.

I have: 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.

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?

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

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:

 <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.

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).

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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