问题描述
我最近在使用 FormView 时遇到了问题,发现解决方法是使用 get_form_kwargs.
这是我的代码:
class InternalResetPasswordView(FormView):模板名称 = 'reset_password.html'form_class = forms.InternalPasswordResetForm# success_message = "密码重置成功"# 获取请求对象# http://notesondjango.wordpress.com/2012/12/18/modelform-formview-and-the-request-object/# https://stackoverflow.com/questions/13383381/show-message-after-password-change# http://pydanny.com/simple-django-email-form-using-cbv.html# http://bubuzzz.wordpress.com/2012/05/01/class-based-generic-views-in-django-a-simple-sample/def get_form_kwargs(self):kwargs = super(InternalResetPasswordView, self).get_form_kwargs()kwargs['user'] = self.request.user返回 kwargsdef get_success_url(self):return reverse('user-detail', kwargs={'pk': self.request.user.id})@method_decorator(login_required)def dispatch(self, *args, **kwargs):return super(InternalResetPasswordView, self).dispatch(*args, **kwargs)'''def get_context_data(self, **kwargs):context = super(InternalResetPasswordView, self).get_context_data(**kwargs)context['InternalPasswordResetForm'] = context.get('form')返回上下文def get_form_kwargs(self):kwargs = super(InternalResetPasswordView, self).get_form_kwargs()kwargs['request'] = self.request返回 kwargs'''# self.request.user 方法获取自# https://docs.djangoproject.com/en/dev/topics/class-based-views/generic-editing/def form_valid(self, form):current_password = form.cleaned_data['old_password']new_password = form.cleaned_data['new_password1']Confirm_new_password = form.cleaned_data['new_password2']用户 = self.request.user如果 user.check_password(current_password) 和 new_password == confirm_new_password:user.set_password(new_password)用户.save()# form.valid() 重定向到 get_success_urlreturn super(InternalResetPasswordView, self).form_valid(form)
在看了这篇文章后,我仍然不明白为什么必须使用 get_form_kwargs 以及为什么在这种情况下使用 self.request 而不是 self.request.user 会导致 __init__() 得到一个意外的关键字参数 'request'
.
有人可以向我解释一下吗?
感谢所有帮助:)
get_form_kwargs
方法将返回一个包含 kwargs 的字典,该字典将传递给你的 __init__
形式.现在,如果您有一个表单需要一个名为 user
的 kwarg,并将其传递给一个名为 request
的 kwarg,它会报告您看到的错误.如果你想传递 request
而不是 user
(这是我通常做的,因为请求包含用户)那么你应该像这样定义你的表单类:
I was having trouble with FormView recently and found that the way to go about doing it was to use get_form_kwargs.
Here is my code:
class InternalResetPasswordView(FormView):
template_name = 'reset_password.html'
form_class = forms.InternalPasswordResetForm
# success_message = "Password was reset successfully"
# To get request object
# http://notesondjango.wordpress.com/2012/12/18/modelform-formview-and-the-request-object/
# https://stackoverflow.com/questions/13383381/show-message-after-password-change
# http://pydanny.com/simple-django-email-form-using-cbv.html
# http://bubuzzz.wordpress.com/2012/05/01/class-based-generic-views-in-django-a-simple-sample/
def get_form_kwargs(self):
kwargs = super(InternalResetPasswordView, self).get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
def get_success_url(self):
return reverse('user-detail', kwargs={'pk': self.request.user.id})
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(InternalResetPasswordView, self).dispatch(*args, **kwargs)
'''
def get_context_data(self, **kwargs):
context = super(InternalResetPasswordView, self).get_context_data(**kwargs)
context['InternalPasswordResetForm'] = context.get('form')
return context
def get_form_kwargs(self):
kwargs = super(InternalResetPasswordView, self).get_form_kwargs()
kwargs['request'] = self.request
return kwargs
'''
# self.request.user method obtained from
# https://docs.djangoproject.com/en/dev/topics/class-based-views/generic-editing/
def form_valid(self, form):
current_password = form.cleaned_data['old_password']
new_password = form.cleaned_data['new_password1']
confirm_new_password = form.cleaned_data['new_password2']
user = self.request.user
if user.check_password(current_password) and new_password == confirm_new_password:
user.set_password(new_password)
user.save()
# form.valid() redirects to get_success_url
return super(InternalResetPasswordView, self).form_valid(form)
After looking at this post, I still don't understand why get_form_kwargs has to be used and why using self.request instead of self.request.user in this case gives __init__() got an unexpected keyword argument 'request'
.
Could someone explain this to me?
Thanks for all the help :)
The get_form_kwargs
method will return a dictionary with the kwargs that will be passed to the __init__
of your form. Now, if you have a form that expects a kwarg named user
and pass it a kwarg named request
it will complain with the error you see. If you want to pass request
instead of user
(this is what I usually do since the request contains the user) then you should define your form class like this:
class RequestForm(forms.Form): def __init__(self, *args, **kwargs): self.request = kwargs.pop('request', None) super(RequestForm, self).__init__(*args, **kwargs)
这篇关于对 FormView 中的 get_form_kwargs 感到好奇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!