Home > Blockchain >  AttributeError Object has no attribute
AttributeError Object has no attribute

Time:04-29

getting AttributeError at /url/ 'SignUpForm' object has no attribute 'newsletter', when trying to sign up a new user

#views
from .forms import SignUpForm
from somewhere.views import subscribe

def createaccount(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()

            if form.newsletter == True:
                email = request.POST['email']
                subscribe(email)
            else:
                pass

            login(request, user)
            return redirect('main:home')

#forms
class SignUpForm(UserCreationForm):
    newsletter = forms.BooleanField(label="Subscribe to Newsletter", required=False)

Traceback Error

Traceback (most recent call last):
  File "project\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "project\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "project\accounts\views.py", line 29, in create_account
    if form.newsletter == True:

Exception Type: AttributeError at /accounts/create_account
Exception Value: 'SignUpForm' object has no attribute 'newsletter'

CodePudding user response:

You should use cleaned_data to access the dictionary of values passed in through your form

if form.cleaned_data['newsletter'] == True:
    # Rest of code
  • Related