์ƒˆ์†Œ์‹

๐Ÿ ํŒŒ์ด์ฌ (Python)

์žฅ๊ณ (Django) ๋กœ๊ทธ์ธ , ๋กœ๊ทธ์•„์›ƒ ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

  • -

2019/12/28 - [์žฅ๊ณ  (django)] - (django) ์žฅ๊ณ  - ํšŒ์›๊ฐ€์ž… ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ (MVC)

 

(django) ์žฅ๊ณ  - ํšŒ์›๊ฐ€์ž… ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ (MVC)

*๋น„๋ฐ€๋ฒˆํ˜ธ๋ž€๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ ๋ž€์„ ๋‹ค๋ฅด๊ฒŒ ์ž…๋ ฅํ–ˆ์„๋•Œ *์•„๋ฌด๊ฒƒ๋„ ์ž…๋ ฅํ•˜์ง€ ์•Š๊ณ  ๋“ฑ๋ก ํ–ˆ์„๋•Œ *Admin์—์„œ ๋“ฑ๋ก๋œ ๋ชฉ๋ก ์ด์ „ ํฌ์ŠคํŒ…์—์„œ ์ƒ์„ฑํ–ˆ๋˜ user๋ผ๋Š” app์— ํšŒ์›๊ฐ€์ž…์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์ „์†ก ๋ฐ ์‚ฝ์ž… ํด๋ž˜์Šค๋ฅผ..

infinitt.tistory.com

ํšŒ์›๊ฐ€์ž… ๊ธฐ๋Šฅ๊นŒ์ง€ ๋งˆ์ณค๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

 

 

1. view์— ๋กœ๊ทธ์ธ, ๋กœ๊ทธ์•„์›ƒ ํ•จ์ˆ˜ ์ƒ์„ฑํ•˜๊ธฐ (์ดํ•˜ ์ฝ”๋“œ๋Š” ์ „๋ถ€ views.py)

*user(์•ฑ) - views.py  ์ž„ํฌํŠธ ๋ถ€๋ถ„
from django.shortcuts import render, redirect
from .models import Myuser
from django.http import HttpResponse
from django.contrib.auth.hashers import make_password, check_password #๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” / ํŒจ์Šค์›Œ๋“œ ์ฒดํฌ(db์—์žˆ๋Š”๊ฑฐ์™€ ์ผ์น˜์„ฑํ™•์ธ)

HttpResponse : ์‘๋‹ต์— ๋Œ€ํ•œ ๋ฉ”ํƒ€์ •๋ณด๋ฅผ ํฌํ•จํ•œ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ๋กœ๊ทธ์ธ ์™„๋ฃŒ์‹œ์— "๋กœ๊ทธ์ธ ์™„๋ฃŒ" ๋ผ๋Š” text๋ฅผ ๋„์šฐ๊ธฐ ์œ„ํ•ด ์ž„ํฌํŠธํ–ˆ์Šต๋‹ˆ๋‹ค.

make_password(str)์ด ํ•จ์ˆ˜์— ๋„ฃ์–ด์ค€ ๋ฌธ์ž์—ด์„ ์•”ํ˜ธํ™”ํ•ฉ๋‹ˆ๋‹ค. (hashing)

check_password(a,b) : a,b๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธ, ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 

 

*user(์•ฑ) - views.py ๋กœ๊ทธ์ธ ํ•จ์ˆ˜
def login(request):
    response_data = {}

    if request.method == "GET" :
        return render(request, 'login.html')

    elif request.method == "POST":
        login_username = request.POST.get('username', None)
        login_password = request.POST.get('password', None)


        if not (login_username and login_password):
            response_data['error']="์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋ชจ๋‘ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
        else : 
            myuser = Myuser.objects.get(username=login_username) 
            #db์—์„œ ๊บผ๋‚ด๋Š” ๋ช…๋ น. Post๋กœ ๋ฐ›์•„์˜จ username์œผ๋กœ , db์˜ username์„ ๊บผ๋‚ด์˜จ๋‹ค.
            if check_password(login_password, myuser.password):
                request.session['user'] = myuser.id 
                #์„ธ์…˜๋„ ๋”•์…”๋„ˆ๋ฆฌ ๋ณ€์ˆ˜ ์‚ฌ์šฉ๊ณผ ๋˜‘๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
                #์„ธ์…˜ user๋ผ๋Š” key์— ๋ฐฉ๊ธˆ ๋กœ๊ทธ์ธํ•œ id๋ฅผ ์ €์žฅํ•œ๊ฒƒ.
                return redirect('/')
            else:
                response_data['error'] = "๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค."

        return render(request, 'login.html',response_data)

