2017-10-31 7 views
1

私は、ユーザーの電子メールを使ってウェブサイトにログインしたいと思います。メールでDjangoにログイン

は私が持っている: 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) 

、私の知る限り理解し、DBへの移行後、顧客ユーザモデルを作成することは難しいでしょう。

このコードを使用してログインするか、ユーザーのモデルを変更する必要がありますか?

p.s.それはstandartのログインユーザーのモデルではありませんが、standartのauth.logoutを使用してログアウトできますか?

のURL:

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コード:

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

URL 0.8000 /ログインでサーバを実行した後、すべてのプログラムがユーザー= Noneを返してみてくださいIndex.htmlにリダイレクトします。

私はすでにログインしているので、間違いがあると思いましたが、今は分かりません。また、Main.htmlページからログアウトしようとすると、action = "/ logout"を使ってリターンします(ページが見つかりません)。

+2

のようなカスタム 'User'モデルを使わないでください。パスワードは暗号化されません。 [Django docs](https://docs.djangoproject.com/ja/1.11/topics/auth/customizing/#substituting-a-custom-user-model)では、カスタムモデルを正しく使用する方法について説明しています。 – Alasdair

答えて

0

Djangoはデフォルトで認証システムを提供しています。これは、ログインCookieを作成する認証機能、ハッシングパスワードを提供するなど、これに関するすべてのことを管理します。私のアドバイスは実際のものを使用していますが、何かを変更する必要がある場合は、それを拡張する方法がたくさんあります。ここで私はあなたにcomplete tutorial to do thatを残しますが、これはis my recommendation to use the email as a login

関連する問題