在django allauth应用程序中重写SignupForm时,“AttributeError:'非类型'对象没有属性'会话'”
我尝试使用SignupForm覆盖实现django allauth应用程序,我的代码在将数据输入注册表单并保存用户时几乎可以工作,但保存后,我重定向到一个新页面,显示“NoneType”对象没有属性“session”AttributeError。 我怎样才能修好它 url.py views.py 覆盖SignupView,如下所示:在django allauth应用程序中重写SignupForm时,“AttributeError:'非类型'对象没有属性'会话'”,django,django-forms,django-views,django-allauth,Django,Django Forms,Django Views,Django Allauth,我尝试使用SignupForm覆盖实现django allauth应用程序,我的代码在将数据输入注册表单并保存用户时几乎可以工作,但保存后,我重定向到一个新页面,显示“NoneType”对象没有属性“session”AttributeError。 我怎样才能修好它 url.py views.py 覆盖SignupView,如下所示: from django.shortcuts import render, HttpResponseRedirect from .form import Allaut
from django.shortcuts import render, HttpResponseRedirect
from .form import AllauthLoginForm, AllauthSignUpForm
from allauth.account.views import SignupView
from django.urls import reverse_lazy
class AllauthSignUpView(SignupView):
template_name = 'register/signup.html'
form_class = AllauthSignUpForm
success_url = reverse_lazy('core:home') #Redirect to home.html
def form_valid(self, form):
# This method is called when valid form data has been POSTed.
if form.is_valid():
form.save()
form.clean()
return HttpResponseRedirect('core:home') #Redirect to home.html
# It should return an HttpResponse.
return super().form_valid(form)
def get_context_data(self, **kwargs):
context = super(AllauthSignUpView, self).get_context_data(**kwargs)
signUpForm = AllauthSignUpForm(self.request.POST or None)
context['form'] = signUpForm
return context
from allauth.account.forms import LoginForm, SignupForm
from django import forms
class AllauthSignUpForm(SignupForm):
def __init__(self, *args, **kwargs):
super(AllauthSignUpForm, self).__init__(*args, **kwargs)
self.fields['username'] = forms.CharField(
label='',
widget=forms.TextInput(
attrs={
'class': 'signup_name_inp text-right mb-4 border-top-0 border-right-0 border-left-0',
'placeholder': 'نام کاربری',
'dir': 'rtl',
}
),
)
self.fields['email'] = forms.EmailField(
label='',
widget=forms.EmailInput(
attrs={
'class': 'signup_mail_inp text-right mb-4 border-top-0 border-right-0 border-left-0',
'placeholder': 'ایمیل (اختیاری)',
'dir': 'rtl',
}
)
)
self.fields['password1'] = forms.CharField(
label='',
widget=forms.PasswordInput(
attrs={
'class': 'signup_pass_inp1 text-right mb-4 border-top-0 border-right-0 border-left-0',
'placeholder': 'کلمه عبور',
'dir': 'rtl',
}
)
)
self.fields['password2'] = forms.CharField(
label='',
widget=forms.PasswordInput(
attrs={
'class': 'signup_pass_inp2 text-right mb-4 border-top-0 border-right-0 border-left-0',
'placeholder': 'تکرار کلمه عبور',
'dir': 'rtl'
}
)
)
def save(self, request=None):
# Ensure you call the parent class's save.
# .save() returns a User object.
user = super(AllauthSignUpForm, self).save(request)
# Add your own processing here.
print(user.username)
# You must return the original result.
return user
form.py
按如下方式覆盖注册表单:
from django.shortcuts import render, HttpResponseRedirect
from .form import AllauthLoginForm, AllauthSignUpForm
from allauth.account.views import SignupView
from django.urls import reverse_lazy
class AllauthSignUpView(SignupView):
template_name = 'register/signup.html'
form_class = AllauthSignUpForm
success_url = reverse_lazy('core:home') #Redirect to home.html
def form_valid(self, form):
# This method is called when valid form data has been POSTed.
if form.is_valid():
form.save()
form.clean()
return HttpResponseRedirect('core:home') #Redirect to home.html
# It should return an HttpResponse.
return super().form_valid(form)
def get_context_data(self, **kwargs):
context = super(AllauthSignUpView, self).get_context_data(**kwargs)
signUpForm = AllauthSignUpForm(self.request.POST or None)
context['form'] = signUpForm
return context
from allauth.account.forms import LoginForm, SignupForm
from django import forms
class AllauthSignUpForm(SignupForm):
def __init__(self, *args, **kwargs):
super(AllauthSignUpForm, self).__init__(*args, **kwargs)
self.fields['username'] = forms.CharField(
label='',
widget=forms.TextInput(
attrs={
'class': 'signup_name_inp text-right mb-4 border-top-0 border-right-0 border-left-0',
'placeholder': 'نام کاربری',
'dir': 'rtl',
}
),
)
self.fields['email'] = forms.EmailField(
label='',
widget=forms.EmailInput(
attrs={
'class': 'signup_mail_inp text-right mb-4 border-top-0 border-right-0 border-left-0',
'placeholder': 'ایمیل (اختیاری)',
'dir': 'rtl',
}
)
)
self.fields['password1'] = forms.CharField(
label='',
widget=forms.PasswordInput(
attrs={
'class': 'signup_pass_inp1 text-right mb-4 border-top-0 border-right-0 border-left-0',
'placeholder': 'کلمه عبور',
'dir': 'rtl',
}
)
)
self.fields['password2'] = forms.CharField(
label='',
widget=forms.PasswordInput(
attrs={
'class': 'signup_pass_inp2 text-right mb-4 border-top-0 border-right-0 border-left-0',
'placeholder': 'تکرار کلمه عبور',
'dir': 'rtl'
}
)
)
def save(self, request=None):
# Ensure you call the parent class's save.
# .save() returns a User object.
user = super(AllauthSignUpForm, self).save(request)
# Add your own processing here.
print(user.username)
# You must return the original result.
return user
signup.html
下面有一些有用的链接:
您的代码似乎存在一些潜在问题,但可能导致您所述问题的主要原因是您没有将请求对象传递给views.py文件中的form.save调用 它应该看起来更像这样:
def form_valid(self, form):
# Don't do this. form_valid() only gets called on a valid form anyway
# if form.is_valid():
form.save(self.request)
# No need to call form.clean() again, especially after already saving the form
# form.clean()
return HttpResponseRedirect('core:home') #Redirect to home.html
因此,总而言之,您似乎并没有在自定义表单中执行任何自定义有效函数,因此您最好完全删除它,而只使用父类中定义的函数。您已经指定了成功url,以便让allauth的表单为您完成工作
def form_valid(self, form):
# Don't do this. form_valid() only gets called on a valid form anyway
# if form.is_valid():
form.save(self.request)
# No need to call form.clean() again, especially after already saving the form
# form.clean()
return HttpResponseRedirect('core:home') #Redirect to home.html