๐Ÿ›์žฅ๊ณ ๋กœ ์‡ผํ•‘๋ชฐ ๋งŒ๋“ค๊ธฐ(์ฝ”๋“œ ๋ฆฌํŽ™ํ† ๋ง)

ํŒ”๋ฆฌ๋™ยท2021๋…„ 4์›” 17์ผ
0

์žฅ๊ณ ๋กœ ์‡ผํ•‘๋ชฐ ๋งŒ๋“ค๊ธฐ(์ฝ”๋“œ ๋ฆฌํŽ™ํ† ๋ง)

  • ๊ธฐ์กด์— ์žˆ๋˜ form์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ถ€๋ถ„์€ view๋กœ ์˜ฎ๊ธฐ๋Š” ์ฝ”๋“œ ๋ฆฌํŽ™ํ† ๋ง ์ž‘์—…์„ ํ–ˆ๋‹ค.

  • form์—๋Š” ๊ฐ’์˜ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์ •๋„ ํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ถ€๋ถ„์€ view๋‹จ์— ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

User form ๋ณ€ํ™”

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๋‹จ์œผ๋กœ ์˜ฎ๊ธฐ๋ฉด ์ •์˜ ๋˜์–ด์žˆ์ง€ ์•Š์•„์„œ ๋”ฐ๋กœ ๋ฐ›์•„์ค˜์•ผํ•œ๋‹ค.

User 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์œผ๋กœ ๋ฝ‘์•„์˜จ๋‹ค.

product form ๋ณ€ํ™”

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()
  • User ๋™์ผํ•˜๊ฒŒ ์ƒํ’ˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์ฝ”๋“œ๋Š” ์‚ฌ๋ผ์กŒ๋‹ค.

product view ๋ณ€ํ™”

@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๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.

Order form ๋ณ€ํ™”

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):๋กœ ํ‘œํ˜„ํ–ˆ๋‹ค.

order view ๋ณ€ํ™”

@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')) ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ฐ”๊พธ์–ด ์ฃผ์—ˆ๋‹ค.
profile
๋ฐฐ์›€์˜ ๊ธฐ๋ก

0๊ฐœ์˜ ๋Œ“๊ธ€