*** 기본 구성 : windows11, vscode, sqlite3
기본적인 DRF를 구성해보자.
command : python3 -m venv env
리눅스 : command :source env/bin/activate
윈도우 : command :cd env/Scripts → activate
command :pip install django==3.2.5
command :django-admin startproject edubrain
command : pip install djangorestframework==3.12.4
settings.py 에 DRF 추가
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
]
time zone 변경
TIME_ZONE = 'Asia/Seoul'
command : python manage.py startapp GPT
settings.py 에 App 추가
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'GPT',
]
settings.py 에 추가 후 migration 진행
command : python manage.py makemigrations
command : python manage.py migrate
--------------------------------------------결과는 아래와 같이 나옴—-------------------------
(env) D:\django\study-2\edubrain>python manage.py makemigrations
No changes detected
(env) D:\django\study-2\edubrain>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying sessions.0001_initial... OK
—--------------------------------------------------------------------------------------------------------------------------
GPT app 의 views.py 에 추가
from rest_framework import viewsets, permissions, status, generics, mixins
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.decorators import api_view
from .serializers import *
@api_view(['GET'])
def helloAPI(request):
return Response("Hello Django!")
URL 설정
edubrain root 의 urls.py 추가
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('GPT', include(GPT.urls)),
]
GPT 폴더의 urls.py 생성
from django.urls import path, include
from .views import helloAPI
urlpatterns = [
path('hello/', helloAPI),
]
command : python manage.py runserver
http://127.0.0.1:8000/ 접속하여 확인 한다.(아래와 같이 나오면 정상 실행)
| Request Method: | GET |
| Request URL: | http://127.0.0.1:8000/ |
Using the URLconf defined in edubrain.urls, Django tried these URL patterns, in this order:
The empty path didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
http://127.0.0.1:8000/GPT/hello/ 로 접속하여 페이지가 나오면 정상 실행.
| wise 해외송금 (1) | 2023.04.08 |
|---|---|
| Django REST Framework _ Postman 사용하기 (1) | 2023.03.23 |
| Django REST Framework _ Postman 사용하기 (1) | 2023.03.14 |
| Ms access 에서 이미지 주소를 이미지로 보여주기 (0) | 2023.02.28 |
| Ajax communication in PHP & sample code (0) | 2023.02.28 |
댓글 영역