from django.shortcuts import render
from django.http import HttpResponseRedirect
from .forms import SiteUserForm
from django.contrib.auth import login, authenticate


def index(request):
    return render(request, 'profile.html')


def register(request):
    if request.method == 'POST':
        form = SiteUserForm(request.POST)
        if form.is_valid():
            form.save()
            # # сразу аутентификация
            user = authenticate(username=form.username, password=form.password1)
            login(request, user)
            return HttpResponseRedirect('/account')
        else:
            return render(request, 'registration/register.html', {'form': form})
    else:
        form = SiteUserForm()
    return render(request, 'registration/register.html', {'form': form})


def profile(request):
    return render(request, 'profile.html')
