from django.shortcuts import render, redirect

# Create your views here.
# coding:utf-8
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse

from django.contrib import auth
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
from django.contrib.auth.decorators import login_required

# 第四个是 auth中用户权限有关的类。auth可以设置每个用户的权限。
from django.views.decorators.csrf import csrf_exempt


def index(request):
    return HttpResponse(u"django很有趣!")


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


def list(request):
    list = ['HTML', 'JAVA', 'JS', 'PYTHON', 'C++']
    return render(request, 'home.html', {'list': list})


def add(request):
    # request.GET.get('a', 0)
    a = request.GET['a']
    b = request.GET['b']
    c = int(a) + int(b)
    return HttpResponse(str(c))


def addRest(request, a, b):
    c = int(a) + int(b);
    return HttpResponse(str(c))


# 转发
def old_add2_redirect(request, a, b):
    return HttpResponseRedirect(
        reverse('add2', args=(a, b))
    )


from .forms import UserForm


# 注册
@csrf_exempt
def register_view(req):
    context = {}
    if req.method == 'POST':
        form = UserForm(req.POST)
        if form.is_valid():
            # 获得表单数据
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']

            # 判断用户是否存在
            user = auth.authenticate(username=username, password=password)
            if user:
                context['userExit'] = True
                return render(req, 'register.html', context)

            # 添加到数据库（还可以加一些字段的处理）
            user = User.objects.create_user(username=username, password=password)
            user.save()

            # 添加到session
            req.session['username'] = username
            # 调用auth登录
            auth.login(req, user)
            # 重定向到首页
            return redirect('/')
    else:
        context = {'isLogin': False}
    # 将req 、页面 、以及context{}（要传入html文件中的内容包含在字典里）返回
    return render(req, 'register.html', context)


# 登陆
@csrf_exempt
def login_view(req):
    context = {}
    if req.method == 'POST':
        form = UserForm(req.POST)
        if form.is_valid():
            # 获取表单用户密码
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']

            # 获取的表单数据与数据库进行比较
            user = authenticate(username=username, password=password)
            if user:
                # 比较成功，跳转index
                auth.login(req, user)
                req.session['username'] = username
                return redirect("list/")
            else:
                # 比较失败，还在login
                context = {'isLogin': False, 'pawd': False}
                return render(req, 'login.html', context)
    else:
        context = {'isLogin': False, 'pswd': True,'site_header':'我的登录','site_title':'django不错'}
    return render(req, 'login.html', context)


# 登出
def logout_view(req):
    # 清理cookie里保存username
    auth.logout(req)
    return redirect('/')

e88769afa7821de46dae3290b9cfe8e6aa52a227
<mask token>


def list(request):
    list = ['HTML', 'JAVA', 'JS', 'PYTHON', 'C++']
    return render(request, 'home.html', {'list': list})


def add(request):
    a = request.GET['a']
    b = request.GET['b']
    c = int(a) + int(b)
    return HttpResponse(str(c))


<mask token>


def old_add2_redirect(request, a, b):
    return HttpResponseRedirect(reverse('add2', args=(a, b)))


<mask token>


