get 테스트

홍태경·2021년 4월 21일
0

http -v get http://localhost:8000/products/productsview
GET /products/productsview HTTP/1.1
Accept: /
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:8000
User-Agent: HTTPie/1.0.3

HTTP/1.1 201 Created
Content-Length: 627
Content-Type: application/json
Date: Wed, 21 Apr 2021 05:06:31 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.8.8
Vary: Origin
X-Content-Type-Options: nosniff
X-Frame-Options: DENY

{
"data": [
{
"category": "패브릭",
"discount_rate": "0.20",
"name": "사계절 이불",
"price": "99000.00"
},
{
"category": "패브릭",
"discount_rate": "0.20",
"name": "청정커튼 크림화이트",
"price": "159000.00"
},
{
"category": "패브릭",
"discount_rate": "0.20",
"name": "[시즌오프]20FW 겨울잠 이불",
"price": "129000.00"
},
{
"category": "패브릭",
"discount_rate": "0.20",
"name": "[특가할인] 휴대용 낮잠 패드 겸 이불",
"price": "29000.00"
}
]
}

products/views.py

import json
import jwt

from django.views import View
from django.http import JsonResponse

from .models import Category, Product
from users.models import User

class ProductsView(View):
def get(self, request):

    try:
        products = Product.objects.all()
        product_list = []

        for i in products:
            product_list.append(
                    {
                    "name" : i.name,
                    'price' : i.price,
                    'discount_rate' : i.discount_rate,
                    'category': i.category.name
                    }
                )
        return JsonResponse({"data" : product_list}, status=201)
    except KeyError:
        return JsonResponse({"message" :"KEY_ERROR"}, status=400)
        
        

products/urls.py

from django.urls import path
from .views import ProductsView

urlpatterns =[
path('/productsview', ProductsView.as_view()),

   # path('/signin', SignInView.as_view())
    ]
    
    
    

shell/urls.py

from django.urls import path,include

urlpatterns = [
path('products', include('products.urls')),
]

products/models.py

from django.db import models
class Category(models.Model):
name = models.CharField(max_length=45, unique=True)
class Meta:
db_table = 'categories'
class Product(models.Model):
name = models.CharField(max_length=45, unique=True)
price = models.DecimalField(max_digits=8, decimal_places=2)
description = models.CharField(max_length=255)
discount_rate = models.DecimalField(max_digits=3, decimal_places=2)
thumbnail_url = models.URLField(max_length=500)
size = models.ManyToManyField('Size', through='ColorSizeOption')
color = models.ManyToManyField('Color', through='ColorSizeOption')
category = models.ForeignKey(Category, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
db_table = 'products'

profile
나의 에고를 인정하고 사랑하자

0개의 댓글