[英]str' object has no attribute
when i want to create a new user i got this error 当我想创建一个新用户时,出现此错误
Exception Value:
'str' object has no attribute 'get'
here is a full code of view.py 这是view.py的完整代码
from .forms import LoginForm, RegisterForm
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, get_user_model
from django.http import HttpResponse
def register_page(request):
signup_form = RegisterForm(request.POST or None)
context = {"signup_form": signup_form}
if signup_form.is_valid():
print(signup_form.cleaned_data)
username = signup_form.cleaned_data.get("username")
email = signup_form.cleaned_data.get("email")
password = signup_form.cleaned_data.get("password")
new_user = user.objects.create_user(username, email, password)
print(new_user)
return render(request, "auth/signup.html", context)
here is a code of forms.py 这是forms.py的代码
class RegisterForm(forms.Form):
username = forms.CharField(widget=forms.TextInput(
attrs={"class": "form-control","placeholder": "your username"}))
email = forms.CharField(widget=forms.EmailInput(
attrs={'class': 'form-control',"placeholder": "your Password"}))
password = forms.CharField(widget=forms.PasswordInput(
attrs={'class': 'form-control',"placeholder": "your Password"}))
password2 = forms.CharField(label='Confirm possword',
widget=forms.PasswordInput(attrs={'class': 'form-control',
"placeholder": "your Password"}))
def clean(self):
data = self.cleaned_data
username = self.cleaned_data.get('username')
qs = user.objects.filter(username=username)
if qs.exists():
raise forms.ValidationError('username already exists')
return username
email = self.cleaned_data.get('email')
qs = user.objects.filter(email=email)
if qs.exists():
raise forms.ValidationError('email already exists')
return email
password = self.cleaned_data.get('password')
password2 = self.cleaned_data.get('password2')
if password2 != password:
raise forms.ValidationError("Password must be matched")
return data
here is all the code now please help to to solve this error. 这是所有代码,现在请帮助解决该错误。 but when i remove validation from email fields the code run successfully
但是当我从电子邮件字段中删除验证时,代码成功运行
Is the line return email
in there a mistake? 线路
return email
是否有误? You have return
in there twice. 您已经两次
return
那里。
qs = user.objects.filter(email=email)
if qs.exists():
raise forms.ValidationError('email already exists')
return email # <--- THIS LINE!
password = self.cleaned_data.get('password')
password2 = self.cleaned_data.get('password2')
if password2 != password:
raise forms.ValidationError("Password must be matched")
return data
That could be your issue because you're returning a string and not an object that the clean()
method expects. 那可能是您的问题,因为您返回的是字符串,而不是
clean()
方法期望的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.