示例#1
0
def login_view(request):
    if request.user.is_authenticated:
        return redirect('posts:post-list')

    elif request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            form.login(request)
            return redirect('posts:post-list')
    else:
        form = LoginForm()

    login_base_url = 'https://nid.naver.com/oauth2.0/authorize'
    login_params = {
        'response_type': 'code',
        'client_id': SECRET['NAVER_CLIENT_ID'],
        'redirect_uri': 'http://localhost:8000/members/naver-login/',
        'state': 'RANDOM_STATE',
    }
    login_url = '{base}?{params}'.format(
        base=login_base_url,
        params='&'.join(
            [f'{key}={value}' for key, value in login_params.items()]))

    print(login_url)

    context = {'form': form, 'login_url': login_url}

    return render(request, 'members/login.html', context)
示例#2
0
def login_view(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            # is_valid() 이후에 self.cleaned_data 가 반환되었다
            form.login(request)
            return redirect('posts:post-list')
    else:
        # GET을 할때는 form 형식만 보내주면 되기 때문에 LoginForm 내부에 request.POST를 안넣어 줘도 된다.
        form = LoginForm()

    login_base_url = 'https://nid.naver.com/oauth2.0/authorize'
    login_params = {
        'response_type': 'code',
        'client_id': SECRETS["NAVER_CLIENT_ID"],
        'redirect_url': 'http://localhost:8000/members/naver-login/',
        'state': 'RANDOM_STATE',
    }
    login_url = '{base}?{params}'.format(
        base=login_base_url,
        params='&'.join(
            [f'{key}={value}' for key, value in login_params.items()]))

    context = {
        'form': form,
        'login_url': login_url,
    }
    return render(request, 'members/login.html', context)
示例#3
0
def login_view(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            form.login(request)
            return redirect('members:index')
    else:
        form = LoginForm()
    context = {
        'form': form,
    }
    return render(request, 'member/login.html', context)
示例#4
0
def login_view(request):
    # if request.method == 'POST':
    # username = request.POST['username']
    # password = request.POST['password']
    # # email = request.POST['email']
    # # name = request.POST['name']
    #
    # print(username)
    # print(password)
    # user = authenticate(request, username=username, password=password)
    # # user = authenticate( username=username, password=password)
    # # return value = kimdooh request없어도 됨
    # print(user)
    # if user is not None:
    #     login(request, user)
    #     return redirect('index')
    # else:
    #     return redirect('members:login')
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            form.login(request)
            return redirect('posts:post-list')

    else:
        # 입력했던 데이터 안날려먹으려고 이렇게 로직 구
        form = LoginForm()
    login_base_url = 'https://nid.naver.com/oauth2.0/authorize'
    login_params = {
        'response_type': 'code',
        'client_id': 'XgjJDOfau4a3p4w7excG',
        'redirect_uri': 'http://localhost:8000/members/naver-login/',
        'state': 'RANDOM_STATE',
    }
    login_url = '{base}?{params}'.format(
        base=login_base_url,
        params='&'.join(
            [f'{key}={value}' for key, value in login_params.items()]))
    context = {
        'form': form,
        'login_url': login_url,
    }

    return render(request, 'members/login.html', context)
示例#5
0
def login_view(request):
    """
    Template: templates/members/login.html
    POST 요청을 처리하는 form
    내부에는 input 2개를 가지며, 각각 username, password로 name을 가짐
    URL: /members/login/ (members.urls 사용, config.urls에 include해 사용)
    name: members:login (url namespace 사용)

    POST 요청시, 예제를 보고 적절히 로그인 처리한 후, index로 돌아갈 수 있도록 함
    """
    # if request.method == 'POST':
    #     username = request.POST['username']
    #     password = request.POST['password']
    #     user = authenticate(request, username=username, password=password)
    #     if user:
    #         login(request, user)
    #         # return render(request, 'index.html')
    #         # return redirect('index')
    #         return redirect('posts:post-list')
    #     else:
    #         return redirect('members:login')
    #         # return render(request, 'members/login.html')
    # else:
    #     return render(request, 'members/login.html')

    if request.method == 'POST':
        # username = request.POST['username']
        # password = request.POST['password']
        # user = authenticate(request, username=username, password=password)

        form = LoginForm(request.POST)

        if form.is_valid():
            # user = authenticate(request)
            # if user:
            #     login(request, user)
            #     return redirect('posts:post-list')
            # else:
            #     return redirect('members:login')

            form.login(request)
            return redirect('posts:post-list')
        # 아이디 혹은 비밀번호가 잘못되면 members:login 페이지로 다시 load
        # else:
        #     return redirect('members:login')
        #
        #     # form.errors가 살아있는 상태 (form 인스턴스가 유지되고 있음)
        #     # 아래 코드와 중복..
        #     context = {
        #         'form': form
        #     }
        #     return render(request, 'members/login.html', context)
    else:
        form = LoginForm()

    login_base_url = 'https://nid.naver.com/oauth2.0/authorize'

    login_params = {
        'response_type': 'code',
        'client_id': SECRETS['CLIENT_ID'],
        'redirect_uri': 'http://localhost:8000/members/naver-login/',
        'state': 'RANDOM_STATE'
    }

    login_url = '{base}?{params}'.format(
        base=login_base_url,
        params='&'.join(
            [f'{key}={value}' for key, value in login_params.items()]))
    print('login_url >> ', login_url)

    context = {'form': form, 'login_url': login_url}
    return render(request, 'members/login.html', context)