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")


[((27, 4, 27, 24), 'django.contrib.auth.logout', 'auth.logout', ({(27, 16, 27, 23): 'request'}, {}), '(request)', False, 'from django.contrib import auth\n'), ((50, 4, 50, 24), 'django.contrib.auth.logout', 'auth.logout', ({(50, 16, 50, 23): 'request'}, {}), '(request)', False, 'from django.contrib import auth\n'), ((51, 11, 51, 41), 'django.http.HttpResponseRedirect', 'HttpResponseRedirect', ({(51, 32, 51, 40): '"""/login"""'}, {}), "('/login')", False, 'from django.http import HttpResponseRedirect\n'), ((9, 16, 9, 29), 'django.template.context_processors.csrf', 'csrf', ({(9, 21, 9, 28): 'request'}, {}), '(request)', False, 'from django.template.context_processors import csrf\n'), ((15, 15, 15, 70), 'django.contrib.auth.authenticate', 'auth.authenticate', (), '', False, 'from django.contrib import auth\n'), ((23, 15, 23, 53), 'django.shortcuts.render_to_response', 'render_to_response', ({(23, 34, 23, 46): '"""login.html"""', (23, 48, 23, 52): 'args'}, {}), "('login.html', args)", False, 'from django.shortcuts import render, render_to_response, redirect\n'), ((30, 23, 30, 60), 'django.contrib.auth.forms.UserCreationForm', 'UserCreationForm', (), '', False, 'from django.contrib.auth.forms import UserCreationForm\n'), ((39, 23, 39, 41), 'django.contrib.auth.forms.UserCreationForm', 'UserCreationForm', ({}, {}), '()', False, 'from django.contrib.auth.forms import UserCreationForm\n'), ((17, 12, 17, 37), 'django.contrib.auth.login', 'auth.login', ({(17, 23, 17, 30): 'request', (17, 32, 17, 36): 'user'}, {}), '(request, user)', False, 'from django.contrib import auth\n'), ((18, 19, 18, 36), 'django.shortcuts.redirect', 'redirect', ({(18, 28, 18, 35): '"""/main"""'}, {}), "('/main')", False, 'from django.shortcuts import render, render_to_response, redirect\n'), ((21, 19, 21, 57), 'django.shortcuts.render_to_response', 'render_to_response', ({(21, 38, 21, 50): '"""login.html"""', (21, 52, 21, 56): 'args'}, {}), "('login.html', args)", False, 'from django.shortcuts import render, render_to_response, redirect\n'), ((33, 22, 33, 140), 'django.contrib.auth.authenticate', 'auth.authenticate', (), '', False, 'from django.contrib import auth\n'), ((34, 12, 34, 40), 'django.contrib.auth.login', 'auth.login', ({(34, 23, 34, 30): 'request', (34, 32, 34, 39): 'newuser'}, {}), '(request, newuser)', False, 'from django.contrib import auth\n'), ((35, 19, 35, 36), 'django.shortcuts.redirect', 'redirect', ({(35, 28, 35, 35): '"""/main"""'}, {}), "('/main')", False, 'from django.shortcuts import render, render_to_response, redirect\n'), ((47, 54, 47, 76), 'django.contrib.auth.get_user', 'auth.get_user', ({(47, 68, 47, 75): 'request'}, {}), '(request)', False, 'from django.contrib import auth\n')]