2015-12-07 93 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) 

它会散列用户密码。