简体   繁体   中英

KeyError in customized Django-all auth form

I'm working on a website built on Django framework. I chose django-allauth as my backend and have customized the standard login form it provides like so:

myapp/forms.py:

from allauth.account.forms import LoginForm, SignupForm
from django import forms


class MyLoginForm(LoginForm):
    def __init__(self, *args,
                 **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)
        self.fields['remember'].label = 'Remember me'
        self.fields['remember'].initial = True
      # To use a placeholder text we'll have to use below
        login_widget = forms.TextInput(attrs={'type':'email',
                                      'placeholder':
                                       ('Enter your email'),
                                      'autofocus':'autofocus'
                                                     })
        self.fields['email'] = forms.CharField(label=("Email"),
                                               widget=login_widget)

        self.fields['email'].label = 'Email'

        def clean_email(self):
            email = self.cleaned_data.get('email')
            base, provider = email.split('@')
            domain, extent = provider.split('.')
            if extent == "edu":
                forms.ValidationError("Please use a valid \
                                      personal email address")
            return email



class MySignUpForm(SignupForm):
    def __init__(self, *args, **kwargs):
        super(SignupForm, self).__init__(*args, **kwargs)

And here is my view which handles the form rendering: another_app/views.py:

from django.shortcuts import render
from userlogin.forms import MyLoginForm, MySignUpForm


def index(request):
    login = MyLoginForm()
    signup = MySignUpForm()
    if request.method == "POST":
        login = MyLoginForm(request.POST or None)
        signup = MySignUpForm(request.POST or None)

        if login.is_valid():
            lg_instance = login.save(commit=False)
            print (lg_instance)

    context = {
              'login': login,
              'signup': signup,
              'login_url': '/profile/'
    }

    return render(
        request,
        'homepage.html',
        context,
    )

Given the above information, I've got the following questions:

  1. Upon my clicking submit, the website shows KeyError at /. What could be the cause of this? The traceback shows that in the 'LoginForm' class(in allauth/accounts/forms.py) there is a field called Login which is causing conflict.

  2. How do I actually log the user in and go to the profile view.

The issue is with MyLoginForm initialization. Remove all code related to email from there. Use ACCOUNT_AUTHENTICATION_METHOD = 'email' in your settings.py and then use __init__ method to override placeholders only. Check here that email is already supported in django all auth.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM