简体   繁体   English

TypeError: UserCreateForm() 得到了一个意外的关键字参数“初始”

[英]TypeError: UserCreateForm() got an unexpected keyword argument 'initial'

I am new to django and I am developing a social media site following the course and I am facing this weird error when I try to open signup page from the site, which after a lot of search and experiments wasn't able to get away with.Thus, I came here so if anyone could help me.我是django的新手,我正在按照课程开发一个社交媒体网站,当我尝试从该网站打开注册页面时遇到这个奇怪的错误,经过大量搜索和实验后无法逃脱.因此,我来到这里,如果有人可以帮助我。

My forms.py file where i think the actual culprit is我认为真正的罪魁祸首是我的 forms.py 文件

from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm

def UserCreateForm(UserCreationForm):

  class Meta:
      fields = ['username', 'email', 'password1', 'password2']
      model = get_user_model()

  def __init__(self, *args, **kwargs):
      super(UserCreationForm, self).__init__(*args, **kwargs)
      self.fields['username'].label = 'Username'
      self.fields['email'].label = 'Email Address'

views.py file视图.py 文件

from django.shortcuts import render
from django.views.generic import CreateView
from django.urls import reverse_lazy
from . import forms


# Create your views here.
class SignUpView(CreateView):
    form_class = forms.UserCreateForm
    success_url = reverse_lazy('login')
    template_name = 'accounts/signup.html'

urls.py file in the application directory ie accounts/urls.py应用程序目录中的 urls.py 文件,即 accounts/urls.py

from django.urls import re_path
from django.contrib.auth.views import LoginView, LogoutView
from . import views

#code here
urlpatterns = [
    re_path(r'^login/$', LoginView.as_view(template_name='signin.html'), name='login'),
    re_path(r'^logout/$', LogoutView.as_view(), name='logout'),
    re_path(r'^signup/$', views.SignUpView.as_view(), name='signup'),
]

full log of error I am facing我面临的完整错误日志

File "C:\Users\dell\Anaconda3\envs\pyEnv\lib\site-packages\django\views\generic\edit.py", line 133, in get
    return self.render_to_response(self.get_context_data())
  File "C:\Users\dell\Anaconda3\envs\pyEnv\lib\site-packages\django\views\generic\edit.py", line 66, in get_context_
data
    kwargs['form'] = self.get_form()
  File "C:\Users\dell\Anaconda3\envs\pyEnv\lib\site-packages\django\views\generic\edit.py", line 33, in get_form
    return form_class(**self.get_form_kwargs())
TypeError: UserCreateForm() got an unexpected keyword argument 'initial'
[31/May/2020 12:51:46] "GET /accounts/signup/ HTTP/1.1" 500 90675

I ran into the same problem as this, and for the same reason: You declared your form improperly.我遇到了同样的问题,出于同样的原因:你不正确地声明了你的表格。

This:这个:

def UserCreateForm(UserCreationForm):

Should be this:应该是这样的:

class UserCreateForm(UserCreationForm):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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