我正在使用 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/