I have this form
class RegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = User
fields = ["username", "email", "password1",
"pass"]
and this view
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})
but when I visit the url
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("main.urls")),
path('register/', userViews.register, name="register"),
path('', include("django.contrib.auth.urls")),
]
I keep getting this error
Internal Server Error: /register/
....
AttributeError: 'RegisterForm' object has no attribute 'cleaned_data'
Please I need some help. Thanks in advance
CodePudding user response:
Remove the form.save()
in case of a GET request: you can only save an object in case it is bounded and valid, so:
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 successful
return redirect('home') #