Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Registering New Users

00:00 Registering New Users. Your application can now handle all URLs related to Django user management, but one feature isn’t working yet. You may have noticed there’s no option to create a new user. Unfortunately, Django doesn’t provide user registration out of the box. You can, however, add it on your own. Django offers lots of forms that you can use in your projects.

00:29 One of them is UserCreationForm. It contains all the necessary fields to create a new user, but it doesn’t include an email field. In many applications, this might not be a problem, but you’ve already implemented a password reset feature.

00:45 Your users need to configure an email address or else they won’t be able to receive password reset emails. To fix that, you need to add your own user creation form, but don’t worry.

00:57 You can reuse almost the entire UserCreationForm. You just need to add the email field. Create a new Python file in the users directory called forms.py and add a custom form there.

01:24 As you can see, your CustomUserCreationForm extends Django’s UserCreationForm. The inner class Meta keeps additional information about the form and in this case extends UserCreationForm.Meta, so almost everything from Django’s form will be reused.

01:47 The key difference is the fields attribute, which determines the fields that will be included in the form. Your custom form will use all the fields from UserCreationForm and will add the email field.

02:00 Now the form is ready, create a new view called register.

02:27 If the view is displayed by a browser, then it will be accessed by a GET method. In that case, a template called users/register.html will be rendered.

02:37 The last argument of .render() is a context, which in this case contains your custom user creation form. If the form is submitted, then the view will be accessed by a POST method.

02:50 In that case, Django will attempt to create a user. A new CustomUserCreationForm is created using the values submitted to the form, which are contained in the request.POST object.

03:03 If the form is valid, then a new user is created on line 20 using form.save(). Then the user is logged in on line 21 using login(). Finally, line 22 redirects the user to the dashboard.

03:20 The template itself should look as seen on-screen.

03:38 This is very similar to the previous templates. As before, it takes the form from the context and renders it. The only difference is that this time you had to add the form to the context on your own, instead of letting Django do it.

03:57 Remember to add a URL for the registration view.

04:15 It’s also a good idea to add a link to the registration form on the login page.

04:28 Your newly created form should look as seen on-screen.

04:40 Please keep in mind that this is a bare-bones example of a registration form. In the real world, you would also display proper error messages if someone had tried to register an account that already exists. In addition, you would probably send emails with confirmation links after someone creates a user account. To do that, your site would need to be able to send emails to the outside world.

05:03 And in the next part of the course, that’s what you’ll be looking at.

Become a Member to join the conversation.