C
C
Cyber_bober2016-03-10 00:43:44
Django
Cyber_bober, 2016-03-10 00:43:44

Why doesn't clean work?

Doing registration, Django 1.9 Python 2.7

def signup(request):
    user_form = UserCreateForm(data=request.POST)
    if request.method == 'POST':
        if user_form.is_valid():
            username = user_form.clean_username()
            password = user_form.clean_password2()
            user_form.save()
            user = authenticate(username=username, password=password)
            login(request, user)
            return redirect('/edit')
        else:
            return index(request, user_form=user_form)
    return redirect('/edit')

I get the error
'UserCreateForm' object has no attribute 'clean_username'
What could be the reason? Am I using the old syntax?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rostislav Grigoriev, 2016-03-10
@Cyber_bober

username = user_form.cleaned_data['username']
The clean() method has already worked once, all data must be taken from self.cleaned_data

D
Dmitry Voronkov, 2016-03-10
@DmitryVoronkov

It would probably be better that way. But in general I advise you to always look at the source.

def signup(request):
    user_form = UserCreateForm(data=request.POST)
    if request.method == 'POST':
        if user_form.is_valid():
            model = user_form.save()
            user = authenticate(username=model.username, password=model.password)
            login(request, user)
            return redirect('/edit')
        else:
            return index(request, user_form=user_form)
    return redirect('/edit')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question