728x90
product_detail.html 수정
product 앱 > templates > product.html 코드를 아래와 같이 수정해 주세요.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
{% extends "base.html" %}
{% load humanize %}
{% block contents %}
<div class="row mt-5">
<div class="col-12">
<table class="table table-light">
<thead class="thead-light">
<tr>
<th space="col">#</th>
<th space="col">상품명</th>
<th space="col">가격</th>
<th space="col">등록날짜</th>
</tr>
</thead>
<tbody class="text-dark">
{% for product in product_list %}
<tr>
<th space="row">{{ product.id }}</th>
<th><a href="/product/{{ product.id }}">{{ product.name }}</a></th>
<th>{{ product.price|intcomma }} 원</th>
<th>{{ product.register_date|date:'Y-m-d H:i' }}</th>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% for product in product_list %}
{% endfor %}
{% endblock %}
|
product >> forms.py 수정
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
from 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()
|
order 앱에 forms.py 생성
order 앱에서 forms.py 소스 코드 입력
방금 order 앱에 생성한 forms.py 에서 소스코드를 아래와 같이 입력해주세요
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
from django import forms
from .models import Order
from product.models import Product
from fcuser.models import Fcuser
from django.db import transaction
class RegisterForm(forms.Form):
def __init__(self, request, *args, **kwargs):
super().__init__(*args, **kwargs)
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', '값이 없습니다')
|
product 앱에서 views.py 수정
product >> views.py 의 소스를 아래와 같이 수정합니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from django.views.generic.edit import FormView
from .models import Product
from .forms import RegisterForm
from order.forms import RegisterForm as OrderForm
# Create your views here.
class ProductList(ListView):
model = Product
template_name = 'product.html'
context_object_name = 'product_list'
class ProductCreate(FormView):
template_name = 'register_product.html'
form_class = RegisterForm
success_url = '/product/'
class ProductDetail(DetailView):
template_name = 'product_detail.html'
queryset = Product.objects.all()
context_object_name = 'product'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = OrderForm(self.request)
return context
|
order 앱에서 views.py 수정
views.py 소스 코드 입력
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
from django.shortcuts import render, redirect
from django.views.generic.edit import FormView
from django.views.generic import ListView
from .forms import RegisterForm
from .models import Order
# Create your views here.
class OrderCreate(FormView):
form_class = RegisterForm
success_url = '/product/'
def form_invalid(self, form):
return redirect('/product/' + str(form.product))
def get_form_kwargs(self, **kwargs):
kw = super().get_form_kwargs(**kwargs)
kw.update({
'request': self.request
})
return kw
class OrderList(ListView):
template_name = 'order.html'
context_object_name = 'order_list'
def get_queryset(self, **kwargs):
queryset = Order.objects.filter(fcuser__email=self.request.session.get('user'))
return queryset
|
order 앱에서 order.html 생성
order.html 소스코드 입력
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
{% extends "base.html" %}
{% load humanize %}
{% block contents %}
<div class="row mt-5">
<div class="col-12">
<table class="table table-light">
<thead class="thead-light">
<tr>
<th space="col">#</th>
<th space="col">상품명</th>
<th space="col">수량</th>
<th space="col">주문날짜</th>
</tr>
</thead>
<tbody class="text-dark">
{% for order in order_list %}
<tr>
<th space="row">{{ order.id }}</th>
<th>{{ order.product }}</th>
<th>{{ order.quantity|intcomma }} 개</th>
<th>{{ order.register_date|date:'Y-m-d H:i' }}</th>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% for order in order_list %}
{% endfor %}
{% endblock %}
|
urls.py 추가
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
from django.contrib import admin
from django.urls import path
from fcuser.views import index, RegisterView, LoginView
from product.views import ProductList, ProductCreate, ProductDetail
from order.views import OrderCreate, OrderList
urlpatterns = [
path('admin/', admin.site.urls),
path('', index),
path('register/', RegisterView.as_view()), #클래스는 .as_view() 를 해주어야 함
path('login/', LoginView.as_view()),
path('product/', ProductList.as_view()),
path('product/<int:pk>/', ProductDetail.as_view()),
path('product/create/', ProductCreate.as_view()),
path('order/', OrderList.as_view()),
path('order/create/', OrderCreate.as_view()),
]
|
from order.views import OrderCreate, OrderList
path('order/', OrderList.as_view()),
path('order/create/', OrderCreate.as_view()),
를 추가해주세요
확인하기
이제 터미널에서 python manage.py runserver 을 해서 서버를 열고
127.0.0.1:8000/product/로 가시면 아래와 같이 상품 목록이 나올거에요 화면이 나올거에요
이 상태에서 상품을 눌러보시면
이렇게 주문하기가 나올겁니다. 그럼 수량을 적고 주문하기를 눌러보세요
주문하기 눌르면 목록으로 url 화면이 바뀔겁니다.
그리고 주문했던 상품을 들어가보시면 수량이 줄어든걸 확인하실수 있구요
127.0.0.1:8000/order 주소로 들어가보시면 주문했던걸 확인하실수 있으실거에요
그럼 20000~~
728x90
'Python & Django > Django Framework 실전' 카테고리의 다른 글
[Decorator] - 페이지 권한 쉽게 설정하기 (0) | 2020.08.16 |
---|---|
[view] 상품 등록하기, 상세 보기 (0) | 2020.07.30 |
[view] 상품 목록 만들기 (0) | 2020.07.27 |
[view] 회원 가입, 로그인 만들기 (0) | 2020.07.24 |
[설정] Model, Admin 구성하기 (0) | 2020.07.21 |
댓글