from django.shortcuts import render, render_to_response, redirect
from django.contrib import auth
from django.contrib.auth.forms import UserCreationForm
from django.template.context_processors import csrf
from django.http import HttpResponseRedirect

def login(request):
    args = {}
    args.update(csrf(request))

    if request.POST:

        username = request.POST.get('username')
        password = request.POST.get('password')
        user = auth.authenticate(username=username, password=password)
        if user is not None:
            auth.login(request, user)
            return redirect('/main')
        else:
            args['login_error'] = "Пользователь не найден или пароль введен неверный пароль"
            return render_to_response('login.html', args)
    else:
        return render_to_response('login.html', args)


def reg(request):
    auth.logout(request)
    error = ''
    if request.method == "POST":
        newuser_form = UserCreationForm(data = request.POST)
        if newuser_form.is_valid():
            newuser_form.save()
            newuser = auth.authenticate(username = newuser_form.cleaned_data['username'], password = newuser_form.cleaned_data['password1'])
            auth.login(request, newuser)
            return redirect('/main')
        else:
            error = 'Проверьте правильность вводимых данных.'
    else:
        newuser_form = UserCreationForm()


    return render(request, 'reg.html', locals() )



def main(request):
    return render(request, 'index.html', {'username': auth.get_user(request).username} )

def logout(request):
    auth.logout(request)
    return HttpResponseRedirect("/login")


[((863, 883), 'django.contrib.auth.logout', 'auth.logout', (['request'], {}), '(request)\n', (874, 883), False, 'from django.contrib import auth\n'), ((1597, 1617), 'django.contrib.auth.logout', 'auth.logout', (['request'], {}), '(request)\n', (1608, 1617), False, 'from django.contrib import auth\n'), ((1629, 1659), 'django.http.HttpResponseRedirect', 'HttpResponseRedirect', (['"""/login"""'], {}), "('/login')\n", (1649, 1659), False, 'from django.http import HttpResponseRedirect\n'), ((301, 314), 'django.template.context_processors.csrf', 'csrf', (['request'], {}), '(request)\n', (305, 314), False, 'from django.template.context_processors import csrf\n'), ((450, 505), 'django.contrib.auth.authenticate', 'auth.authenticate', ([], {'username': 'username', 'password': 'password'}), '(username=username, password=password)\n', (467, 505), False, 'from django.contrib import auth\n'), ((800, 838), 'django.shortcuts.render_to_response', 'render_to_response', (['"""login.html"""', 'args'], {}), "('login.html', args)\n", (818, 838), False, 'from django.shortcuts import render, render_to_response, redirect\n'), ((955, 990), 'django.contrib.auth.forms.UserCreationForm', 'UserCreationForm', ([], {'data': 'request.POST'}), '(data=request.POST)\n', (971, 990), False, 'from django.contrib.auth.forms import UserCreationForm\n'), ((1389, 1407), 'django.contrib.auth.forms.UserCreationForm', 'UserCreationForm', ([], {}), '()\n', (1405, 1407), False, 'from django.contrib.auth.forms import UserCreationForm\n'), ((547, 572), 'django.contrib.auth.login', 'auth.login', (['request', 'user'], {}), '(request, user)\n', (557, 572), False, 'from django.contrib import auth\n'), ((592, 609), 'django.shortcuts.redirect', 'redirect', (['"""/main"""'], {}), "('/main')\n", (600, 609), False, 'from django.shortcuts import render, render_to_response, redirect\n'), ((736, 774), 'django.shortcuts.render_to_response', 'render_to_response', (['"""login.html"""', 'args'], {}), "('login.html', args)\n", (754, 774), False, 'from django.shortcuts import render, render_to_response, redirect\n'), ((1083, 1202), 'django.contrib.auth.authenticate', 'auth.authenticate', ([], {'username': "newuser_form.cleaned_data['username']", 'password': "newuser_form.cleaned_data['password1']"}), "(username=newuser_form.cleaned_data['username'], password=\n    newuser_form.cleaned_data['password1'])\n", (1100, 1202), False, 'from django.contrib import auth\n'), ((1214, 1242), 'django.contrib.auth.login', 'auth.login', (['request', 'newuser'], {}), '(request, newuser)\n', (1224, 1242), False, 'from django.contrib import auth\n'), ((1262, 1279), 'django.shortcuts.redirect', 'redirect', (['"""/main"""'], {}), "('/main')\n", (1270, 1279), False, 'from django.shortcuts import render, render_to_response, redirect\n'), ((1536, 1558), 'django.contrib.auth.get_user', 'auth.get_user', (['request'], {}), '(request)\n', (1549, 1558), False, 'from django.contrib import auth\n')]