Notice
Recent Posts
Recent Comments
Link
«   2025/11   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
Tags more
Archives
Today
Total
관리 메뉴

JOOHUUN

django / 로그인 본문

카테고리 없음

django / 로그인

JOOHUUN 2022. 5. 27. 16:35

1. user/views.py

from django.http import HttpResponse

me = Usermodel의 username이 우리가 요청한 username과 일치하면 object를 가져온다

def sign_in_view(request):
    if request.method == 'POST':
        username = request.POST.get('username', None)
        password = request.POST.get('password', None)

        me = UserModel.objects.get(username=username)   # Usermodel/username == request.POST/username
        if me.password == password:
            request.session['user'] = me.username
            return HttpResponse("로그인 성공!")
        else:
            return redirect('/sign-in')

    elif request.method == 'GET':
        return render(request, 'user/signin.html')

2. signin.html

method, action 작성

<form class="form-area" method="post" action="/sign-in/">
    {% csrf_token %}
    <div class="form-group mt-2 mb-2">
        <label for="username">이름</label>
        <input type="text" class="form-control" id="username" name="username">
    </div>
    <div class="form-group mt-2 mb-2">
        <label for="password">비밀번호</label>
        <input type="password" class="form-control" id="password" name="password">
    </div>

 

Comments