/accounts/signup/View.__init__() 处的类型错误采用 1 个位置参数,但给出了 2 个
TypeError at /accounts/signup/ View.__init__() takes 1 positional argument but 2 were given
我正在使用 Django allauth 对用户进行身份验证以进行注册和登录。但是当我尝试访问 django allauth 注册和登录模板时出现此错误 TypeError at /accounts/signup/ View.__init__() takes 1 positional argument but 2 were given
。
urls.py:
from django.urls import path
from allauth.account import views as allauthViews
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("<int:myid>/", views.quiz, name="quiz"),
path('<int:myid>/data/', views.quiz_data_view, name='quiz-data'),
path('<int:myid>/save/', views.save_quiz_view, name='quiz-save'),
path("accounts/signup/", allauthViews.SignupView, name="account_signup"),
path("accounts/login/", allauthViews.LoginView, name="account_login"),
path("accounts/logout/", allauthViews.LogoutView, name="account_logout"),
path('add_quiz/', views.add_quiz, name='add_quiz'),
path('add_question/', views.add_question, name='add_question'),
path('add_options/<int:myid>/', views.add_options, name='add_options'),
path('results/', views.results, name='results'),
path('delete_question/<int:myid>/', views.delete_question, name='delete_question'),
path('delete_result/<int:myid>/', views.delete_result, name='delete_result'),
]
注册视图
class SignupView(
RedirectAuthenticatedUserMixin,
CloseableSignupMixin,
AjaxCapableProcessFormViewMixin,
FormView,
):
template_name = "account/signup." + app_settings.TEMPLATE_EXTENSION
form_class = SignupForm
redirect_field_name = "next"
success_url = None
@sensitive_post_parameters_m
def dispatch(self, request, *args, **kwargs):
return super(SignupView, self).dispatch(request, *args, **kwargs)
def get_form_class(self):
return get_form_class(app_settings.FORMS, "signup", self.form_class)
def get_success_url(self):
# Explicitly passed ?next= URL takes precedence
ret = (
get_next_redirect_url(self.request, self.redirect_field_name)
or self.success_url
)
return ret
def form_valid(self, form):
# By assigning the User to a property on the view, we allow subclasses
# of SignupView to access the newly created User instance
self.user = form.save(self.request)
try:
return complete_signup(
self.request,
self.user,
app_settings.EMAIL_VERIFICATION,
self.get_success_url(),
)
except ImmediateHttpResponse as e:
return e.response
def get_context_data(self, **kwargs):
ret = super(SignupView, self).get_context_data(**kwargs)
form = ret["form"]
email = self.request.session.get("account_verified_email")
if email:
email_keys = ["email"]
if app_settings.SIGNUP_EMAIL_ENTER_TWICE:
email_keys.append("email2")
for email_key in email_keys:
form.fields[email_key].initial = email
login_url = passthrough_next_redirect_url(
self.request, reverse("account_login"), self.redirect_field_name
)
redirect_field_name = self.redirect_field_name
site = get_current_site(self.request)
redirect_field_value = get_request_param(self.request, redirect_field_name)
ret.update(
{
"login_url": login_url,
"redirect_field_name": redirect_field_name,
"redirect_field_value": redirect_field_value,
"site": site,
}
)
return ret
callback
<class 'allauth.account.views.SignupView'>
callback_args
()
callback_kwargs
{}
middleware_method
<bound method CsrfViewMiddleware.process_view of <django.middleware.csrf.CsrfViewMiddleware object at 0x0000017B8F25B070>>
request
<WSGIRequest: GET '/accounts/signup/'>
response
None
self
<django.core.handlers.wsgi.WSGIHandler object at 0x0000017B8DF32290>
wrapped_callback
<class 'allauth.account.views.SignupView'>
您需要在网址中使用 as_view()
…
urlpatterns = [
…
path("accounts/signup/", allauthViews.SignupView.as_view(), name="account_signup"),
path("accounts/login/", allauthViews.LoginView.as_view(), name="account_login"),
path("accounts/logout/", allauthViews.LogoutView.as_view(), name="account_logout"),
…
]
我正在使用 Django allauth 对用户进行身份验证以进行注册和登录。但是当我尝试访问 django allauth 注册和登录模板时出现此错误 TypeError at /accounts/signup/ View.__init__() takes 1 positional argument but 2 were given
。
urls.py:
from django.urls import path
from allauth.account import views as allauthViews
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("<int:myid>/", views.quiz, name="quiz"),
path('<int:myid>/data/', views.quiz_data_view, name='quiz-data'),
path('<int:myid>/save/', views.save_quiz_view, name='quiz-save'),
path("accounts/signup/", allauthViews.SignupView, name="account_signup"),
path("accounts/login/", allauthViews.LoginView, name="account_login"),
path("accounts/logout/", allauthViews.LogoutView, name="account_logout"),
path('add_quiz/', views.add_quiz, name='add_quiz'),
path('add_question/', views.add_question, name='add_question'),
path('add_options/<int:myid>/', views.add_options, name='add_options'),
path('results/', views.results, name='results'),
path('delete_question/<int:myid>/', views.delete_question, name='delete_question'),
path('delete_result/<int:myid>/', views.delete_result, name='delete_result'),
]
注册视图
class SignupView(
RedirectAuthenticatedUserMixin,
CloseableSignupMixin,
AjaxCapableProcessFormViewMixin,
FormView,
):
template_name = "account/signup." + app_settings.TEMPLATE_EXTENSION
form_class = SignupForm
redirect_field_name = "next"
success_url = None
@sensitive_post_parameters_m
def dispatch(self, request, *args, **kwargs):
return super(SignupView, self).dispatch(request, *args, **kwargs)
def get_form_class(self):
return get_form_class(app_settings.FORMS, "signup", self.form_class)
def get_success_url(self):
# Explicitly passed ?next= URL takes precedence
ret = (
get_next_redirect_url(self.request, self.redirect_field_name)
or self.success_url
)
return ret
def form_valid(self, form):
# By assigning the User to a property on the view, we allow subclasses
# of SignupView to access the newly created User instance
self.user = form.save(self.request)
try:
return complete_signup(
self.request,
self.user,
app_settings.EMAIL_VERIFICATION,
self.get_success_url(),
)
except ImmediateHttpResponse as e:
return e.response
def get_context_data(self, **kwargs):
ret = super(SignupView, self).get_context_data(**kwargs)
form = ret["form"]
email = self.request.session.get("account_verified_email")
if email:
email_keys = ["email"]
if app_settings.SIGNUP_EMAIL_ENTER_TWICE:
email_keys.append("email2")
for email_key in email_keys:
form.fields[email_key].initial = email
login_url = passthrough_next_redirect_url(
self.request, reverse("account_login"), self.redirect_field_name
)
redirect_field_name = self.redirect_field_name
site = get_current_site(self.request)
redirect_field_value = get_request_param(self.request, redirect_field_name)
ret.update(
{
"login_url": login_url,
"redirect_field_name": redirect_field_name,
"redirect_field_value": redirect_field_value,
"site": site,
}
)
return ret
callback
<class 'allauth.account.views.SignupView'>
callback_args
()
callback_kwargs
{}
middleware_method
<bound method CsrfViewMiddleware.process_view of <django.middleware.csrf.CsrfViewMiddleware object at 0x0000017B8F25B070>>
request
<WSGIRequest: GET '/accounts/signup/'>
response
None
self
<django.core.handlers.wsgi.WSGIHandler object at 0x0000017B8DF32290>
wrapped_callback
<class 'allauth.account.views.SignupView'>
您需要在网址中使用 as_view()
…
urlpatterns = [
…
path("accounts/signup/", allauthViews.SignupView.as_view(), name="account_signup"),
path("accounts/login/", allauthViews.LoginView.as_view(), name="account_login"),
path("accounts/logout/", allauthViews.LogoutView.as_view(), name="account_logout"),
…
]