We need to create a Django project and a Django app:
django-admin startproject registrationcd registrationpython manage.py startapp signup_login
We now have a project named registration and an app named signup_login
signup_login/apps.py
from __future__ import unicode_literalsfrom django.apps import AppConfigclass CoreConfig(AppConfig):name = 'core'
signup_login/views.py
from django.contrib.auth.decorators import login_requiredfrom django.contrib.auth import login, authenticatefrom django.contrib.auth.forms import UserCreationFormfrom django.shortcuts import render, redirect@login_requireddef home(request):return render(request, 'home.html')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})
settings.py
INSTALLED_APPS = ['django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','signup_login.core',]MIDDLEWARE = ['django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware',]
urls.py
from django.conf.urls import url, includefrom django.contrib.auth import views as auth_viewsfrom signup_login.core import views as core_viewsurlpatterns = [url(r'^$', core_views.home, name='home'),url(r'^login/$', auth_views.LoginView.as_view(template_name="login.html"), name="login"),url(r'^logout/$', auth_views.LogoutView.as_view(next_page="login"), name='logout'),url(r'^signup/$', core_views.signup, name='signup'),]
templates/static.html
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8"></head>
<body>
<header>
<h1>My Site</h1>{% if user.is_authenticated %}<a href="{% url 'logout' %}">logout</a>{% else %}<a href="{% url 'login' %}">login</a> / <a href="{% url 'signup' %}">signup</a>{% endif %}<hr>
</header>
<main>
{% block content %}{% endblock %}</main>
</body>
</html>
templates/home.html
{% extends 'static.html' %}{% block content %}<h2>Welcome, {{ user.username }}!</h2>{% endblock %}
templates/login.html
{% extends 'static.html' %}{% block content %}<h2>Log in to My Site</h2>{% if form.errors %}<p style="color: red">Your username and password didn't match. Please try again.</p>{% endif %}<form method="post">{% csrf_token %}<input type="hidden" name="next" value="{{ next }}" />{% for field in form %}<p>
{{ field.label_tag }}<br>
{{ field }}<br>
{% for error in field.errors %}<p style="color: red">{{ error }}</p>{% endfor %}{% if field.help_text %}<p><small style="color: grey">{{ field.help_text }}</small></p>{% endif %}</p>
{% endfor %}<button type="submit">Log in</button><a href="{% url 'signup' %}">New to My Site? Sign up</a></form>
{% endblock %}
templates/signup.html
{% extends 'static.html' %}{% block content %}<h2>Sign up</h2><form method="post">{% csrf_token %}{% for field in form %}<p>
{{ field.label_tag }}<br>
{{ field }}{% if field.help_text %}<small style="color: grey">{{ field.help_text }}</small>{% endif %}{% for error in field.errors %}<p style="color: red">{{ error }}</p>{% endfor %}</p>
{% endfor %}<button type="submit">Sign up</button></form>
{% endblock %}
Our Django site is ready. Start it like this:
python manage.py runserver
And then visit http://localhost:8000/ in your browser