Пример #1
0
def login_register_view(request, register=False, modal=False):
    csrf_tk = {}
    csrf_tk.update(csrf(request))

    login_error = False
    if request.method == 'POST' and not register:
        login_form = LoginForm(request.POST)
        if login_form.is_valid():
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
            if user is not None:
                login(request, user)
                request.session['get_token'] = generate_token()
                if not 'remember' in request.POST:
                    request.session.set_expiry(0)
                if 'next' in request.POST:
                    return redirect(request.POST['next'] or '/')
                else:
                    return redirect('/')
            else:
                login_error = 'Bad user/password'
        else:
            if request.POST['username'] and request.POST['password']:
                login_error = 'Form invalid'
            else:
                login_error = 'Please enter username and password'

    login_form = LoginForm()
    register_form = RegisterForm()

    if request.method == 'POST' and register:
        register_form = RegisterForm(request.POST)
        if register_form.is_valid():
            data = register_form.data
            user = User.objects.create_user(
                data['username'],
                data['email'],
                data['password'])
            profile = Profile(user=user)
            profile.save()
            user.backend = 'django.contrib.auth.backends.ModelBackend'
            login(request, user)
            return redirect('/user/register/success')

    csrf_tk['register_form'] = register_form
    csrf_tk['login_error'] = login_error
    csrf_tk['login_form']  = login_form
    csrf_tk['register'] = register
    if 'next' in request.GET:
        csrf_tk['next']  = request.GET.get('next')
    if not modal:
        return render_template('user/login_register.html', csrf_tk)
    else:
        return render_template('user/login_register_modal.html', csrf_tk)
Пример #2
0
def login_view(request, modal=False):
    csrf_tk = {}
    csrf_tk.update(csrf(request))

    login_error = False
    if request.method == 'POST':
        login_form = LoginForm(request.POST)
        if login_form.is_valid():
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
            if user is not None:
                login(request, user)
                request.session['get_token'] = generate_token()
                if not 'remember' in request.POST:
                    request.session.set_expiry(0)
                if 'next' in request.POST:
                    return redirect(request.POST['next'] or '/')
                else:
                    return redirect('/')
            else:
                login_error = 'Bad user/password'
        else:
            if request.POST['username'] and request.POST['password']:
                login_error = 'Form invalid'
            else:
                login_error = 'Please enter username and password'

    next = '/'
    if 'next' in request.GET:
        next = request.GET['next']

    login_form = LoginForm(({'next': next}), )
    register_form = RegisterForm()

    csrf_tk['register_form'] = register_form
    csrf_tk['login_error'] = login_error
    csrf_tk['login_form'] = login_form
    if 'next' in request.GET:
        csrf_tk['next'] = request.GET.get('next')
    if not modal:
        return render_template('user/login.html', csrf_tk)
    else:
        return render_template('user/login_register_modal.html', csrf_tk)
Пример #3
0
def login_view(request, modal=False):
    csrf_tk = {}
    csrf_tk.update(csrf(request))

    login_error = False
    if request.method == 'POST':
        login_form = LoginForm(request.POST)
        if login_form.is_valid():
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
            if user is not None:
                login(request, user)
                request.session['get_token'] = generate_token()
                if not 'remember' in request.POST:
                    request.session.set_expiry(0)
                if 'next' in request.POST:
                    return redirect(request.POST['next'] or '/')
                else:
                    return redirect('/')
            else:
                login_error = 'Bad user/password'
        else:
            if request.POST['username'] and request.POST['password']:
                login_error = 'Form invalid'
            else:
                login_error = 'Please enter username and password'

    next = '/'
    if 'next' in request.GET:
        next = request.GET['next']

    login_form = LoginForm(({'next': next}),)
    register_form = RegisterForm()

    csrf_tk['register_form'] = register_form
    csrf_tk['login_error'] = login_error
    csrf_tk['login_form']  = login_form
    if 'next' in request.GET:
        csrf_tk['next']  = request.GET.get('next')
    if not modal:
        return render_template('user/login.html', csrf_tk)
    else:
        return render_template('user/login_register_modal.html', csrf_tk)
Пример #4
0
def login_view(request, modal=False):
    csrf_tk = {}
    csrf_tk.update(csrf(request))

    login_error = False
    if request.method == "POST":
        login_form = LoginForm(request.POST)
        if login_form.is_valid():
            username = request.POST["username"]
            password = request.POST["password"]
            user = authenticate(username=username, password=password)
            if user is not None:
                login(request, user)
                request.session["get_token"] = generate_token()
                if not "remember" in request.POST:
                    request.session.set_expiry(0)
                if "next" in request.POST:
                    return redirect(request.POST["next"] or "/")
                else:
                    return redirect("/")
            else:
                login_error = "Bad user/password"
        else:
            if request.POST["username"] and request.POST["password"]:
                login_error = "Form invalid"
            else:
                login_error = "Please enter username and password"

    login_form = LoginForm()
    register_form = RegisterForm()

    csrf_tk["register_form"] = register_form
    csrf_tk["login_error"] = login_error
    csrf_tk["login_form"] = login_form
    if "next" in request.GET:
        csrf_tk["next"] = request.GET.get("next")
    if not modal:
        return render_template("user/login.html", csrf_tk)
    else:
        return render_template("user/login_register_modal.html", csrf_tk)