Django无法加密用户密码信息,也无法检索用户配置信息

时间:2015-08-24 07:35:14

标签: python django user-registration

您好我有一个Web应用程序,它具有用户注册和更新用户配置文件。我正在使用密码哈希。

这是我的models.py

class UserProfile(models.Model):

    user = models.OneToOneField(User)


    website = models.URLField(blank=True)
    picture = models.ImageField(upload_to='profile_images', blank=True)


    def __unicode__(self):
        return self.user.username

这是我的forms.py

class UserForm(forms.ModelForm):
    username = forms.CharField(
        max_length=254,
        widget=forms.TextInput(attrs={'class': "form-control input-lg",'placeholder': 'username'}),
    )
    email = forms.CharField(
        max_length=254,
        widget=forms.TextInput(attrs={'class': "form-control input-lg",'placeholder': 'email'}),
    )
    password = forms.CharField(widget=forms.PasswordInput(attrs={'class': "form-control input-lg",'placeholder': 'password'}))
    password2 = forms.CharField(widget=forms.PasswordInput(attrs={'class': "form-control input-lg",'placeholder': 'confirm password'}),label="Confirm password")

    class Meta:
        model = User
        fields = ('username', 'email', 'password','password2')

class UserProfileForm(forms.ModelForm):
    website = forms.URLField(widget=forms.TextInput(attrs={'class': "form-control input-lg",'placeholder': 'website'}))
   picture=form.Imagefield
    class Meta:
        model = UserProfile
        fields = ('website', 'picture')

这是我的views.py更新和注册

def register(request):


registered = False


if request.method == 'POST':

    user_form = UserForm(data=request.POST)
    profile_form = UserProfileForm(data=request.POST)


    if user_form.is_valid() and profile_form.is_valid():

        user = user_form.save()


        user.set_password(user.password)
        user.save()


        profile = profile_form.save(commit=False)
        profile.user = user


        if 'picture' in request.FILES:
            profile.picture = request.FILES['picture']


        profile.save()

        registered = True

    else:
        print user_form.errors, profile_form.errors


else:
    user_form = UserForm()
    profile_form = UserProfileForm()


return render(request,
        'Survey/register.html',
        {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} )

@login_required
def update(request):
updated = False
user = request.user
profile=request.userprofile
# If it's a HTTP POST, we're interested in processing form data.
if request.method == 'POST':

    user_form = UserForm(data=request.POST,instance=user)
    profile_form = UserProfileForm(data=request.POST,instance=profile)


    if user_form.is_valid() and profile_form.is_valid():

        user = user_form.save()

        u = User.objects.get(username=user.username)
        u.set_password(user.password)
        u.save()


        profile = profile_form.save(commit=False)
        profile.user = user


        if 'picture' in request.FILES:
            profile.picture = request.FILES['picture']


        profile.save()


        update = True


    else:
        print user_form.errors, profile_form.errors


else:
    user_form = UserForm(instance=user)
    profile_form = UserProfileForm()


return render(request,
        'Survey/update_profile.html.html',
        {'user_form': user_form,'profile_form': profile_form,'updated': updated} )

我有两个问题

1.我能够提取用户信息,但不能查看用户信息

2.我更新的密码字段是在没有散列的情况下存储的(在注册过程中密码是用散列存储的)所以我无法使用新密码登录,因为验证失败 有什么帮助吗?

0 个答案:

没有答案