views.py는 해당 APP의 로직을 담당하는 파일입니다.
step#6에서 생성한 "user"라는 APP의 views.py 파일을 만들어 보겠습니다.
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » pwd
/Users/marie/mynewdjango
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » tree
.
├── db.sqlite3
├── manage.py
├── mynewjangodirec
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-37.pyc
│ │ ├── settings.cpython-37.pyc
│ │ └── urls.cpython-37.pyc
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── user
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-37.pyc
│ ├── admin.cpython-37.pyc
│ └── models.cpython-37.pyc
├── admin.py
├── apps.py
├── migrations
│ ├── 0001_initial.py
│ ├── __init__.py
│ └── __pycache__
│ ├── 0001_initial.cpython-37.pyc
│ └── __init__.cpython-37.pyc
├── models.py
├── tests.py
├── urls.py
└── views.py
6 directories, 24 files
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » vi ./user/views.py
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » cat ./user/views.py
from django.shortcuts import render
# Create your views here.
import json #json은 자바스크립트의 오브젝트를 나열한 데이터타입! 파이썬은 json을 모르기때문에 import 필수!
from django.views import View #http 렌더링 및 http response의 메소드 구별을 위해 import 필수!
from django.http import JsonResponse #파이썬은 json을 모름. 그래서! http response를json으로 응답하기 위해서 import 필수!
from .models import Users #생성한"Users"라는 클래스에 지금의 views.py을 적용하고자 import!
class UserView(View):
def post(self, request):
data = json.loads(request.body)
Users(
name = data['name'],
email = data['email'],
password = data['password'],
).save()
return JsonResponse({'message':'It\'s success!'}, status = 200)
class AuthView(View):
def post(self, request):
data = json.loads(request.body)
user = Users.objects.get(email=data['email'])
if data['password'] == users.password:
return JsonResponse({'message':'Thank you for Login!'}, status = 200)
else:
return JsonResponse({'message':'Invalid Passowrd!!'}, status = 401)
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
위에서 생성된 views.py를 사용하기 위해서는 urls.py를 수정해야해요!
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » pwd
/Users/marie/mynewdjango
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » tree
.
├── db.sqlite3
├── manage.py
├── mynewjangodirec
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-37.pyc
│ │ ├── settings.cpython-37.pyc
│ │ └── urls.cpython-37.pyc
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── user
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-37.pyc
│ ├── admin.cpython-37.pyc
│ └── models.cpython-37.pyc
├── admin.py
├── apps.py
├── migrations
│ ├── 0001_initial.py
│ ├── __init__.py
│ └── __pycache__
│ ├── 0001_initial.cpython-37.pyc
│ └── __init__.cpython-37.pyc
├── models.py
├── tests.py
├── urls.py
└── views.py
"""mynewjangodirec URL Configuration
6 directories, 24 files
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » cat ./user/urls.py
"""mynewjangodirec URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.urls import path
urlpatterns = [
path('', ),
]
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » vi ./user/urls.py
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » cat ./user/urls.py
"""mynewjangodirec URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.urls import path
from .views import UserView, AuthView #이 파일(urls.py)이 위치한 디렉터리의 views.py파일에서 클래스 UserViewdhk 클래스 AuthView를 import!
urlpatterns = [
path('', UserView.as_views()), #".as_view()"는 진입메소드로! http 메소드(get,post)를 알아내는 dispatch() 메소드를 호출한다
path('/auth', AuthView.as_view()),
]
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »