简体   繁体   中英

Django: How to pass in context variables for registration redux?

here is the views.py for registration redux. i am a little bit confused and i don't really understand how to pass in context variables.

class RegistrationView(BaseRegistrationView):
    SEND_ACTIVATION_EMAIL = getattr(settings, 'SEND_ACTIVATION_EMAIL', True)
    success_url = 'registration_complete'

    def register(self, request, form):
        if Site._meta.installed:
            site = Site.objects.get_current()
        else:
            site = RequestSite(request)

        if hasattr(form, 'save'):
            new_user_instance = form.save()
        else:
            new_user_instance = UserModel().objects.create_user(**form.cleaned_data)

        new_user = RegistrationProfile.objects.create_inactive_user(
            new_user=new_user_instance,
            site=site,
            send_email=self.SEND_ACTIVATION_EMAIL,
            request=request,
        )
        signals.user_registered.send(sender=self.__class__,
                                     user=new_user,
                                     request=request)
        return new_user

BaseRegistrationView inherits from FormView, which has in its inheritance chain django.views.generic.base.ContextMixin . This defines the get_context_data method, which returns context as a dict. You can override that method and add in your own variables like so:

class RegistrationView(BaseRegistrationView):
    ...
    def get_context_data(self, **kwargs):
        context = super(RegistrationView, self).get_context_data(**kwargs)
        context['key'] = value 
        return context

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM