17. 로그인 - eungyukm/DjangoBase GitHub Wiki

로그인

  1. 로그인 실패했을 경우 처리

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>미니 프로젝트</title>
<!-- Bootstrap CDN -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
</head>
<body>

{% include 'top_menu.html' %}

<div class="container" style="margin-top:100px">
	<div class="row">
		<div class="col-sm-3"></div>
		<div class="col-sm-6">
			<div class="card shadow">
				<div class="card-body">
					{% if login_chk == '1'%}
					<div class="alert alert-danger">
						<h3>로그인 실패</h3>
					 	<p>아이디 비밀번호를 확인해주세요</p>
					</div>
					{% endif %}
					<form action="index.html" method="post">
						<div class="form-group">
							<label for="user_id">아이디</label>
							<input type="text" id="user_id" name="user_id" class="form-control"/>
						</div>
						<div class="form-group">
							<label for="user_pw">비밀번호</label>
							<input type="password" id="user_pw" name="user_pw" class="form-control"/>
						</div>
						<div class="form-group text-right">
							<button type="submit" class="btn btn-primary">로그인</button>
							<a href="join.html" class="btn btn-danger">회원가입</a>
						</div>
					</form>
				</div>
			</div>
		</div>
		<div class="col-sm-3"></div>
	</div>
</div>

{% include 'bottom_info.html' %}

</body>
</html>
  1. login을 체크하는 함수 추가
from django.shortcuts import render
from django.http import HttpRequest, HttpResponse
from django.template import loader
from django.views.decorators.csrf import csrf_exempt
import user_app.models

# user/join
def join(request) :
    template = loader.get_template('join.html')
    return HttpResponse(template.render())

# user/login
def login(request) :
    # 로그인 확인 여부를 나타내는 파라미터를 추출합니다.
    # 지정된 파라미터가 없을 수 도 있다면 get함수를 사용합니다.
    login_chk = request.GET.get('login_chk')
    # print(login_chk)

    render_data = {
        'login_chk' : login_chk
    }

    template = loader.get_template('login.html')
    return HttpResponse(template.render(render_data, request) )

# user/modify_user
def modify_user(request) :
    template = loader.get_template('modify_user.html')
    return HttpResponse(template.render()) 

# user/join_result
# post 요청시에는 csrf 토큰을 사용하게 된다.
# 이럴 경우 함수에 csrf_exempt라는 데코레이트를 설정해야 합니다.
@csrf_exempt
def join_result(request) :
    # print(request.POST['user_name']) 
    # print(request.POST['user_id'])
    # print(request.POST['user_pw'] )

    # 파라미터 데이터를 추출합니다.
    user_name = request.POST['user_name']
    user_id = request.POST['user_id']
    user_pw = request.POST['user_pw'] 

    # 데이터 저장 처리
    user_model = user_app.models.UserTable()
    user_model.user_name = user_name
    user_model.user_id = user_id
    user_model.user_pw = user_pw
    user_model.save() 

    confirm = '''
                <script>
                    alert('가입이 완료되었습니다')
                    location.href = '/user/login'
                </script>
            ''' 

    return HttpResponse(confirm)
  1. 로그인 시 csrf 처리를 합니다.
<form action="/user/login_result" method="post">
{% csrf_token  %}
  1. urls.py파일에 처리
from django.urls import path
from . import views


urlpatterns = [
    path('join', views.join, name='join'),
    path('login', views.login, name='login'),
    path('modify_user', views.modify_user, name='modify_user'),
    path('join_result', views.join_result, name='join_result'),
    path('login_result', views.login_result, name='login_result'),
]
  1. user id pw 체크
@csrf_exempt
def login_result(requeest) :
    # 사용자가 입력한 파라미터를 추출합니다.
    user_id = requeest.POST['user_id']
    user_pw = requeest.POST['user_pw']

    print(user_id)
    print(user_pw)
  1. 로그인 전체 처리
from django.shortcuts import render
from django.http import HttpRequest, HttpResponse
from django.template import loader
from django.views.decorators.csrf import csrf_exempt
import user_app.models

# user/join
def join(request) :
    template = loader.get_template('join.html')
    return HttpResponse(template.render())

# user/login
def login(request) :
    # 로그인 확인 여부를 나타내는 파라미터를 추출합니다.
    # 지정된 파라미터가 없을 수 도 있다면 get함수를 사용합니다.
    login_chk = request.GET.get('login_chk')
    # print(login_chk)

    render_data = {
        'login_chk' : login_chk
    }

    template = loader.get_template('login.html')
    return HttpResponse(template.render(render_data, request) )

# user/modify_user
def modify_user(request) :
    template = loader.get_template('modify_user.html')
    return HttpResponse(template.render()) 

# user/join_result
# post 요청시에는 csrf 토큰을 사용하게 된다.
# 이럴 경우 함수에 csrf_exempt라는 데코레이트를 설정해야 합니다.
@csrf_exempt
def join_result(request) :
    # print(request.POST['user_name']) 
    # print(request.POST['user_id'])
    # print(request.POST['user_pw'] )

    # 파라미터 데이터를 추출합니다.
    user_name = request.POST['user_name']
    user_id = request.POST['user_id']
    user_pw = request.POST['user_pw'] 

    # 데이터 저장 처리
    user_model = user_app.models.UserTable()
    user_model.user_name = user_name
    user_model.user_id = user_id
    user_model.user_pw = user_pw
    user_model.save() 

    confirm = '''
                <script>
                    alert('가입이 완료되었습니다')
                    location.href = '/user/login'
                </script>
            ''' 

    return HttpResponse(confirm)

@csrf_exempt
def login_result(requeest) :
    # 사용자가 입력한 파라미터를 추출합니다.
    user_id = requeest.POST['user_id']
    user_pw = requeest.POST['user_pw']

    # print(user_id)
    # print(user_pw)

    # 데이터 베이스에서 사용자 데이터를 가져옵니다.
    # 데이터를 가져올 때 조건에 만족하는 것이 없으면 오류가 발생합니다.
    # 데이터가 없을 때의 처리르 해야 한다면 예외처리를 통해 처리합니다.
    try :
        user_model = user_app.models.UserTable.objects.get(user_id = user_id)
        # print(user_model)

        # 로그인한 사용자와 데이터베이스에서 가져온 데이터의 비밀번호가 같을 경우
        if user_pw == user_model.user_pw :
            message = '''
                    <script>
                        alert('로그인에 성공했습니다')
                        location.href = '/'
                    </script>
                    '''
        # 비밀번호가 다를 경우
        else :
            message = '''
                     <script>
                        alert('비밀번호가 잘못되었습니다.')
                        location.href = '/user/login?login_chk=1'
                    </script>
                    '''
    except :
        # 아이다가 없는 경우
        message = '''
                     <script>
                        alert('존재하지 않는 id입니다.')
                        location.href = '/user/login?login_chk=1'
                    </script>
                    '''

    return HttpResponse(message)
⚠️ **GitHub.com Fallback** ⚠️