如何修复我的 django 表单没有 cleaned_data 属性的错误?
How do I fix the error of my django form not having cleaned_data attribute?
我有这个表格:
class RegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = User
fields = ["username", "email", "password1",
"pass"]
这个视图:
def register(req):
if req.method == 'POST':
form = RegisterForm(req.POST)
print('yaml')
if form.is_valid():
form.save()
return redirect("/home")
else:
form = RegisterForm()
form.save()
return render(req, "users/register.html", {"form":form})
但是当我访问 url
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("main.urls")),
path('register/', userViews.register, name="register"),
path('', include("django.contrib.auth.urls")),
]
我不断收到此错误:
Internal Server Error: /register/
....
AttributeError: 'RegisterForm' object has no attribute 'cleaned_data'
拜托,我需要一些帮助。
在 GET 请求的情况下删除 form.save()
:您只能保存一个对象,以防它是有界且有效的,因此:
def register(request):
if request.method == 'POST':
form = RegisterForm(request.POST, request.FILES)
if form.is_valid():
form.save()
# only redirect when the POST is <strong>successful</strong>
return redirect('home') # 🖘 use the name of a view
else:
form = RegisterForm() # <strong>no</strong> form.save()
return render(request, 'users/register.html', {'form': form})
首先,pass
是内置 User
模型中没有的字段,有两个字段与密码相关,分别是 password1
和 password2
,除非并且直到你没有在原来的 django 代码中改变它。
不需要重定向,else条件下请去掉form.save()
我有这个表格:
class RegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = User
fields = ["username", "email", "password1",
"pass"]
这个视图:
def register(req):
if req.method == 'POST':
form = RegisterForm(req.POST)
print('yaml')
if form.is_valid():
form.save()
return redirect("/home")
else:
form = RegisterForm()
form.save()
return render(req, "users/register.html", {"form":form})
但是当我访问 url
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("main.urls")),
path('register/', userViews.register, name="register"),
path('', include("django.contrib.auth.urls")),
]
我不断收到此错误:
Internal Server Error: /register/
....
AttributeError: 'RegisterForm' object has no attribute 'cleaned_data'
拜托,我需要一些帮助。
在 GET 请求的情况下删除 form.save()
:您只能保存一个对象,以防它是有界且有效的,因此:
def register(request):
if request.method == 'POST':
form = RegisterForm(request.POST, request.FILES)
if form.is_valid():
form.save()
# only redirect when the POST is <strong>successful</strong>
return redirect('home') # 🖘 use the name of a view
else:
form = RegisterForm() # <strong>no</strong> form.save()
return render(request, 'users/register.html', {'form': form})
首先,pass
是内置 User
模型中没有的字段,有两个字段与密码相关,分别是 password1
和 password2
,除非并且直到你没有在原来的 django 代码中改变它。
不需要重定向,else条件下请去掉form.save()