lightstrike
lightstrike

Reputation: 399

Passing 'success_url' to CBV Django-Registration

I'd like to pass a success_url to the class-based ActivationView in Django Registration like this answer covers for function-based views and this answer covers for RegistrationView. What I have tried so far that has been unsuccessful:

url(r'^activate/(?P<activation_key>\w+)/$',
    ActivationView.as_view({'success_url':'/activation_routing'}),
    name='registration_activate',                              
    ),

returns "TypeError: as_view() takes exactly 1 argument (2 given)" I have also tried

and:

url(r'^activate/(?P<activation_key>\w+)/$',
    ActivationView.as_view(success_url='/activation_routing'),
    name='registration_activate',                              
    ),

returns "TypeError: ActivationView() received an invalid keyword 'success_url'. as_view only accepts arguments that are already attributes of the class."

I feel like I'm missing something with class-based views, or is subclassing ActivationView and putting in custom logic my best bet?

Upvotes: 1

Views: 1312

Answers (3)

lightstrike
lightstrike

Reputation: 399

As others confirmed, I was able to resolve this by subclassing ActivationView and overriding the get_success_url() and activate() methods:

"""
views.py
"""    

from registration.views import ActivationView  

class CustomActivation(ActivationView):                          
    def activate(self, request, *args, **kwargs):                  
        return True

    def get_success_url(self, request, user):
        success_url =  # insert success URL here 
        return success_url

It's also important to set the correct URL in your urls.py file to override the default ActivationView that will be called by django-registration. One quirk to remember is that django-registration will set its URLs according to the filepath of auth.urls and not what you specify in your app's urls.py file:

"""
urls.py
"""

from yourapp.views import CustomActivation

urlpatterns += patterns('',
    url(r'^user_settings/', include('auth.urls')),
    url(r'^user_settings/activate/(?P<activation_key>\w+)/$',
        CustomActivation.as_view(),
        name='registration_activate',
        ),
    # will still set registration URLs under user_settings!
    url(r'^accounts/', include('registration.backends.default.urls')),
)

Upvotes: 1

Alasdair
Alasdair

Reputation: 308999

I think you have to subclass the view and override the get_success_url method.

I opened pull request 57 to enable setting success_url as a class attribute, but it has not been merged yet.

Upvotes: 1

Simeon Visser
Simeon Visser

Reputation: 122456

You can indeed only pass existing attributes to as_view(). As such, and looking at the source of django-registration, the view doesn't have a success_url attribute but it obtains its value by calling self.get_success_url(...).

By default this method is not implemented so you have little choice besides subclassing ActivationView and implementing get_success_url yourself.

Upvotes: 1

Related Questions