(ORM 미사용)


#1/4

C:\Users\SeYun\OneDrive\바탕 화면\Test_0723\myproject\myproject\settings.py

#ip 주소 설정
ALLOWED_HOSTS = ['192.168.0.15']  # 또는 모든 요청 허용

DATABASES = {
     'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb', # 데이터베이스 이름
        'USER': 'root',
        'PASSWORD': '1234',
        'HOST': '192.168.0.15', # 본인 호스트 ip 주소
        'PORT': '3306',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
        }
    }
}

#2/4

C:\Users\SeYun\OneDrive\바탕 화면\Test_0723\myproject\myapp\views.py

import mariadb
import json

def index(request):
    return render(request, 'myapp/index.html')

# DB 연결 함수
def get_connection():
    db = settings.DATABASES['default']
    return mariadb.connect(
        user=db['USER'],
        password=db['PASSWORD'],
        host=db['HOST'],
        port=int(db['PORT']),
        database=db['NAME']
    )

@csrf_exempt
def save_data(request):
    if request.method == 'POST':
        data = json.loads(request.body)
        name = int(data.get('name'))
        number = int(data.get('number'))
        try:
            conn = get_connection()
            cur = conn.cursor()
            cur.execute("INSERT INTO psy (name, number) VALUES (?, ?)", (name, number))
            conn.commit()
            conn.close()
            return JsonResponse({'status': 'ok'})
        except Exception as e:
            return JsonResponse({'status': 'error', 'message': str(e)})

#3/4

C:\Users\SeYun\OneDrive\바탕 화면\Test_0723\myproject\myapp\models.py

managed = False # django 접근 막는 명령

#4/4