python - 扩展 django-inspectional-registration 中的 View

标签 python django python-2.7 django-registration

我正在使用 django-inspectional-registration,我想扩展 RegistrationView

class RegistrationView(FormMixin, TemplateResponseMixin, ProcessFormView):
"""A complex view for registration

GET:
    Display an RegistrationForm which has ``username``, ``email1`` and ``email2``
    for registration.
    ``email1`` and ``email2`` should be equal to prepend typo.

    ``form`` and ``supplement_form`` is in context to display these form.

POST:
    Register the user with passed ``username`` and ``email1``
"""
template_name = r'registration/registration_form.html'

def __init__(self, *args, **kwargs):
    self.backend = get_backend()
    super(RegistrationView, self).__init__(*args, **kwargs)

我在我的views.py中执行了以下操作:

def extended_registration(request, *args, **kwargs):
    k = 1+1

    return RegistrationView(request, *args, **kwargs)

RegistrationView = extended_registration(RegistrationView)

似乎创建的装饰器正在工作,但我得到:

Traceback:
File "/Users/my_environment/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  92.                     response = middleware_method(request)
File "/Users/my_environment/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/Users/myProject/urls.py" in <module>
  5. from myProject.views import *
File "/Users/myProject/views.py" in <module>
  92. RegistrationView = extended_registration(RegistrationView)
File "/Users/myProject/views.py" in extended_registration
  90.     return RegistrationView(request, *args, **kwargs)
File "/Users/myProject/registration/views.py" in __init__
  157.         super(RegistrationView, self).__init__(*args, **kwargs)

Exception Type: TypeError at /accounts/register/complete/
Exception Value: __init__() takes exactly 1 argument (2 given)

最佳答案

您可以使用继承来扩展基于类的 View :

class MyRegistrationView(RegistrationView):
    def __init__(self, *args, **kwargs):
        k = 1 + 1
        super(MyRegistrationView, self).__init__(*args, **kwargs)

然后定义自定义路由:

url(r'^registration/register/$', MyRegistrationView.as_view(),
    name='registration_register'),
url('^registration/', include('registration.urls')),

顺序很重要!我们的自定义路由需要在注册应用路由之前定义。

关于python - 扩展 django-inspectional-registration 中的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19187539/

相关文章:

django - 使用 django-allauth 注销消息并重定向到主页

python - Django ValueError 位于/admin/

python - python中的方法委托(delegate)

python - 如何从 nltk 分类器中获取精度和召回率?

Python 日志记录 : propagate messages of level below current logger level

python - tensorflow 中的字符串连接

python - map 或列表理解实现比通过循环调用函数的性能提升从何而来?

python - Numpy.where() 在其条件中包含一个数组

python - 收到意外的关键字参数 'pk'

python - 从另一个目录导入文件