Django:无法使用新用户登录

时间:2015-12-07 12:16:51

标签: python django

当我创建新用户时,我无法登录。它仅适用于运行“createsuperuser”命令时设置的用户。

我也注意到它没有哈希密码。它以明文形式存储。

我尝试在管理员和我的用户表单中创建它,但两者都不起作用。

的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 个答案:

答案 0 :(得分: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)

它会哈希用户密码。