Django-Registration Activation redirect with django.contrib.messages

旧巷老猫 提交于 2019-12-01 09:24:24

问题


I'm trying to set up my django-registration activation workflow so that when the user hits the activation link it redirects them to the login page with a nice little message using the django messages framework django.contrib.messages

Right now, I've managed to send the user back to the homepage using the success_url parameter:

 url(r'activate/(?P<activation_key>\w+)/$',
    activate,
    {'backend': 'registration.backends.default.DefaultBackend', 'success_url':'/'},
        name='registration_activate',
    ),

where '/' is the home login view. I need to set the success message somewhere along the way...perhaps using the extra_context field?


回答1:


Django-registration is using signals to hook on some points. In your case it should be something like:

from registration import signals
def register_handler(request, **kwargs):
    messages.success(request, 'Thank you!')
signals.user_registered.connect(register_handler)



回答2:


ilvar's response is probably a better way to do it, but I also managed to get it working by wrapping a view around django-registration view.

In urls.py I now point to my new view

url(r'^accounts/activate/(?P<activation_key>\w+)/$',
        Custom_Activation_View.as_view(),
        {'backend': 'registration.backends.default.DefaultBackend'},
        name='registration_activate'),

and in my views.py file:

    class Custom_Activation_View(TemplateView):
        template_name='home.html'

        def get(self, request, backend, success_url=None, extra_context=None, **kwargs):
            messages.success(self.request, 'Activation complete, please login below')

            return activate(self.request, backend, template_name=self.template_name, success_url='/', extra_context=None, **kwargs)


来源:https://stackoverflow.com/questions/10360098/django-registration-activation-redirect-with-django-contrib-messages

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