前提・実現したいこと
DjangoでBLOGを作成しております。
FormViewを利用して
アカウント作成の処理を書いておりますが、以下エラーが発生しております。
init() takes 1 positional argument but 2 were given
前提条件として何か必要でしょうか。
発生している問題・エラーメッセージ
TypeError at /user/add __init__() takes 1 positional argument but 2 were given Request Method: GET Request URL: http://localhost:8080/user/add Django Version: 3.0.8 Exception Type: TypeError Exception Value: __init__() takes 1 positional argument but 2 were given Exception Location: /usr/local/lib64/python3.6/site-packages/django/core/handlers/base.py in _get_response, line 113 Python Executable: /usr/bin/python3 Python Version: 3.6.8 Python Path: ['/var/www/html/wto_web_app', '/usr/lib64/python36.zip', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/usr/local/lib64/python3.6/site-packages', '/usr/local/lib/python3.6/site-packages', '/usr/lib64/python3.6/site-packages', '/usr/lib/python3.6/site-packages']
Internal Server Error: /user/add Traceback (most recent call last): File "/usr/local/lib64/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/usr/local/lib64/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "/usr/local/lib64/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) TypeError: __init__() takes 1 positional argument but 2 were given
該当のソースコード
Django
1### user/view.py 2class UserAdd(FormView): 3 context_object_name = 'add_user' 4 template_name = 'user/user_add.html' 5 form_class = UserAddForm 6 7 def form_valid(self, form): 8 password = form.cleaned_data.get('password') 9 user = User.objects.create_user( 10 screen_id = form.cleaned_data.get('screen_id'), 11 email = form.cleaned_data.get('email'), 12 password = password 13 ) 14 user.icon = form.cleaned_data.get('icon') 15 user.screen_name = form.cleaned_data.get('screen_name') 16 user.full_name = form.cleaned_data.get('full_name') 17 user.full_kana = form.cleaned_data.get('full_kana') 18 user.resident_flag = form.cleaned_data.get('resident_flag') 19 user.notification_flag = form.cleaned_data.get('notification_flag') 20 user.city = form.cleaned_data.get('country') 21 user.save() 22 return super().form_valid(form) 23 24 def form_invalid(self, form): 25 return super().form_invalid(form) 26 27 def get_success_url(self): 28 return reverse_lazy('top_page') 29
Django
1### user/forms.py 2class UserAddForm(forms.Form): 3 4 # アイコン 5 icon = forms.ImageField( 6 label='アイコン', 7 widget=forms.TextInput(), #あとで 8 required=False, # defであとで定義 9 ) 10 11 # ユーザー名 12 screen_name = forms.CharField( 13 label='ユーザー名', 14 max_length=30, 15 widget=forms.TextInput(), 16 error_messages={ 17 'required': get_error_message(message='required_user_screen_name'), 18 } 19 ) 20 21 # ID 22 screen_id = forms.CharField( 23 label='ID', 24 widget=forms.TextInput(), 25 max_length=30, 26 min_length=6, 27 error_messages={ 28 'required': get_error_message(message='required_user_screen_id'), 29 'min_length': get_error_message(message='min_length',num='6') 30 } 31 ) 32 33 # 氏名 34 full_name = forms.CharField( 35 label='氏名', 36 max_length=60, 37 error_messages={ 38 'required': get_error_message(message='required_user_name'), 39 } 40 ) 41 42 # フリガナ 43 full_kana = forms.CharField( 44 label='フリガナ', 45 max_length=60, 46 error_messages={ 47 'required': get_error_message(message='required_user_kana'), 48 } 49 ) 50 51 # メールアドレス 52 email = forms.CharField( 53 label='メールアドレス', 54 max_length=255, 55 widget=forms.TextInput(), 56 error_messages={ 57 'required': get_error_message(message='required_user_email'), 58 'max_length': get_error_message(message='max_length',num='128') 59 } 60 ) 61 62 # 海外在住フラグ 63 resident_flag = forms.ChoiceField( 64 label= '海外在住の有無', 65 widget=forms.RadioSelect, 66 choices=RESIDENT_CHOICE, 67 ) 68 69 # 通知フラグ 70 notification_flag = forms.ChoiceField( 71 label= '通知', 72 widget=forms.RadioSelect, 73 choices=NOTIFICATION_CHOICE, 74 ) 75 76 # パスワード 77 password = forms.CharField( 78 label='パスワード', 79 widget=forms.PasswordInput(), 80 max_length=30, 81 min_length=10, 82 error_messages={ 83 'required': get_error_message(message='required_password'), 84 } 85 ) 86 87 # パスワード確認 88 password_verification = forms.CharField( 89 label='パスワード確認', 90 widget=forms.PasswordInput(), 91 max_length=30, 92 min_length=10, 93 error_messages={ 94 'required': get_error_message(message='required_password_verification'), 95 } 96 ) 97 98 # 興味のある国 99 country=forms.MultipleChoiceField( 100 label='興味のある国', 101 required=False, 102 widget=forms.CheckboxSelectMultiple 103 ) 104 105 def __init__(self, *args, **kwargs): 106 super().__init__(*args, **kwargs) 107 # 国リスト 108 countries = City.objects.all() 109 list = [] 110 for country in countries: 111 list.append((str(country.pk),country.country_name)) 112 self.fields['country'].choices = list 113
試したこと
class から def にすると表示は可能なことからforms.pyの記述には問題なし(?)
def StudentUserAdd(FormView):
return HttpResponse(str(UserAddForm()))
補足情報(FW/ツールのバージョンなど)
Python3.6.8
Django3.0.8
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。