如何使用Django-Userena将初始值传递给自定义表单

时间:2012-09-13 20:40:55

标签: django django-forms django-views

我一直在网上搜索(和StackOverflow),以回答看似真正简单的问题。我正在使用django-userena和自定义表单。

我的form.py里面是:

class RegistrationForm(SignupFormOnlyEmail):

    first_name = forms.CharField(label='First Name', max_length=30, required=True, error_messages={'required':'Please Provide Your First Name'})
    last_name = forms.CharField(label='Last Name', max_length=30, required=True)
    email2 = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=75)), label=_("Retype Email"), required=True)
    parent_type = forms.ChoiceField(label="I am a", choices=ACCOUNT_TYPES, widget=forms.RadioSelect)
    child_count = forms.ChoiceField(label="My Kids", choices=KID_CHOICES, widget=forms.Select)
    grade = forms.ChoiceField(label="My Classroom", choices=GRADE_CHOICES, widget=forms.Select)
    tos = forms.BooleanField(label="I accept the Terms of Use.", required=True)

    def __init__(self, *args, **kwargs):

        self.initial = kwargs.pop('initial', None)
        kidCount = self.initial['child_count']
        account_type = self.initial['parent_type']

        super(RegistrationForm, self).__init__(*args, **kwargs)

在我的urls.py中,我把:

url(r'^membership/signup/$', signup_view, name='userena_signup'),

我的自定义视图如下所示:

def signup_view(request):

    form = RegistrationForm(initial={'parent_type':request.session['parent_type'],'child_count':request.session['child_count']})
    response = userena_views.signup(request,  signup_form=form, extra_context={'section':'Membership','pagetitle':'Sign Up'})
    return response

当我发布表单并转到/ membership / signup /时,我得到

的“TypeError”
  

'RegistrationForm'对象不可调用

这是堆栈跟踪的转储:

  

回溯:   在get_response中输入文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py”     111. response = callback(request,* callback_args,** callback_kwargs)   在signup_view中输入文件“/Users/lawrenceleach/Dropbox/Sites/django/wonderville/membership/views.py”     42. response = userena_views.signup(request,signup_form = form,extra_context = {'section':'Membership','pagetitle':'Sign up'})   _wrapped_view中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/userena/decorators.py”     28. return view_func(request,* args,** kwargs)   注册时提交“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/userena/views.py”     115. form = signup_form()

     

异常类型:/ membership / signup /的TypeError   异常值:'RegistrationForm'对象不可调用

就像我说的那样,我确定答案是正确地盯着我,我只是看不到它。任何帮助将不胜感激。

提前谢谢!

L,

2 个答案:

答案 0 :(得分:1)

您应该将表单类传递给signup对象的函数。请查看code以获取更多信息。

Insted of:

form = RegistrationForm(initial={'parent_type':request.session['parent_type'],'child_count':request.session['child_count']})
response = userena_views.signup(request,  signup_form=form, extra_context={'section':'Membership','pagetitle':'Sign Up'})

应该是:

response = userena_views.signup(request,  signup_form=RegistrationForm, extra_context={'section':'Membership','pagetitle':'Sign Up'})

因为在userena_views.signupform = signup_form()。当然,你现在丢失了表格初始值。

答案 1 :(得分:1)

解决方案是将初始表单值作为字典对象传递给视图中表单调用的“extra_context”变量。如果一直使用Django的内置表单工具,那么我最初在上面做过的工作就可以了。我把这个问题发布给了Userena的人。它似乎是他们系统中的一个错误。