简体   繁体   English

密码未保存到Django中的auth_user表

[英]Password not saving to auth_user table in Django

Below is the code for the user registration. 以下是用户注册的代码。 The password value is not saving to the auth_users table. 密码值未保存到auth_users表中。 I am using MySQL as my database. 我正在使用MySQL作为数据库。

Any help is highly appreciated TIA 任何帮助都受到TIA的高度赞赏

form.py form.py

class MyRegistrationForm(UserCreationForm):
    email=forms.EmailField(required=True)
    class Meta:
        model=User
        fields = ('username','email','password1','password2')

    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.email=self.cleaned_data["email"]
        if commit:
            user.save()
        return user

view.py view.py

def register_user(request):
    if request.method =='POST':
        print request.POST['username']
        print request.POST['password1']
        print request.POST['password2']
        form=MyRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/register-success/')
    args={}
    args.update(csrf(request))
    args['form']=MyRegistrationForm()
    return render_to_response("register.html",args,
                              context_instance=RequestContext(request))

HTML HTML

  <form method="post" class="form-signin" action="/register/">{% csrf_token %}
    <h2 class="form-signin-heading">Please sign in</h2>
     <label for="users" class="sr-only">UserName</label>
    <input name="username" type="text" id="users" class="form-control" placeholder="Username" required autofocus>
    <label for="inputEmail" class="sr-only">Email address</label>
    <input name="email" type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
    <label for="inputPassword" class="sr-only">Password</label>
    <input name="password1" type="password" id="inputPassword" class="form-control"  placeholder="Password" required>
    <label for="inputPassword1" class="sr-only">Password</label>
    <input name="password2" type="password" id="inputPassword1" class="form-control"  placeholder="Password" required>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
  </form>

我想指出的是,您的def regiseter_user(request):视图函数具有多余的“ e”(regisEter)。

You are calling super() incorrectly. 您正在错误地调用super() You need to use the form class MyRegistrationForm , so that the save method of UserCreationForm is called and sets the password. 您需要使用表单类MyRegistrationForm ,以便调用UserCreationForm的save方法并设置密码。

    user = super(MyRegistrationForm, self).save(commit=False)

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

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