Skip to main content
 首页 » 编程设计

python之使用 django 创建个人资料页面

2024年10月17日39findumars

我用 django 创建了一个个人资料页面,让用户可以更改我在 sing up form 中提供的信息。 (例如姓名、电子邮件、密码)

密码字段有两个问题:

1 - 当用户在文本字段中插入密码时,它以原始格式提交,我需要 django 用户的表格格式

<algorithm>$<iterations>$<salt>$<hash> 

我对个人资料页面的看法:

def user_profile(request): 
    current_user = request.user 
    form = UserProfileForm(request.POST or None, instance=current_user) 
    if request.POST: 
        if form.is_valid(): 
             # pwd = form.cleaned_data['password'] 
             # form_obj = form.save(commit=False) 
             # form_obj.password = make_password(pwd) 
             form.save() 
             message = "saved successfully" 
             return render(request, 'Profile.html', {'form':form, 'message':message}, context_instance=RequestContext(request)) 
 
    return render_to_response('Profile.html', { 
        'form': form, 
    }, context_instance=RequestContext(request)) 

正如您在评论中看到的那样,我使用 make_password 函数来散列密码,它工作正常但是在提交页面后,用户无法转到其他页面并需要重新登录...为什么?!

2 - 当个人资料页面显示给用户时,它填充了数据库中的当前信息,密码也是上述格式(哈希) 如果用户提交表单,密码字段没有任何更改,则密码已更改(它发送哈希一次并再次哈希!)

我怎样才能解决这个问题并在 django 中制作一个简单的工作配置文件页面? (我真的不喜欢它自己的管理面板!它看起来不太好!)

请您参考如下方法:

来自文档:

Changing a user’s password will log out all their sessions if the SessionAuthenticationMiddleware is enabled.

所以你必须使用update_session_auth_hash(request, user)

有关详细信息,请参阅 https://docs.djangoproject.com/en/1.9/topics/auth/default/#session-invalidation-on-password-change

关于预填充的密码字段:您应该将该字段设置为 passwordInput,默认情况下,它不会被预填充,请参阅 https://docs.djangoproject.com/en/1.9/ref/forms/widgets/#django.forms.PasswordInput