::: 현재까지 진행 상황 :::
- 초기 세팅 완료
- Users 앱 생성 완료
- models.py의 User 클래스 작성 완료
- DB에 migrate까지 진행
{"message": "KEY_ERROR"}, status code 400
반환@
and .
가 반드시 포함되어야하며, 조건이 만족하지 않을 경우 에러 반환8자리 이상
and 문자 + 숫자 + 특수문자
포함, 조건이 만족하지 않을 경우 에러 반환정규표현식(Regular Expression)
을 사용{"message": "SUCCESS"}, status code 201
반환class SignUpView(View):
def post(self, request):
try:
data = json.loads(request.body)
name = data['name']
email = data['email']
password = data['password']
contact = data['contact']
class SignUpView(View):
def post(self, request):
try:
data = json.loads(request.body)
# 회원가입에 필요한 정보를 client로부터 Json형식으로 reqeust.body에 담겨서 전달받음
# data = json.loads(request.body)
# client로부터 받은 정보를 파이썬에서 사용할 수 있는 "dict" 형태로 바꾸어주고
# data 라는 변수에 할당
name = data['name']
email = data['email']
password = data['password']
contact = data['contact']
# data 는 객체이고, Key와 Value를 불러서 각각 name, email, password, contact에 할당
(re package 사용)
import re
(***)
email_regex = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
password_regex = '^(?=.*[A-Za-z])(?=.*\d)(?=.*[@!%*#?&])[A-Za-z\d@!%*#?&]{8,}$'
if not re.match(email_regex, email):
return JsonResponse({"message": "INVALID_EMAIL"}, status=400)
if not re.match(password_regex, password):
return JsonResponse({"message": "INVALID_PASSWORD"}, status=400)
정규표현식 변수 할당
email_regex = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
# **Email Validation** ::
# `@` and `.` 가 반드시 포함되어야하며, 조건이 만족하지 않을 경우 에러 반환
password_regex = '^(?=.*[A-Za-z])(?=.*\d)(?=.*[@!%*#?&])[A-Za-z\d@!%*#?&]{8,}$'
# **Password Validation** ::
# `8자리 이상` and `문자 + 숫자 + 특수문자` 포함, 조건이 만족하지 않을 경우 에러 반환
** 정규표현식은 그냥 인터넷에서 찾아서 써도 무방하다. 굳이 내가 만들거나 작성하지 않아도 괜츈!
** 하지만 정규표현식이 표현하고 있는 규칙을 알고 읽을 수는 있어야한다.
re.match(패턴, 패턴을 찾을 문자열)
** 위 정규표현식을 통해서 요청받은 문자열이 해당 패턴에 만족하는지에 대해 확인을 하기 위해서
** re.match(패턴, 패턴을 찾을 문자열) 사용(***중요***)
** re.match :: "문자열의 처음"부터 시작하여 패턴이 일치되는 것이 있는 지 확인을 하는 Method)
re.match 적용
import re
** Email Validation에 만족할 때,
email = "jhwang90801@gmail.com"
email_regex = "^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
print(re.match(email_regex, email))
# <re.Match object; span=(0, 20), match='jhwang90801@gmail.com'>
** Email Validation에 만족하지 않을 때,
email = "jhwang90801.com"
email_regex = "^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
print(re.match(email_regex, email))
# None
'정규표현식' & 're.match' 코드 적용
if not re.match(email_regex, email):
return JsonResponse({"message": "INVALID_EMAIL"}, status=400)
if not re.match(password_regex, password):
return JsonResponse({"message": "INVALID_PASSWORD"}, status=400)
# client에게 요청으로 받은 email과 password 데이터를
# 정규표현식으로 할당한 validation과 비교해서 일치하지 않는다면,
# {"message": "INVALID_EMAIL"}, status=400 반환하도록 설정
# {"message": "INVALID_PASSWORD"}, status=400 반환하도록 설정
REFERENCE
if User.objects.filter(email = email).exists():
return JsonResponse({"message": "EMAIL_IS_ALREADY_REGISTERED"}, status=400)
QuerySet API :::
filter() & exists() 사용
filter()
filter() : filter(**kwargs): 키워드 인자로 주어진 lookup 조건에 일치하는 레코드들의 QuerySet 반환
case1
In : Category.objects.filter(name='브루드커피')
Out : [<Category: Category object (3)>, <Category: Category object (4)>]
case2
In : Category.objects.filter(name='브루드커피').filter(id=3)
Out : [<Category: Category object (3)>]
case3
In : Category.objects.filter(name='브루드커피').exclude(id=3)
Out : [<Category: Category object (4)>]
exists()
exists() : filter()와 함께 서용해서 filter 조건에 맞는 데이터가 있는지 조회,
존재하면 True 존재하지 않으면 False를 반환 (QuerySet을 반환하지 않음)
In : Category.objects.filter(name='브루드커피').exists()
Out : True
REFERENCE
User.objects.create(
name = name,
email = email,
password = password,
contact = contact
)
return JsonResponse({"message": "SUCCESS"}, status=201)
위 모든 If문을 통과한다면, (이메일, 비밀번호를 중복없이 조건에 알맞게 입력했다면)
create() Method를 통해서 데이터는 DB users table에 추가
추가로, SUCCESS 메세지와 201을 반환
try:
except KeyError:
return JsonResponse({"message": "KEY_ERROR"}, status=400)
이메일 or 비밀번호가 제대로 전달되지 않는 경우 400에러를 반환시키기 위한 예외처리
이 케이스의 경우 KeyError에 해당되므로, 위와 같이 코드를 작성
(KeyError:: 키 없음 에러(주로 딕셔너리 사용시)
REFERENCE
import json
import re
from django.http import JsonResponse
from django.views import View
from users.models import User
class SignUpView(View):
def post(self, request):
try:
data = json.loads(request.body)
name = data['name']
email = data['email']
password = data['password']
contact = data['contact']
email_regex = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
password_regex = '^(?=.*[A-Za-z])(?=.*\d)(?=.*[@!%*#?&])[A-Za-z\d@!%*#?&]{8,}$'
if not re.match(email_regex, email):
return JsonResponse({"message": "INVALID_EMAIL"}, status=400)
if not re.match(password_regex, password):
return JsonResponse({"message": "INVALID_PASSWORD"}, status=400)
if User.objects.filter(email = email).exists():
return JsonResponse({"message": "EMAIL_IS_ALREADY_REGISTERED"}, status=400)
User.objects.create(
name = name,
email = email,
password = password,
contact = contact
)
return JsonResponse({"message": "SUCCESS"}, status=201)
except KeyError:
return JsonResponse({"message": "KEY_ERROR"}, status=400)
westagram/urls.py
from django.urls import path, include
urlpatterns = [
# users
path("users", include("users.urls"))
]
users/urls.py
from django.urls import path
from users.views import SignUpView, SignInView
urlpatterns = [
path('/signup', SignUpView.as_view()),
]
Runserver
:::
$ python manage.py runserver 0:8000
Httpie
:::
-`http -v POST http://localhost:8000/users/signup name="홍길동" email="jfooej@gmail.com" password='ooej213$@@3!' contact="001-1200-1232"`
POST하여 원하는 에러가 반환 혹은 성공 메세지가 반환되는지 확인하여야 한다.