2015-12-07 91 views
1

當我新建一個用戶時,我無法登錄。它只適用於運行'createsuperuser'命令時設置的用戶。Django:無法用新用戶登錄

我也注意到它不會散列密碼。它以明文形式存儲。

我曾嘗試在管理員和我的用戶窗體中創建它,但都不起作用。

的login.html

<form class="m-t" role="form" method="post" 
    action="{% url 'django.contrib.auth.views.login' %}">{% csrf_token %} 

    <div class="form-group"> 
     <input id="id_username" name="username" type="text" class="form-control" placeholder="Username" 
         required=""> 
    </div> 
    <div class="form-group"> 
     <input id="id_password" name="password" type="password" 
      class="form-control" placeholder="Password" required=""> 
     </div> 

     <button type="submit" class="btn btn-primary block full-width m-b">Login</button> 
</form> 

views.py

class UserProfileCreate(CreateView): 
    model = UserProfile 
    form_class = UserProfileForm 
    queryset = UserProfile.objects.all() 
    success_url = '/sites/list' 

forms.py

class UserProfileForm(forms.ModelForm): 
    class Meta: 
     model = UserProfile 
     exclude = ('creation', 'last_modified') 

models.py

class UserProfile(AbstractUser): 
    company = models.ForeignKey(Company, blank=True, null=True) 
    site = models.ManyToManyField(Site, blank=True, null=True) 
+1

請顯示UserProfileForm的代碼。 –

+0

已添加UserProfileForm – Mantis

+0

您的用戶表單不正確地在數據庫中散列密碼。請參閱[文檔中的示例](https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#a-full-example)以獲取更多信息。 – Alasdair

回答

2

你應該改變你的UserProfileCreate類與以下內容:

class UserProfileCreate(CreateView): 
    model = UserProfile 
    form_class = UserProfileForm 
    queryset = UserProfile.objects.all() 
    success_url = '/sites/list' 
    def form_valid(self, form): 
     self.object = form.save() 
     self.object.set_password(self.object.password) 
     return super(UserProfileCreate, self).form_valid(form) 

它會散列用戶密碼。