gpt4 book ai didi

python - Django 密码不匹配的获取属性

转载 作者:行者123 更新时间:2023-12-01 04:06:19 24 4
gpt4 key购买 nike

我在注册时遇到了这个问题。

我在 forms.py 中进行了注册:

class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
confirm_password = forms.CharField(widget=forms.PasswordInput())

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

def clean(self):
# Check that the two password entries match
password = self.cleaned_data.get("password")
conf_password = self.cleaned_data.get("confirm_password")
if conf_password and password and conf_password != password:
raise forms.ValidationError("Passwords don't match")
return password

当我尝试从本地主机注册时,会发生这种情况:

AttributeError at /signup/
'str' object has no attribute 'get'
Request Method: POST
Request URL: http://127.0.0.1:8000/signup/
Django Version: 1.9.1
Exception Type: AttributeError
Exception Value:
'str' object has no attribute 'get'
Exception Location: C:\Users\Hai\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\forms\models.py in _get_validation_exclusions, line 337
Python Executable: C:\Users\Hai\AppData\Local\Programs\Python\Python35-32\python.exe
Python Version: 3.5.0
Python Path:
['C:\\Users\\Hai\\OneDrive\\WebSoftware\\p3\\wsd_game',
'C:\\Users\\Hai\\AppData\\Local\\Programs\\Python\\Python35-32\\python35.zip',
'C:\\Users\\Hai\\AppData\\Local\\Programs\\Python\\Python35-32\\DLLs',
'C:\\Users\\Hai\\AppData\\Local\\Programs\\Python\\Python35-32\\lib',
'C:\\Users\\Hai\\AppData\\Local\\Programs\\Python\\Python35-32',
'C:\\Users\\Hai\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\site-packages']
Server time: Sat, 20 Feb 2016 20:09:17 +0200

如果我评论 def clean(self) 这个问题就会消失,但我需要密码相互匹配。

除了这张注册表之外,其他一切都按计划进行。当我完成表单并按下注册后,出现“get”属性错误。

查看

def register(request):
# Like before, get the request's context.
context = RequestContext(request)

# A boolean value for telling the template whether the registration was successful.
# Set to False initially. Code changes value to True when registration succeeds.
registered = False

# If it's a HTTP POST, we're interested in processing form data.
if request.method == 'POST':
# Attempt to grab information from the raw form information.
# Note that we make use of both UserForm and UserProfileForm.
user_form = UserForm(data=request.POST)
profile_form = UserProfileForm(data=request.POST)

# If the two forms are valid...
if user_form.is_valid() and profile_form.is_valid():
# Save the user's form data to the database.
user = user_form.save()

# Now we hash the password with the set_password method.
# Once hashed, we can update the user object.
user.set_password(user.password)
user.save()

# Now sort out the UserProfile instance.
# Since we need to set the user attribute ourselves, we set commit=False.
# This delays saving the model until we're ready to avoid integrity problems.
profile = profile_form.save(commit=False)
profile.user = user

# Did the user provide a profile picture?
# If so, we need to get it from the input form and put it in the UserProfile model.
if 'picture' in request.FILES:
profile.picture = request.FILES['picture']

# Now we save the UserProfile model instance.
profile.save()

# Update our variable to tell the template registration was successful.
registered = True

# Invalid form or forms - mistakes or something else?
# Print problems to the terminal.
# They'll also be shown to the user.
else:
print (user_form.errors, profile_form.errors)

# Not a HTTP POST, so we render our form using two ModelForm instances.
# These forms will be blank, ready for user input.
else:
user_form = UserForm()
profile_form = UserProfileForm()

# Render the template depending on the context.
return render_to_response(
'registration/sign_up.html',
{'user_form': user_form, 'profile_form': profile_form, 'registered': registered},
context)

型号:

class UserProfile(models.Model):
# This line is required. Links UserProfile to a User model instance.
user = models.OneToOneField(User)

# The additional attributes we wish to include.
website = models.URLField(blank=True)
picture = models.ImageField(upload_to='profile_images', blank=True)
CHOICES = (('pl', 'Player'),('dv', 'Developer'),)
user_type = models.CharField(max_length=2,choices=CHOICES,default='pl')


# Override the __unicode__() method to return out something meaningful!
def __unicode__(self):
return self.user.username

最佳答案

在 clean 函数结束时,您将返回密码本身,并且模型 View 无法使用 get 访问它(因为它是一个字符串)。只需使用 return self.cleaned_data 而不是 return password ,一切都会正常工作。

关于python - Django 密码不匹配的获取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35527143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com