상세 컨텐츠

본문 제목

Django restframework 설치, 장고 api 설치 및 개발

Dev

by cyborgangel 2023. 4. 27. 11:48

본문

728x90
반응형
SMALL

*** 기본 구성 : windows11, vscode, sqlite3

 

기본적인 DRF를 구성해보자.

  1. 가상환경 만들기

command : python3 -m venv env

 

  1. 가상환경 activation

리눅스 : command :source env/bin/activate

윈도우 : command :cd  env/Scripts  → activate 

 

  1. Django 설치

command :pip install django==3.2.5

 

  1. Django admin 으로 main web server 설치

command :django-admin startproject edubrain

 

  1. DRF 설치

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'




  1. App “GPT” 생성

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

          —--------------------------------------------------------------------------------------------------------------------------



  1. Hello Django API 만들어 보기

 

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),

]

 

 

  1. 서버 실행 & 확인

command : python manage.py runserver

 

http://127.0.0.1:8000/  접속하여 확인 한다.(아래와 같이 나오면 정상 실행)

Page not found (404)

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:

  1. admin/
  2. GPT/

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/ 로 접속하여 페이지가 나오면 정상 실행.

 

728x90
반응형
LIST

관련글 더보기

댓글 영역