python - 将参数传递给 RegistrationView 的 form_class

标签 python django django-forms django-views

我正在尝试使用 RegistrationView 类将自定义参数传递到名为“RegistrationForm”的表单。

但是,当我执行以下操作时,我只是收到错误:

TypeError at /accounts/register/
'RegistrationForm' object is not callable

我猜这是因为我创建了一个对象,并且我不只是将类名传递给 RegistrationView。

有什么办法可以实现这个目标吗?

url.py

url(r'^register/$',
    RegistrationViewBillingSecret.as_view(template_name='registration/registration_form.html'),
    name='registration_register'),

View .py

from registration.backends.default.views import RegistrationView
class RegistrationViewBillingSecret(RegistrationView):
    form_class = RegistrationForm(billing_secret='somestring')

最佳答案

我似乎有这种能力,只有在提出问题后才能弄清楚如何解决问题。

这是我为接受 form_class 和 RegistrationView 子类的参数所做的操作。

class RegistrationViewBillingSecret(RegistrationView):
    form_class = RegistrationForm
    billing_secret = None
    def get_form_kwargs(self):
        kwargs = super(RegistrationViewBillingSecret, self).get_form_kwargs()
        kwargs.update({'billing_secret': self.billing_secret})
        return kwargs
    def __init__(self, *args, **kwargs):
        self.billing_secret = kwargs.pop('billing_secret', None)

关于python - 将参数传递给 RegistrationView 的 form_class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25954941/

相关文章:

python - Django:将表单放入表单(递归...)

java - 在 JAVA 上与 Telegram BOT 进行对话

python - 将图像发送到 CouchDb 两次而不是 1 次

python - Jenkins 构建的 Django 应用程序的 Docker 容器中的错误时间

python - Django - 在图像编辑表单中删除 "Currently"并将其替换为实际图像

Python/Django 模型覆盖清理后的数据

python - 创建一个脚本,在一周的工作时间内以 15 分钟的间隔运行

python - python 列表中可以跳过索引吗?

javascript - django保护静态文件

python - Django 没有额外的表?