1
0
mirror of https://github.com/Microsoft/sql-server-samples.git synced 2025-12-08 14:58:54 +00:00
Files
sql-server-samples/samples/development-frameworks/django/bootcamp/authentication/views.py
2016-11-08 13:10:49 -08:00

32 lines
1.2 KiB
Python

from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from bootcamp.authentication.forms import SignUpForm
from django.contrib.auth.models import User
from bootcamp.feeds.models import Feed
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if not form.is_valid():
return render(request, 'authentication/signup.html',
{'form': form})
else:
username = form.cleaned_data.get('username')
email = form.cleaned_data.get('email')
password = form.cleaned_data.get('password')
User.objects.create_user(username=username, password=password,
email=email)
user = authenticate(username=username, password=password)
login(request, user)
welcome_post = u'{0} has joined the network.'.format(user.username,
user.username)
feed = Feed(user=user, post=welcome_post)
feed.save()
return redirect('/')
else:
return render(request, 'authentication/signup.html',
{'form': SignUpForm()})