密码不保存到Django中的auth_user表

时间:2015-09-11 19:15:14

标签: django django-models django-admin

以下是用户注册的代码。密码值未保存到auth_users表。我使用MySQL作为我的数据库。

任何帮助都非常感谢TIA

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

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

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

2 个答案:

答案 0 :(得分:0)

我想指出你的def regiseter_user(请求):view函数有一个多余的“e”(regisEter)。

答案 1 :(得分:0)

您正在正确呼叫super()。您需要使用表单类MyRegistrationForm,以便调用UserCreationForm的保存方法并设置密码。

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