request.POST.get('username', None)POST๋กœ ๋“ค์–ด์˜จ ๋ฐ์ดํ„ฐ์—์„œ username์ด๋ผ๋Š” key๋กœ ๊ฐ’์„ ์–ป์Šต๋‹ˆ๋‹ค. ์—†๋‹ค๋ฉด None์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Myuser.objects.get(username=login_username) : POST๋กœ ๋“ค์–ด์˜จ login_username์œผ๋กœ Myuserํด๋ž˜์Šค์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊บผ๋‚ด์˜ต๋‹ˆ๋‹ค. (username, password, email)

request.session['user'] = myuser.id : ์„ธ์…˜(session)์— id๊ฐ’์„ ๋„ฃ์Šต๋‹ˆ๋‹ค. ๋กœ๊ทธ์ธ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค.

๋กœ๊ทธ์•„์›ƒ, home ํ•จ์ˆ˜
def home(request):
    user_id = request.session.get('user')
    if user_id :
        myuser_info = Myuser.objects.get(pk=user_id)  #pk : primary key
        return HttpResponse(myuser_info.username)   # ๋กœ๊ทธ์ธ์„ ํ–ˆ๋‹ค๋ฉด, username ์ถœ๋ ฅ

    return HttpResponse('๋กœ๊ทธ์ธ์„ ํ•ด์ฃผ์„ธ์š”.') #session์— user๊ฐ€ ์—†๋‹ค๋ฉด, (๋กœ๊ทธ์ธ์„ ์•ˆํ–ˆ๋‹ค๋ฉด)
    
    
def logout(request):
    request.session.pop('user')
    return redirect('/')

myuser_info = Myuser.objects.get(pk=user_id) : ์„ธ์…˜์— ๋„ฃ์–ด๋†จ๋˜ id ๊ฐ’์„ pk๋กœ ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊บผ๋‚ด์˜ต๋‹ˆ๋‹ค.

( pk : primary key)

 

๋กœ๊ทธ์•„์›ƒ์€ ์„ธ์…˜์— ์žˆ๋Š” 'user'์˜ ๊ฐ’์„ ์ œ๊ฑฐํ•˜๋ฉด(pop ๋˜๋Š” del) ๋ฉ๋‹ˆ๋‹ค.

 

2. ํŽ˜์ด์ง€(html) ์ƒ์„ฑ

 

<div class = "raw">
    <div class = "col-12" >
      <div class = "col-12 text-center">
<h1>๋กœ๊ทธ์ธ</h1>
<br><br>

</div>
<form method = "POST">
  {% csrf_token %}
  
  <div class="form-group">
    <label for="username">์‚ฌ์šฉ์ž ์ด๋ฆ„</label>
    <input type="text" 
    class="form-control" 
    name = "username" 
    id="username" 
    placeholder="์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”">
  </div>

  <div class="form-group">
    <label for="password">๋น„๋ฐ€๋ฒˆํ˜ธ</label>
    <input type="password" 
    name = "password" 
    class="form-control" id="password" placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”">
  </div>


  <span style="color:red;"><strong>{{ error }}</strong> </span>
  <button type="submit" class="btn btn-primary">๋กœ๊ทธ์ธ</button>
</form>

ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€์—์„œ form ๋ช‡์นธ๋งŒ ์‚ญ์ œํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

3. URL์—ฐ๋™

 

*ํ”„๋กœ์ ํŠธ ํด๋” - url.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from myuser.views import home
urlpatterns = [
    path('admin/', admin.site.urls),
    path('user/', include('myuser.urls')), #ํด๋”.ํŒŒ์ผ๋ช…
    #์ฆ‰, user/ ์ดํ•˜ url๋“ค์€ myuserํด๋”์˜ urls์—์„œ ๊ด€๋ฆฌํ•˜๋„๋กํ•œ๋‹ค.๋ผ๋Š” ์„ค์ •
    path('', home),
]

๋กœ๊ทธ์ธ์ด ๋˜๋ฉด, homeํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•˜๋„๋ก ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด home์„ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

 

user(์•ฑ) - url.py
from django.urls import path
from . import views

urlpatterns = [
    path('register/', views.register ),
    path('login/', views.login  ),
    path('logout/', views.logout  ),
]

login , logout url์„ ๋งŒ๋“ค์–ด์ฃผ๊ณ , views์— ๋งŒ๋“  ๋กœ๊ทธ์ธ, ๋กœ๊ทธ์•„์›ƒ ํ•จ์ˆ˜์™€ ์—ฐ๊ฒฐํ•ด ์ค๋‹ˆ๋‹ค.

 

 

community.zip
0.05MB

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.