๊ธฐ์กด์ ์๋ form์์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๋ถ๋ถ์ view๋ก ์ฎ๊ธฐ๋ ์ฝ๋ ๋ฆฌํํ ๋ง ์์ ์ ํ๋ค.
form์๋ ๊ฐ์ ์ ํจ์ฑ ๊ฒ์ฌ์ ๋ ํ๊ณ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๋ถ๋ถ์ view๋จ์ ํ๋ ๊ฒ์ ๊ถ์ฅํ๋ค๊ณ ํ๋ค.
from django import forms
from .models import Fcuser
from django.contrib.auth.hashers import check_password
class RegisterForm(forms.Form):
email = forms.EmailField(
error_messages={
'required' : '์ด๋ฉ์ผ์ ์
๋ ฅํด์ฃผ์ธ์.'
},
max_length=64, label='์ด๋ฉ์ผ'
)
password = forms.CharField(
error_messages={
'required' : '๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.'
},
widget=forms.PasswordInput, label='๋น๋ฐ๋ฒํธ'
)
re_password = forms.CharField(
error_messages={
'required' : '๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.'
},
widget=forms.PasswordInput, label='๋น๋ฐ๋ฒํธ ํ์ธ'
)
def clean(self):
# super()๋ ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ
cleaned_data = super().clean() # ๊ฐ์ด ๋ค์ด์๋์ง ๊ฒ์ฌํ๋ค. ๋ค์ด์์ผ๋ฉด ์๋ ๋ณ์๋ฅผ ํ ๋น, ์์ผ๋ฉด ์คํจํ๊ณ ํ๊ตฐ๋ค.
email = cleaned_data.get('email')
password = cleaned_data.get('password')
re_password = cleaned_data.get('re_password')
if password and re_password:
if password != re_password:
self.add_error('password', '๋น๋ฐ๋ฒํธ๊ฐ ์๋ก ๋ค๋ฆ
๋๋ค.')
self.add_error('re_password', '๋น๋ฐ๋ฒํธ๊ฐ ์๋ก ๋ค๋ฆ
๋๋ค.')
#else:
# fcuser = Fcuser(
# email=email,
# password=make_password(password)
# )
# fcuser.save()
'๊ธฐ์กด ์ฝ๋์์ ํ์๊ฐ์
ํ ํ์์ ์ ์ฅํ๋ ์ฝ๋๊ฐ form์์ ๋น ์ก๋ค.'
class LoginForm(forms.Form):
email = forms.EmailField(
error_messages={
'required' : '์ด๋ฉ์ผ์ ์
๋ ฅํด์ฃผ์ธ์.'
},
max_length=64, label='์ด๋ฉ์ผ'
)
password = forms.CharField(
error_messages={
'required' : '๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.'
},
widget=forms.PasswordInput, label='๋น๋ฐ๋ฒํธ'
)
def clean(self):
# super()๋ ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ
cleaned_data = super().clean() # ๊ฐ์ด ๋ค์ด์๋์ง ๊ฒ์ฌํ๋ค. ๋ค์ด์์ผ๋ฉด ์๋ ๋ณ์๋ฅผ ํ ๋น, ์์ผ๋ฉด ์คํจํ๊ณ ํ๊ตฐ๋ค.
email = cleaned_data.get('email')
password = cleaned_data.get('password')
if email and password:
try:
fcuser = Fcuser.objects.get(email=email)
except Fcuser.DoesNotExist:
self.add_error('email', '์์ด๋๊ฐ ์์ต๋๋ค.')
return
if not check_password(password, fcuser.password):
self.add_error('password', '๋น๋ฐ๋ฒํธ๋ฅผ ํ๋ ธ์ต๋๋ค.')
# self.email = fcuser.email
'์ด ๋ถ๋ถ๋ ๋ทฐ๋จ์ผ๋ก ๋์ด๊ฐ๋ค.'
'์ฌ๊ธฐ์๋ email์ด ์ ์๊ฐ ๋์ด์๋ค'
self.email = fcuser.email
์ฌ๋ผ์ง ์ฝ๋์ด๋ค. ์ฌ๊ธฐ์๋ email์ด ๋ณ์์ ํ ๋น ๋์ด์์ง๋ง view๋จ์ผ๋ก ์ฎ๊ธฐ๋ฉด ์ ์ ๋์ด์์ง ์์์ ๋ฐ๋ก ๋ฐ์์ค์ผํ๋ค. from django.shortcuts import render, redirect
from django.views.generic.edit import FormView
from .forms import RegisterForm, LoginForm
from django.contrib.auth.hashers import make_password
from .models import Fcuser
# Create your views here.
def index(request):
return render(request, 'index.html', {'email': request.session.get('user')})
class RegisterView(FormView):
template_name = 'register.html'
form_class = RegisterForm
success_url = '/'
# form_valid ํจ์๊ฐ ์ถ๊ฐ ๋์๋ค.
def form_valid(self,form): # form_valid ํจ์๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ
fcuser = Fcuser(
email=form.data.get('email'),
password=make_password(form.data.get('password')),
level = 'user'
)
fcuser.save()
return super().form_valid(form)
class LoginView(FormView):
template_name = 'login.html'
form_class = LoginForm
success_url = '/'
# ์ฌ๊ธฐ๋ ๋ง์ฐฌ๊ฐ์ง๋ก ์ถ๊ฐ๋์๋ค.
def form_valid(self, form):
self.request.session['user'] = form.data.get('email') # form_valid ํจ์๋ฅผ ํต๊ณผํ๋ฉด ์ด ์กฐ๊ฑด ์คํ
return super().form_valid(form)
def logout(request):
if 'user' in request.session:
del(request.session['user'])
return redirect('/')
ํ์๊ฐ์
, ๋ก๊ทธ์ธ ํจ์์ form_valid
ํจ์๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ ํด์
์
๋ ฅ๋ฐ์ ๊ฐ์ ๊ฒ์ฌํ๊ณ ๋ฐ์ดํฐ์ ์ ์ฅํ๋ ๋ก์ง์ ๋ฃ์๋ค.
form.data.get('email')
์ด๋ ๋ฏ form์ ์ ์ฅํ๋ ๋ณ์๊ฐ ์์ด์ก์ผ๋ฏ๋ก ์
๋ ฅ๋ฐ์ ๊ฐ์์ email์ ๋ฝ์์จ๋ค.
๋๋ถ๋ถ์ ๊ฐ๋ค์ data.get
์ผ๋ก ๋ฝ์์จ๋ค.
rom django import forms
from .models import Product
class RegisterForm(forms.Form):
name = forms.CharField(
error_messages={
'required' : '์ํ๋ช
์ ์
๋ ฅํด์ฃผ์ธ์.'
},
max_length=64, label='์ํ๋ช
'
)
price = forms.IntegerField(
error_messages={
'required' : '์ํ๊ฐ๊ฒฉ์ ์
๋ ฅํด์ฃผ์ธ์.'
},
label='์ํ๊ฐ๊ฒฉ'
)
description = forms.CharField(
error_messages={
'required' : '์ํ์ค๋ช
์ ์
๋ ฅํด์ฃผ์ธ์.'
},
label='์ํ์ค๋ช
'
)
stock = forms.IntegerField(
error_messages={
'required' : '์ฌ๊ณ ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.'
},
label='์ฌ๊ณ '
)
def clean(self):
cleaned_data = super().clean()
name = cleaned_data.get('name')
price = cleaned_data.get('price')
description = cleaned_data.get('description')
stock = cleaned_data.get('stock')
# if name and price and description and stock:
# product = Product(
# name=name,
# price=price,
# description=description,
# stock=stock
# )
# product.save()
@method_decorator(admin_required, name='dispatch')
class ProductCreate(FormView):
template_name = 'register_product.html'
form_class = RegisterForm
success_url = '/product/'
# ์ถ๊ฐ๋ ๋ถ๋ถ
def form_valid(self, form):
product = Product(
name=form.data.get('name'),
price=form.data.get('price'),
description=form.data.get('description'),
stock=form.data.get('stock')
)
product.save()
return super().form_valid(form)
User์ ๋์ผํ๋ค.
return super().form_valid(form)
์ค๋ฒ๋ผ์ด๋ฉ ํ๊ธฐ ๋๋ฌธ์ super()ํจ์๋ฅผ ์ฌ์ฉํ๋ค.
์ ์ฅํ๊ธฐ ๋๋ฌธ์ return๊ฐ์ ๋ฃ์ด์ค๋ค.
from django import forms
from .models import Order
from fcuser.models import Fcuser
from product.models import Product
from django.db import transaction
class RegisterForm(forms.Form):
def __init__(self, request, *args, **kwargs):
super().__init__(*args, **kwargs) # form์ request๋ฅผ ์ ๋ฌ ๋ฐ๊ธฐ ์ํด ๋ง๋ค์๋ค.
self.request = request
quantity = forms.IntegerField(
error_messages={
'required' : '์๋์ ์
๋ ฅํด์ฃผ์ธ์.'
}, label='์๋'
)
product = forms.IntegerField(
error_messages={
'required' : '์ํ๊ฐ๊ฒฉ์ ์
๋ ฅํด์ฃผ์ธ์.'
}, label='์ํ๊ฐ๊ฒฉ', widget=forms.HiddenInput # ํ๋ฉด์๋ ๋ณด์ด์ง ์๋ ์ธํ
)
def clean(self):
cleaned_data = super().clean()
quantity = cleaned_data.get('quantity')
product = cleaned_data.get('product')
fcuser = self.request.session.get('user')
#if quantity and product and fcuser:
# with transaction.atomic():
# prod = Product.objects.get(pk=product)
# order = Order(
# quantity=quantity,
# product=prod,
# fcuser=Fcuser.objects.get(email=fcuser)
# )
# order.save()
# prod.stock -= quantity
# prod.save()
#else:
# self.product = product
# self.add_error('quantity', '์๋์ ์
๋ ฅํด์ฃผ์ธ์')
# self.add_error('product', '๊ฐ์ ์
๋ ฅํด์ฃผ์ธ์.')
if not (quantity and product):
self.add_error('quantity', '์๋์ ์
๋ ฅํด์ฃผ์ธ์')
self.add_error('product', '๊ฐ์ ์
๋ ฅํด์ฃผ์ธ์.')
if not (quantity and product):
๋ก ํํํ๋ค. @method_decorator(login_required, name='dispatch')
class OrderCreate(FormView):
form_class = RegisterForm
success_url = '/product/'
# ์ถ๊ฐ๋ ํจ์
def form_valid(self, form):
with transaction.atomic():
prod = Product.objects.get(pk=form.data.get('product'))
order = Order(
quantity=form.data.get('quantity'),
product=prod, # ์ฃผ๋ฌธํ ์ํ์ ๊ฐ์ฒด๋ฅผ ์ฃผ๋ฌธ๋ชจ๋ธ์ ์ํํ๋์ ์ ์ฅ
fcuser=Fcuser.objects.get(email=self.request.session.get('user'))
)
order.save()
prod.stock -= int(form.data.get('quantity'))
prod.save()
return super().form_valid(form)
form_valid
ํจ์๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ ํ๋ค. prod.stock -= int(form.data.get('quantity'))
์ ์ํ์ผ๋ก ๋ฐ๊พธ์ด ์ฃผ์๋ค.