2016-02-23 161 views
1

我有一個django登錄問題。我使用django自己的登錄頁面,但問題是當我註冊一個新用戶時,用戶無法登錄。我嘗試了一切,但無法找到任何解決方案。我可以在我的database.DB中看到用戶名。任何想法解決這個問題?Django登錄無法正常工作

我登錄form.py:

class UserForm(forms.ModelForm): 
    password = forms.CharField(widget=forms.PasswordInput()) 
    confirm_password = forms.CharField(widget=forms.PasswordInput()) 

    class Meta: 
     model = User 
     fields = ('username', 'email') 
class UserProfileForm(forms.ModelForm): 
    class Meta: 
     model = UserProfile 
     fields = ('website', 'picture','user_type') 

錯誤說:請輸入正確的用戶名和密碼。兩個地方都要注意大小寫。

所以這意味着它不能找到我的用戶,即使我可以從數據庫中看到它們。

+0

你應該告訴你如何創建用戶。 –

回答

1

我覺得你的問題是,你不要在你的元類中有'密碼'。所以,你的元級應該是這樣的:

類元: 模型=用戶 欄=(「用戶名」,「電子郵件」,「密碼」)

+1

你是對的。我忘了那個 – chazefate

1

在Django中處理認證的常見錯誤是不正確地在你的視圖中處理登錄表單;即使用進行身份驗證登錄由Django正確提供的函數。這些是要求爲了正確認證用戶; 身份驗證創建密碼的哈希檢查對數據庫加密的密碼,並登錄設置爲登錄標誌着當前用戶的會話數據。

from django.contrib.auth import authenticate, login 

def my_view(request): 
    username = request.POST['username'] 
    password = request.POST['password'] 
    user = authenticate(username=username, password=password) 
    if user is not None: 
     if user.is_active: 
      login(request, user) 
      # Redirect to a success page. 
     else: 
      # Return a 'disabled account' error message 
      ... 
    else: 
     # Return an 'invalid login' error message. 

如果你覺得你是正確的在您的視圖中使用這些函數,然後請發佈您用於視圖的代碼。

0

您應該使用django.contrib.auth.views

例如:urls.py

from django.contrib.atuh import views as auth_views url(r'^login/' , auth_views.login , {'template_name' : 'path/to/template.html'} , name='login')

就是這樣,只要使用{{form}}變量path/to/template.html

DOCS

+0

感謝您的評論,但這不是問題。 – chazefate