Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'UserCreationForm' object has no attribute 'get_username' django 1.8

So I'm a newbie to django, know python enough not to call myself a beginner but I'm by no means a pro. I'm just trying to get user authentication working on a small django app. I'm using the default authentication system https://docs.djangoproject.com/en/1.8/topics/auth/default/ and the built in forms, the login, logout, etc have their own views but the UserCreationForm doesn't have it's own view, so I figured I had to make my own. Not sure what I'm doing wrong.

this is my views.py

  1 from django.shortcuts import render
  2 from django.http import HttpResponse
  3 from django.contrib.auth.forms import UserCreationForm
  4 from django.contrib.auth import login
  5
  6 def home(request):
  7         return HttpResponse("This is a barebones homepage")
  8
  9 def register(request):
 10         registered = False
 11
 12         if request.method == 'POST':
 13                 user_form = UserCreationForm(data=request.POST)
 14
 15                 if user_form.is_valid():
 16                         user = user_form.save()
 17                         username = user_form.get_username()
 18                         password = user_form.clean_password2()
 19                         login(request,user)
 20                 else:
 21                         print user_form.errors
 22         else:
 23                 user_form = UserCreationForm()
 24
 25         return render(request, 'registration/register.html', {'user_form': user_form, 'registered': registered}    )
~

this is my register.html

<!DOCTYPE html>

<html>
        <head>
                <title>Jet!</title>
        </head>

        <body>
                {% if registered %}
                        Jet! says Thank you for registering!
                        <a href='/'>Return to the homepage.</a><br />
                {% else %}
                <form method="post" action="/register/">
                {% csrf_token %}
                {{ user_form.as_p }}
                <input type="submit" name="submit" value="Register" />
                </form>
                {% endif %}
        </body>
</html>
like image 263
Luis F Hernandez Avatar asked Dec 07 '25 07:12

Luis F Hernandez


1 Answers

Firstly, the line username = user_form.get_username() is giving an error because, as the message says, the form does not have a get_username method. You can access the username with user_form.cleaned_data['username']

Secondly, the line password = user_form.clean would give an error, because the form has no attribute clean. If you needed it, you could get the value from the password1 field with user_form.cleaned_data['password1'].

Before you login the user, you must authenticate them, otherwise you will get an error about the user having no attribute backend.

Putting it together, you have:

if user_form.is_valid():
    user_form.save()
    username = user_form.cleaned_data['username']
    password = user_form.cleaned_data['password1']
    user = authenticate(username=username, password=password)
    login(request, user)

You'll have to import the authenticate method by changing your import to:

from django.contrib.auth import authenticate, login

Note that you haven't set registered=True anywhere in your code. Usually, it is good practice to redirect after the form has been successfully submitted, to prevent duplicate submissions.

like image 144
Alasdair Avatar answered Dec 08 '25 20:12

Alasdair