signup, login view는 instagram clone project에서 해봤기 때문에 수월하게 진행 되었는데, Product 관련 view는 처음이다보니 시간도 걸리고 모델링 수정도 많이 하게 되었다.
첫 번째 도전의! 코드!
import json
from django.views import View
from django.http import JsonResponse
from products.models import Product, ProductImage, Category, CategoryImage, Comment, AlcoholType, FingerFood, FingerFoodImage, Taste
class ProductListView(View):
def get(self, request):
category = request.GET.get('category', None)
searching = request.GET.get('name', None)
products = Product.objects.all()
products = Product.objects.filter(name__icontains=searching) if searching else products
product_list = [{
'id' : product.id,
'name' : product.name,
'price' : product.price,
'product_image' : productimage.image_url,
'description_tag' : product.description_tag,
}]
except ValueError:
return JsonResponse({'message': 'VALUE_ERROR'}, status = 400)
return JsonResponse({'message': 'SUCCESS', 'product_list': product_list}, status = 200)
class ProductDetailView(View):
def get(self, request, product_id):
try:
product = Product.objects.select_related('category', 'alcohol_type').get(id=product_id)
product_detail = {
'id' : product.id,
'size' : product.size,
'name' : product.name,
'description_detail' : product.description_detail,
'description_tag' : product.description_tag,
'price' : product.price,
'alcohol_percentage' : product.alcohol_percentage,
'category' : product.category.name,
'alcohol_type' : product.alcohol_type.type,
'product_image' : productimage.image_url,
'comment' : comment.content,
'taste' : taste.type,
'finger_food' : fingerfood.name,
'finger_food_image' : fingerfoodimage.image_url,
}
except Product.DoesNotExist:
return JsonResponse({'message': 'PRODUCT_NOT_FOUND'}, status = 404)
return JsonResponse({'message': 'SUCCESS', 'product_detail': product_detail}, status = 200)
(통신만을 위한 코드 / 주석처리하고 FE와 통신만 맞춰보았다.)
class ProductDetailView(View):
def get(self, request):
try:
products = Product.objects.all()
# products_images = ProductImage.objects.all()
# categories = Category.objects.all()
# tastes = Taste.objects.all()
# comments = Comment.objects.all()
# finger_food = FingerFood.objects.all()
# finger_food_image = FingerFoodImage.objects.all()
product_detail=[]
for product in products:
product_detail.append({
'id' : product.id,
'size' : product.size,
'name' : product.name,
'description_detail' : product.description_detail,
'description_tag' : product.description_tag,
'price' : product.price,
'alcohol_percentage' : product.alcohol_percentage
})
# for j in categories:
# product_detail.append({
# 'category' : categories.category
# })
# for k in products_images:
# product_detail.append({
# 'product_image' : products_images.image_url
# })
# for l in comments:
# product_detail.append({
# 'comment' : comments.content
# })
# for m in tastes:
# product_detail.append({
# 'taste' : tastes.type
# })
# for n in finger_food:
# product_detail.append({
# 'finger_food' : finger_food.name
# })
# for o in finger_food_image:
# product_detail.append({
# 'finger_food_image' : finger_food_image.image_url
# })
except Product.DoesNotExist:
return JsonResponse({'message': 'PRODUCT_NOT_FOUND'}, status = 404)
return JsonResponse({
"message": 'SUCCESS',
'product_detail': product_detail},
status = 200)
필요한 페이지라고 생각해 view를 짰는데 .. 착각했던 페이지였다.
FE 와의 소통이 매우 중요하다는 것을 다시 한 번 느꼈다.
class MainCategoryListView(View):
def get(self, request, product_id):
try:
product = Product.objects.select_related('category', 'alcohol_type').get(id=product_id)
category = Category.objects.all()
alcohol_types = AlcoholType.objects.all()
category_image = CategoryImage.objects.all()
products_image = ProductImage.objects.all()
category_list = [{
'category_image' : category_image.image_url,
'category' : category.name
}]
product_list = [{
'id' : products.id,
'name' : products.name,
'price' : products.price,
'alcohol_type' : alcohol_types.type,
'product_image' : products_image.image_url,
'description_tag' : products.description_tag,
}]
except ValueError:
return JsonResponse({'message': 'VALUE_ERROR'}, status = 400)
return JsonResponse({
'message': 'SUCCESS','category_list': category_list,'product_list': product_list}, status = 200)
import json, re, random
from django.shortcuts import render
from django.views import View
from django.http import JsonResponse
from django.db.models import Q
from products.models import Category, Product
'''
http로 통신이 된다면?
#http://localhost:8000/products?categoryId=1
#http://localhost:8000/products?offset=0&limit=12
{"categoryId" : 1}
'''
class ProductListView(View):
def get(self, request):
try:
category_id = request.GET.get('categoryId', None)
searching = request.GET.get('productName', None)
offset = request.GET.get('offset', 0)
limit = request.GET.get('limit', 30)
#category_list = []
#categories = Category.objects.all()
#for category in categories:
# category_list.append({
# 'category' : category.id,
# 'category_image' : category.categoryimage_set.first().image_url,
# })
#Q를 사용해 if문 두 개를 한번에 묶어 주었다.
filter_condition = Q()
if category_id:
filter_condition &= Q(category_id=category_id)
if searching:
filter_condition &= Q(name__icontains=searching)
products = Product.objects.filter(filter_condition).order_by('?')[offset:offset+limit]
product_list = [{
#'category_id' : product.category.id,
'product_id' : product.id,
'name' : product.name,
'price' : product.price,
'description_tag' : product.description_tag,
'products_image' : product.productimage_set.first().image_url,
} for product in products]
return JsonResponse({'product_list' : product_list}, status = 200)
except ValueError:
return JsonResponse({'message':'VALUE_ERROR'}, status = 400)