の初期データを取得することはできませんここでは、これは私がここDjangoのフォーム
'UserChangeForm' object has no attribute 'request'
を取得していますエラーが表示
であるジャンゴユーザー変更フォームでDjangoのフォームclass UserChangeForm(forms.ModelForm):
"""A form for updating users. Includes all the fields on
the user, but replaces the password field with admin's
password hash display field.
"""
class Meta:
model = MyUser
fields = ('first_name', 'last_name', 'mobile' , 'gender')
widgets = {
'gender': forms.RadioSelect(),
}
def __init__(self, *args, **kwargs):
super(UserChangeForm, self).__init__(*args, **kwargs)
if self.request.user.is_authenticated():
user = User.objects.get(email=self.request.user.email)
first_name = user.first_name
last_name = user.last_name
mobile = user.mobile
gender = user.gender
self.fields['first_name'].initial = first_name
self.fields['last_name'].initial = last_name
self.fields['mobile'].initial = mobile
self.fields['gender'].initial = gender
です
class UserProfileEditView(DetailView):
template_name = "edit_profile.html"
def get(self , request , *args , **kwargs):
context = {}
edit_form = UserChangeForm()
context['edit_form'] = edit_form
return render(request , self.template_name , context)
ここはトレースバックです
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\Users\lenovo\Desktop\Grooved2\grooved\src\accounts\views.py" in get
54. edit_form = UserChangeForm()
File "C:\Users\lenovo\Desktop\Grooved2\grooved\src\accounts\forms.py" in __init__
58. if self.request.user.is_authenticated():
django形式のフィールドの初期データを取得するにはどうすればよいですか? が後MHassanの応答
を編集し、あなたに
をありがとうここ
def __init__(self, *args, **kwargs):
super(UserChangeForm, self).__init__(*args, **kwargs)
intial_data = self.kwargs.get('initial')
request = intial_data.get('request')
if self.request.user.is_authenticated():
user = User.objects.get(email=request.user.email)
first_name = user.first_name
last_name = user.last_name
mobile = user.mobile
gender = user.gender
self.fields['first_name'].initial = first_name
self.fields['last_name'].initial = last_name
self.fields['mobile'].initial = mobile
self.fields['gender'].initial = gender
del self.fields['gender'].widget.choices[0]
フォームはこちらを眺めここ
def get(self , request , *args , **kwargs):
context = {}
edit_form = UserChangeForm(initial={'request': request})
context['edit_form'] = edit_form
return render(request , self.template_name , context)
は、トレースバック
ですされますFile "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\Users\lenovo\Desktop\Grooved2\grooved\src\accounts\views.py" in get
54. edit_form = UserChangeForm(initial={'request': request})
File "C:\Users\lenovo\Desktop\Grooved2\grooved\src\accounts\forms.py" in __init__
58. intial_data = self.kwargs.get('initial')
あなたのビューのコードを表示します。 –
ビューに追加@ MHassan –
私は答えを掲載しました。あなたのフォームに初期データを送るために 'get_initial'関数を使います。 –