from django.db import models
class Users(models.Model):
name = models.CharField(max_length=45)
address = models.CharField(max_length=200)
phone_number= models.CharField(max_length=45)
class Meta:
db_table='users'
view.py 에서 c r u d 중 create 를 하기위해서는 위와같은 models.py를 임폴트하여 가지고와야함 .
다음과 같이 코딩 .
#views.py
import json
from django.views import View
from django.http import JsonResponse
from .models import Users
class SignUpView(View):
def post(self , request):
data =json.loads(request.body)
return JsonResponse({"name":data["name"]} , status=200)
아래와 같은 코드를 포스트 한다고 가정할떄 Create를 해보겟다 .
http POST localhost:8000/user name="웅이" address="테헤란로" phone_number="010-1234-1234"
#views.py
import json
from django.views import View
from django.http import JsonResponse
from .models import Users
class SignUpView(View):
def post(self , request):
data =json.loads(request.body)
# name =data["name"]
# address =data["address"]
# phone =data["phone_number"]
# print(name , address , phone)
Users(
name =data["name"],
address =data["address"],
phone_number =data["phone_number"]
).save()
return JsonResponse({"Message":"SUCSESS"} , status=200)