혼자서도 잘해요리는 추천서비스 모델을 활용한 Django 기초 웹 개발 프로젝트다.
그 중 유저 기능은 브라우저에서 회원가입/로그인/로그아웃 기능을 사용하여 웹에서 유저만 사용할 수 있는 다양한 기능을 구현하기 위해 개발했다.
from django.contrib.auth.models import AbstractUser
# Create your models here.
class UserModel(AbstractUser):
class Meta:
db_table = "user"
AUTH_USER_MODEL = 'user.UserModel'
from django.urls import path
from . import views
urlpatterns = [
# path('', views.home, name='home'),
path('signup/', views.sign_up_view, name='signup'),
]
from django.shortcuts import render, redirect
from .models import UserModel
from django.contrib.auth import get_user_model
# Create your views here.
def sign_up_view(request):
if request.method == 'GET':
user = request.user.is_authenticated
if user:
return redirect ('/')
else:
return render(request, 'signup.html')
elif request.method == 'POST':
username = request.POST.get('username', '')
email = request.POST.get('email', '')
password = request.POST.get('password', '')
password2 = request.POST.get('password2' '')
if password != password2:
return render(request, 'signup.html', {'error': '비밀번호가 맞지 않습니다'})
else:
if username == '' or password == '' or email == '':
return render(request, 'signup.html', {'error': '데이터를 전부 입력해주세요'})
exist_user = get_user_model().objects.filter(username=username)
if exist_user:
return render(request, 'signup.html', {'error':'존재하는 사용자입니다'})
exist_user = get_user_model().objects.filter(email=email)
if exist_user:
return render(request, 'signup.html',{'error':'존재하는 이메일입니다'})
else:
UserModel.objects.create_user(
username=username, password=password, email=email)
return redirect('/signin')
# return redirect('signin/', {'msg':'회원가입 성공'})
<main class="wrap main-col signup-main">
<h1 class="signup-title">회원가입</h1>
<form class="form-area" method="post" action="/signup/">
{% csrf_token %}
<div class="signup-div">
<label for="email">아이디(이메일)</label>
<input class="form-control" id="email" type="email" name="email" placeholder="아이디"/>
<!-- <button type="button" class="id_check">중복확인</button> -->
<label for="password">비밀번호</label>
<input class="form-control"
id="password"
type="password"
name="password"
placeholder="비밀번호"
/>
<label for="password2">비밀번호 확인</label>
<input class="form-control"
id="password2"
type="password"
name="password2"
placeholder="비밀번호 확인"
/>
<label for="username">닉네임</label>
<input class="form-control"
id="username"
type="text"
name="username"
placeholder="닉네임을 입력하세요"
/>
</div>
{% if error %}
<div class="alert alert-danger" role="alert">
{{ error }}
</div>
{% endif %}
<button type="submit">회원가입</button>
<a href="/signin" class="login-link"
>회원이신가요? 로그인 페이지로 이동</a
>
</form>
</main>
from django.urls import path
from . import views
urlpatterns = [
# path('', views.home, name='home'),
path('signin/', views.sign_in_view, name='signin'),
]
def sign_in_view(request):
if request.method == 'POST':
username = request.POST.get('username', '')
password = request.POST.get('password', '')
me = auth.authenticate(request, username=username, password=password)
if me is not None:
auth.login(request, me)
return redirect('/')
else:
return render(request, 'signin.html',{'error':'유저 이름 혹은 패스워드를 입력해주세요'})
elif request.method == 'GET':
user = request.user.is_authenticated
if user:
return redirect('/')
else:
return render(request, 'signin.html')
<form class="login-form" action="/signin/" method="post">
{% csrf_token %}
<div class="login-id">
<img src="/static/img/login_id.png" class="login-icon id-icon" />
<input class="form-control" type="text" id="username" name="username" placeholder="아이디" />
</div>
<div class="login-password">
<img
src="/static/img/login_password.png"
class="login-icon password-icon"
/>
<input
class="form-control"
type="password"
id="password"
name="password"
placeholder="비밀번호"
/>
</div>
{% if error %}
<div class="alert alert-danger" role="alert">
{{ error }}
</div>
{% endif %}
<button type="submit">로그인</button>
<a href="/signup"
><button type="button">회원가입</button></a
>
</form>
from django.urls import path
from . import views
urlpatterns = [
# path('', views.home, name='home'),
path('logout/', views.logout, name='logout'),
]
@login_required
def logout(request):
auth.logout(request)
return redirect('/')
{% if user.is_authenticated %}
<a class="nav-link" href="/logout"> 로그아웃 </a>
{% endif %}