@csrf_exempt
def register_view(req):
    context = {}
    if req.method == 'POST':
        form = UserForm(req.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = auth.authenticate(username=username, password=password)
            if user:
                context['userExit'] = True
                return render(req, 'register.html', context)
            user = User.objects.create_user(username=username, password=
                password)
            user.save()
            req.session['username'] = username
            auth.login(req, user)
            return redirect('/')
    else:
        context = {'isLogin': False}
    return render(req, 'register.html', context)


@csrf_exempt
def login_view(req):
    context = {}
    if req.method == 'POST':
        form = UserForm(req.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = authenticate(username=username, password=password)
            if user:
                auth.login(req, user)
                req.session['username'] = username
                return redirect('list/')
            else:
                context = {'isLogin': False, 'pawd': False}
                return render(req, 'login.html', context)
    else:
        context = {'isLogin': False, 'pswd': True, 'site_header': '我的登录',
            'site_title': 'django不错'}
    return render(req, 'login.html', context)


<mask token>

<mask token>


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


def list(request):
    list = ['HTML', 'JAVA', 'JS', 'PYTHON', 'C++']
    return render(request, 'home.html', {'list': list})


def add(request):
    a = request.GET['a']
    b = request.GET['b']
    c = int(a) + int(b)
    return HttpResponse(str(c))


<mask token>


def old_add2_redirect(request, a, b):
    return HttpResponseRedirect(reverse('add2', args=(a, b)))


<mask token>


@csrf_exempt
def register_view(req):
    context = {}
    if req.method == 'POST':
        form = UserForm(req.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = auth.authenticate(username=username, password=password)
            if user:
                context['userExit'] = True
                return render(req, 'register.html', context)
            user = User.objects.create_user(username=username, password=
                password)
            user.save()
            req.session['username'] = username
            auth.login(req, user)
            return redirect('/')
    else:
        context = {'isLogin': False}
    return render(req, 'register.html', context)


@csrf_exempt
def login_view(req):
    context = {}
    if req.method == 'POST':
        form = UserForm(req.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = authenticate(username=username, password=password)
            if user:
                auth.login(req, user)
                req.session['username'] = username
                return redirect('list/')
            else:
                context = {'isLogin': False, 'pawd': False}
                return render(req, 'login.html', context)
    else:
        context = {'isLogin': False, 'pswd': True, 'site_header': '我的登录',
            'site_title': 'django不错'}
    return render(req, 'login.html', context)


<mask token>

<mask token>


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


def list(request):
    list = ['HTML', 'JAVA', 'JS', 'PYTHON', 'C++']
    return render(request, 'home.html', {'list': list})


def add(request):
    a = request.GET['a']
    b = request.GET['b']
    c = int(a) + int(b)
    return HttpResponse(str(c))


<mask token>


def old_add2_redirect(request, a, b):
    return HttpResponseRedirect(reverse('add2', args=(a, b)))


<mask token>


@csrf_exempt
def register_view(req):
    context = {}
    if req.method == 'POST':
        form = UserForm(req.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = auth.authenticate(username=username, password=password)
            if user:
                context['userExit'] = True
                return render(req, 'register.html', context)
            user = User.objects.create_user(username=username, password=
                password)
            user.save()
            req.session['username'] = username
            auth.login(req, user)
            return redirect('/')
    else:
        context = {'isLogin': False}
    return render(req, 'register.html', context)


@csrf_exempt
def login_view(req):
    context = {}
    if req.method == 'POST':
        form = UserForm(req.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = authenticate(username=username, password=password)
            if user:
                auth.login(req, user)
                req.session['username'] = username
                return redirect('list/')
            else:
                context = {'isLogin': False, 'pawd': False}
                return render(req, 'login.html', context)
    else:
        context = {'isLogin': False, 'pswd': True, 'site_header': '我的登录',
            'site_title': 'django不错'}
    return render(req, 'login.html', context)


def logout_view(req):
    auth.logout(req)
    return redirect('/')

<mask token>


def index(request):
    return HttpResponse(u'django很有趣!')


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


def list(request):
    list = ['HTML', 'JAVA', 'JS', 'PYTHON', 'C++']
    return render(request, 'home.html', {'list': list})


def add(request):
    a = request.GET['a']
    b = request.GET['b']
    c = int(a) + int(b)
    return HttpResponse(str(c))


def addRest(request, a, b):
    c = int(a) + int(b)
    return HttpResponse(str(c))


def old_add2_redirect(request, a, b):
    return HttpResponseRedirect(reverse('add2', args=(a, b)))


<mask token>


@csrf_exempt
def register_view(req):
    context = {}
    if req.method == 'POST':
        form = UserForm(req.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = auth.authenticate(username=username, password=password)
            if user:
                context['userExit'] = True
                return render(req, 'register.html', context)
            user = User.objects.create_user(username=username, password=
                password)
            user.save()
            req.session['username'] = username
            auth.login(req, user)
            return redirect('/')
    else:
        context = {'isLogin': False}
    return render(req, 'register.html', context)


@csrf_exempt
def login_view(req):
    context = {}
    if req.method == 'POST':
        form = UserForm(req.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = authenticate(username=username, password=password)
            if user:
                auth.login(req, user)
                req.session['username'] = username
                return redirect('list/')
            else:
                context = {'isLogin': False, 'pawd': False}
                return render(req, 'login.html', context)
    else:
        context = {'isLogin': False, 'pswd': True, 'site_header': '我的登录',
            'site_title': 'django不错'}
    return render(req, 'login.html', context)


def logout_view(req):
    auth.logout(req)
    return redirect('/')

from django.shortcuts import render, redirect

# Create your views here.
# coding:utf-8
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse

from django.contrib import auth
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
from django.contrib.auth.decorators import login_required

# 第四个是 auth中用户权限有关的类。auth可以设置每个用户的权限。
from django.views.decorators.csrf import csrf_exempt


def index(request):
    return HttpResponse(u"django很有趣!")


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


def list(request):
    list = ['HTML', 'JAVA', 'JS', 'PYTHON', 'C++']
    return render(request, 'home.html', {'list': list})


def add(request):
    # request.GET.get('a', 0)
    a = request.GET['a']
    b = request.GET['b']
    c = int(a) + int(b)
    return HttpResponse(str(c))


def addRest(request, a, b):
    c = int(a) + int(b);
    return HttpResponse(str(c))


# 转发
def old_add2_redirect(request, a, b):
    return HttpResponseRedirect(
        reverse('add2', args=(a, b))
    )


from .forms import UserForm


# 注册
@csrf_exempt
def register_view(req):
    context = {}
    if req.method == 'POST':
        form = UserForm(req.POST)
        if form.is_valid():
            # 获得表单数据
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']

            # 判断用户是否存在
            user = auth.authenticate(username=username, password=password)
            if user:
                context['userExit'] = True
                return render(req, 'register.html', context)

            # 添加到数据库（还可以加一些字段的处理）
            user = User.objects.create_user(username=username, password=password)
            user.save()

            # 添加到session
            req.session['username'] = username
            # 调用auth登录
            auth.login(req, user)
            # 重定向到首页
            return redirect('/')
    else:
        context = {'isLogin': False}
    # 将req 、页面 、以及context{}（要传入html文件中的内容包含在字典里）返回
    return render(req, 'register.html', context)


# 登陆
@csrf_exempt
def login_view(req):
    context = {}
    if req.method == 'POST':
        form = UserForm(req.POST)
        if form.is_valid():
            # 获取表单用户密码
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']

            # 获取的表单数据与数据库进行比较
            user = authenticate(username=username, password=password)
            if user:
                # 比较成功，跳转index
                auth.login(req, user)
                req.session['username'] = username
                return redirect("list/")
            else:
                # 比较失败，还在login
                context = {'isLogin': False, 'pawd': False}
                return render(req, 'login.html', context)
    else:
        context = {'isLogin': False, 'pswd': True,'site_header':'我的登录','site_title':'django不错'}
    return render(req, 'login.html', context)


# 登出
def logout_view(req):
    # 清理cookie里保存username
    auth.logout(req)
    return redirect('/')
