How to use different view for django-registration?

房东的猫 提交于 2019-12-30 06:58:03

问题


I have been trying to get django-registration to use the view RegistrationFormUniqueEmail and following the solution from this django-registration question. I have set my urls.py to

from django.conf.urls import patterns, include, url

from registration.forms import RegistrationFormUniqueEmail

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    (r'^users/', include('registration.backends.default.urls')),
    url(r'^users/register/$', 'registration.backends.default.views.RegistrationView',
        {'form_class': RegistrationFormUniqueEmail,
         'backend': 'registration.backends.default.DefaultBackend'},       
        name='registration_register'),
)

However, I can still create multiple accounts with the same email. What is the problem? Shouldn't django-registration be using the view that I specified? I am currently using django-registration 0.9b1.


回答1:


The version of Django registration you are using has been rewritten to use class based views. This means a different approach is required in your urls.py.

First, You need to subclass the RegistrationView, and set the custom form class.

from registration.backends.default.views import RegistrationView
from registration.forms import RegistrationFormUniqueEmail

class RegistrationViewUniqueEmail(RegistrationView):
    form_class = RegistrationFormUniqueEmail

Then, use your custom RegistrationViewUniqueEmail subclass in your urls. As with other class based views, you must call as_view().

url(r'^user/register/$', RegistrationViewUniqueEmail.as_view(),
                    name='registration_register'),

Make sure your customised registration_register view comes before you include the default registration urls, otherwise it won't be used.




回答2:


The version 1.2 of django-registration-redux allows the unique email option with the following urls.py patterns:

url(r'^accounts/register/$', RegistrationView.as_view(form_class=RegistrationFormUniqueEmail), name='registration_register'),
url(r'^accounts/', include('registration.backends.default.urls')),

If you need to do something more, like a specific URL option, you can subclass the RegistrationView in your app views.py and RegistrationForm in your app forms.py



来源:https://stackoverflow.com/questions/16379300/how-to-use-different-view-for-django-registration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!