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

我一直在网上搜索(和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 111中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py”.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':'会员','pagetitle':'注册'})文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/userena/decorators.py”_wrapped_view 28. return view_func (request,* args,** kwargs)注册115中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/userena/views.py”.form = signup_form()异常类型:/ membership / signup / Exception中的TypeError值:'RegistrationForm'对象不可调用

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

先感谢您!

L.

回答(2)

2 years ago

您应该将表单类传递给对象的 signup 函数 . 有关更多信息,请查看code .

Insted:

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.signup 里面有 form = signup_form() . 当然,你现在丢失了表格初始值 .

2 years ago

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