question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sub-class RegistrationView doesn't take to registration_complete when successful

See original GitHub issue

I want to add a couple of fields to the Registration Form and have written the following:

class MyRegistrationView (RegistrationView):
    form_class = UserRegForm
    email_body_template = 'registration/activation_email.txt'
    email_subject_template = 'registration/activation_email_subject.txt'
    success_url = 'registration_complete'
    
    def register(self, form):
        new_user = self.create_inactive_user(form)
        signals.user_registered.send(sender=self.__class__,
                                     user=new_user,
                                     request=self.request)
        return new_user

    def get_success_url(self, user):
        return ('registration_complete', (), {})
        #return redirect('/accounts/register/complete/')

    def create_inactive_user(self, form):
        """
        Create the inactive user account and send an email containing
        activation instructions.

        """
        new_user = form.save(commit=False)
        new_user.is_active = False
        new_user.save()

        self.send_activation_email(new_user)

        return new_user

    def get_activation_key(self, user):
        """
        Generate the activation key which will be emailed to the user.

        """
        return signing.dumps(
            obj=getattr(user, user.USERNAME_FIELD),
            salt=REGISTRATION_SALT
        )

    def get_email_context(self, activation_key):
        """
        Build the template context used for the activation email.

        """
        return {
            'activation_key': activation_key,
            'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
            'site': get_current_site(self.request)
        }

    def send_activation_email(self, user):
        """
        Send the activation email. The activation key is simply the
        username, signed using TimestampSigner.

        """
        activation_key = self.get_activation_key(user)
        context = self.get_email_context(activation_key)
        context.update({
            'user': user
        })
        subject = render_to_string(self.email_subject_template,
                                   context)
        # Force subject to a single line to avoid header-injection
        # issues.
        subject = ''.join(subject.splitlines())
        message = render_to_string(self.email_body_template,
                                   context)
        user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)

This is basically a copy of RegistrationView but I’ve written it so I can use this in the urls.

url(r'^accounts/register', views.MyRegistrationView.as_view(form_class=UserRegForm), name='registration_register'),

I’ve also written a regbackend.py but I don’t think you need it here. The registration works but on success it is going back to the registration form. No errors are showing on the form so I do not understand what is happening. Have I missed something?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
HenryMehtacommented, Sep 5, 2017

Change url(r'^accounts/register/', MyRegistrationView.as_view()), to url(r'^accounts/register/$', MyRegistrationView.as_view()),

0reactions
philejmathcommented, Sep 5, 2017

I created a custom view class to handle user checking authentication before viewing registration page.

class MyRegistrationView(RegistrationView):
    disallowed_url = 'recruiter:user_config'

    def registration_allowed(self):
        if self.request.user.is_authenticated():
            return False
        else:
            return True

Adding this view to urls.py url(r'^accounts/register/', MyRegistrationView.as_view()),

Causes the /account/register and /account/register/complete/ to both show the registration form.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Base view classes — django-registration 2.0.4 documentation
Since it's a subclass of FormView , RegistrationView has all the usual attributes and methods you ... The URL to redirect to after...
Read more >
The New Jersey SREC Solar Registration Program (SRP ...
The purpose of this document is to provide participating contractors with instructions on how to Log-in to the.
Read more >
Professional Cairngorm (Wrox Programmer to Programmer)
Programmer to Programmer™ Get more out of WROX.com Interact Chapters on Demand Take an active role online by partici... Author: Jeremy Wischusen ...
Read more >
Hello web app: intro to web app development using Python and ...
If you're not, it won't take long to get there. ... RegistrationView 2 3 # my new registration view, subclassing RegistrationView 4 #...
Read more >
How can I get kwargs values in redirected view
In your registration view: ... can redirect the user to the anycodings_django view and simply use request.user.email anycodings_django to access the email.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found