Django Signup Tutorial | [Link] [Link]
com/tutorials/django-signup-tutorial
Django Signup Tutorial
By Will Vincent Dec 8, 2022 9 Comments
Previously we added login and logout pages to our Django app. In this tutorial we'll create a sign up page so users can register for a new account.
The Django auth app provided us with built-in url and views for login and logout. All we needed to do was add a template for login. But to create a sign up page we will
need to make our own view and url. Let's begin!
Users app
Since we're making our own view and url for registration, we need to create a dedicated app. Let's call it accounts.
(accounts) $ python [Link] startapp accounts
Make sure to add the new app to the INSTALLED_APPS setting in our django_project/[Link] file:
# django_project/[Link]
INSTALLED_APPS = [
"[Link]",
"[Link]",
"[Link]",
"[Link]",
"[Link]",
"[Link]",
"accounts", # new
]
Then add a project-level url for the accounts app above our included Django auth app. Django will look top to bottom for url patterns so when it sees a url route within our
accounts app that matches one in the built-in auth app, it will choose the accounts route first.
# django_project/[Link]
from [Link] import admin
from [Link] import path, include
from [Link] import TemplateView
urlpatterns = [
path("admin/", [Link]),
path("accounts/", include("[Link]")), # new
path("accounts/", include("[Link]")),
path("", TemplateView.as_view(template_name="[Link]"), name="home"),
]
Create a new urls file in our accounts app. Note that we are importing a view called SignUp which we'll implement in the next section.
(accounts) $ touch accounts/[Link]
# accounts/[Link]
from [Link] import path
from .views import SignUpView
urlpatterns = [
path("signup/", SignUpView.as_view(), name="signup"),
]
Now for the [Link] file:
© LearnDjango | Django is a registered trademark of the Django Software Foundation.
1 of 5 18/12/22, 18:25
Django Signup Tutorial | [Link] [Link]
# accounts/[Link]
from [Link] import UserCreationForm
from [Link] import reverse_lazy
from [Link] import generic
class SignUpView([Link]):
form_class = UserCreationForm
success_url = reverse_lazy("login")
template_name = "registration/[Link]"
We're subclassing the generic class-based view CreateView in our SignUp class. We specify the use of the built-in UserCreationForm and the not-yet-created template at
[Link]. And we use reverse_lazy to redirect the user to the login page upon successful registration.
Why use reverse_lazy instead of reverse I hope you're asking? The reason is that for all generic class-based views the urls are not loaded when the file is imported, so we
have to use the lazy form of reverse to load them later when they're available.
Ok final step. Create a new template templates/registration/[Link] and populate it with this code that looks almost exactly like what we used for [Link].
<!-- templates/registration/[Link] -->
{% extends "[Link]" %}
{% block title %}Sign Up{% endblock %}
{% block content %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Sign Up</button>
</form>
{% endblock %}
And we're done! To confirm it all works, spin up our local server with python [Link] runserver and navigate to
[Link]
The extra text with tips on usernames and passwords comes from Django. We can customize that too but it requires a little more work and is beyond the scope of this
tutorial.
Sign up for a new account and hit the "Sign up" button. You will be redirected to the login page [Link] /login/ where you can log in with your new
account.
And then after a successful login you'll be redirect to the homepage with a personalized "Hi username!" greeting.
2 of 5 18/12/22, 18:25
Django Signup Tutorial | [Link] [Link]
Next Steps
We've successfully created a new sign up functionality to go alongside our existing login and logout. There's only one thing missing: add the ability for users to reset their
passwords. We'll cover this in part 3, Django Password Reset Tutorial.
Join My Newsletter
Subscribe to get the latest tutorials/writings by email.
Your email address
Subscribe
No spam. Unsubscribe at any time.
3 of 5 18/12/22, 18:25