我已经创建了一个注册和登录功能,它使用django User对象将用户数据保存到数据库中。但是当我注册用户时,链接到用户的密码不会被正确散列。这意味着我在django管理界面中出现此错误:“无效的密码格式或未知的散列算法。”。我已经确保使用set_password。
models.py
from django.db import models
from django.contrib.auth.models import User
class User_Information(models.Model):
# Links UserProfile to a User model instance
user = models.OneToOneField(User)
# Override the __unicode__() method to return username
def __unicode__(self):
return self.username
forms.py
from django import forms
from django.contrib.auth.models import User
from authentication.models import User_Information
class User_Form(forms.ModelForm):
# Using the PasswordInput widget to hide the entered content of the password field
password = forms.CharField(widget=forms.PasswordInput())
# Define the nested class. The default fields can be edited here, if you wish to exclude something.
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password')
views.py
def register(request):
context = RequestContext(request)
# Checks if registration was successful. Changes to true if this is the case
# Processing form data.
if request.method == 'POST':
user_form = User_Form(data=request.POST)
# If the form is valid.
if user_form.is_valid():
# Saves the user's data to the database.
user = user_form.save()
# Hash the password and updates the user object.
user.set_password(user.password)
user.save
# Tell the template that registration was successful
messages.success(request, 'You registered successfully')
else:
print user_form.errors
else:
user_form = User_Form()
return render_to_response(
'authentication/register.html',
{'user_form': user_form},
context)
提前致谢。
答案 0 :(得分:0)
可以根据需要扩展预定义的表单django.contrib.auth.forms.UserCreationForm。