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, 'django.contrib.auth.logout', 'auth.logout', False, 'from django.contrib import auth\n'), (50, 'django.contrib.auth.logout', 'auth.logout', False, 'from django.contrib import auth\n'), (51, 'django.http.HttpResponseRedirect', 'HttpResponseRedirect', False, 'from django.http import HttpResponseRedirect\n'), (9, 'django.template.context_processors.csrf', 'csrf', False, 'from django.template.context_processors import csrf\n'), (15, 'django.contrib.auth.authenticate', 'auth.authenticate', False, 'from django.contrib import auth\n'), (23, 'django.shortcuts.render_to_response', 'render_to_response', False, 'from django.shortcuts import render, render_to_response, redirect\n'), (30, 'django.contrib.auth.forms.UserCreationForm', 'UserCreationForm', False, 'from django.contrib.auth.forms import UserCreationForm\n'), (39, 'django.contrib.auth.forms.UserCreationForm', 'UserCreationForm', False, 'from django.contrib.auth.forms import UserCreationForm\n'), (17, 'django.contrib.auth.login', 'auth.login', False, 'from django.contrib import auth\n'), (18, 'django.shortcuts.redirect', 'redirect', False, 'from django.shortcuts import render, render_to_response, redirect\n'), (21, 'django.shortcuts.render_to_response', 'render_to_response', False, 'from django.shortcuts import render, render_to_response, redirect\n'), (33, 'django.contrib.auth.authenticate', 'auth.authenticate', False, 'from django.contrib import auth\n'), (34, 'django.contrib.auth.login', 'auth.login', False, 'from django.contrib import auth\n'), (35, 'django.shortcuts.redirect', 'redirect', False, 'from django.shortcuts import render, render_to_response, redirect\n'), (47, 'django.contrib.auth.get_user', 'auth.get_user', False, 'from django.contrib import auth\n')]