Python - Django Simple Sign up Form
Submitted by nurhodelta_17 on Tuesday, October 24, 2017 - 22:02.
In my previous tutorial, I have discussed about Creating a Simple Login and Logout. As a continuation, we are going to discuss user registration using the built in user table and registration form.
home.html
logout.html
Getting Started
We're going to use our files in the previous tutorial and create a new app named account then install the app. If you have no idea how to create and install an app, refer to my tutorial about Creating and Installing New App.Setting up our URLs
Next, we're gonna update our URLs to accommodate our signup. Go to our main directory, dproject, open urls.py then update it with the ff. codes:- from django.conf.urls import url
- from django.contrib import admin
- from django.contrib.auth import views as auth_views
- from django.views.generic.base import TemplateView
- from account import views as account_views
- urlpatterns = [
- url(r'^admin/', admin.site.urls),
- url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
- url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
- url(r'^logout/$', auth_views.logout, {'template_name': 'logged_out.html'}, name='logout'),
- url(r'^signup/$', account_views.signup, name='signup'),
- ]
Creating our Views
We now then create the view for our sign up. Open our account app that we have created earlier then update views.py with the ff codes:- from django.contrib.auth import login, authenticate
- from django.contrib.auth.forms import UserCreationForm
- from django.shortcuts import render, redirect
- def signup(request):
- if request.method == 'POST':
- form = UserCreationForm(request.POST)
- if form.is_valid():
- form.save()
- username = form.cleaned_data.get('username')
- raw_password = form.cleaned_data.get('password1')
- user = authenticate(username=username, password=raw_password)
- login(request, user)
- return redirect('home')
- else:
- form = UserCreationForm()
- return render(request, 'signup.html', {'form': form})
Creating our signup.html
We now create our signup form and we're gonna create it inside our templates directory. We're gonna name this as signup.html.Updating our Pages
We now then update our pages to accommodate our signup form. base.html- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- {% load staticfiles %}
- <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}" type="text/css">
- </head>
- <body>
- <div class="container">
- <header>
- </header>
- <hr>
- <main>
- {% block content %}
- {% endblock %}
- </main>
- <hr>
- </div>
- </body>
- </html>
- {% extends 'base.html' %}
- {% block title %}Login{% endblock %}
- {% block content %}
- {% if user.is_authenticated %}
- Hi {{ user.username }}!
- {% else %}
- {% endif %}
- {% endblock %}
Running our Server
Lastly, we run our server to check if our signup is working. To run the server, in the command prompt, type: python manage.py runserver That ends this tutorial. Happy Coding :)Add new comment
- 375 views