将** kwargs传递给Django表单 [英] Passing **kwargs to Django Form

查看:527
本文介绍了将** kwargs传递给Django表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建用于更改应用程序的用户名和用户电子邮件的自定义django表单。这就是为什么我需要将会话中的用户详细信息传递给表单,以便检查是否存在已登录的用户。我在这样做:



in views.py

  personal_info_form = PersonalInfoForm(prefix ='personal_info',
user_details = user_details)

其中 user_details 是一个字典:

 'user_details':[{'username' u'username',
'registration_date':datetime.date(2009,10,22),
'id':13,'email':u'user@mail.com'}}}

在forms.py中,我有以下代码:



个人信息格式(forms.Form):

def __init __(self,* args,** kwargs):
user_details = kwargs.pop ('user_details',None)
super(PersonalInfoForm,self).__ init __(* args,** kwargs)

username = forms.CharField(required = True,initial = user_details [0 ] ['username'])
email = forms.EmailField(required = T rue)

我收到以下错误:

  name'user_details'未定义

使用 self.user_details 访问它,只有 user_details ,它给我一样的错误

解决方案

user_details 传递给 __ init __ 在它之外没有定义。这就是为什么当你设置CharField对象时你无法访问它。在 __ init __ 中设置初始,从kwargs弹出它后,例如:

  class PersonalInfoForm(forms.Form):

username = forms.CharField(required = True)
email = form.EmailField(required = True)

def __init __(self,* args,** kwargs):
user_details = kwargs.pop('user_details',None)
super (PersonalInfoForm,self).__ init __(* args,** kwargs)
如果user_details:
self.fields ['username']。initial = user_details [0] ['username']

当你有机会考虑阅读在python中的范围


I am trying to build custom django form for changing username and user email for an application. That's why I need to pass the user details from the session to a form in order to check if the logged user exists. I am doing in this way:

in views.py

personal_info_form = PersonalInfoForm(prefix='personal_info',
                                      user_details=user_details)

where user_details is a dictionary:

'user_details': [{'username': u'username', 
                  'registration_date': datetime.date(2009, 10, 22), 
                  'id': 13, 'email': u'user@mail.com'}]}

In forms.py I have the following code:

class PersonalInfoForm(forms.Form):

    def __init__(self, *args, **kwargs):
        user_details = kwargs.pop('user_details', None)
        super(PersonalInfoForm, self).__init__( *args, **kwargs)

    username = forms.CharField(required=True, initial=user_details[0]['username'])
    email = forms.EmailField(required=True)

And I get the following error:

name 'user_details' is not defined

I tried accessing it with self.user_details and only user_details and it gives me the same error

解决方案

user_details is passed to __init__, so is not defined outside of it. That's why you can't access it when you're instatiating that CharField object. Set initial in __init__ itself, after you've popped it from kwargs, for instance:

class PersonalInfoForm(forms.Form):

    username = forms.CharField(required=True)
    email = forms.EmailField(required=True)

    def __init__(self, *args, **kwargs):
        user_details = kwargs.pop('user_details', None)
        super(PersonalInfoForm, self).__init__(*args, **kwargs)
        if user_details:
            self.fields['username'].initial = user_details[0]['username']

When you get a chance, consider reading up on scopes in python.

这篇关于将** kwargs传递给Django表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