繁体   English   中英

Django:无法使用新用户登录

[英]Django: unable to login with new users

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

我还注意到它不会对密码进行哈希处理。 它以明文形式存储。

我曾尝试在admin和用户表单中创建它,但两者均无法正常工作。

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

您应该使用以下内容转换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)

它将散列用户密码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM