Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lab06_kitae_kim #49

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions toh/README.md

This file was deleted.

2 changes: 1 addition & 1 deletion toh/hero/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.6 on 2021-10-03 13:51
# Generated by Django 4.1.2 on 2022-10-12 16:38

from django.db import migrations, models

Expand Down
2 changes: 1 addition & 1 deletion toh/hero/migrations/0002_hero_age.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.6 on 2021-10-04 14:36
# Generated by Django 4.1.2 on 2022-10-12 17:30

from django.db import migrations, models

Expand Down
17 changes: 0 additions & 17 deletions toh/hero/migrations/0003_remove_hero_age.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.6 on 2021-10-04 17:06
# Generated by Django 4.1.2 on 2022-10-12 17:35

from django.db import migrations, models
import django.db.models.deletion
Expand All @@ -7,7 +7,7 @@
class Migration(migrations.Migration):

dependencies = [
('hero', '0004_hero_age'),
('hero', '0002_hero_age'),
]

operations = [
Expand All @@ -17,7 +17,7 @@ class Migration(migrations.Migration):
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=120)),
('leader', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='leader_set', to='hero.hero')),
('members', models.ManyToManyField(related_name='teams', to='hero.Hero')),
('members', models.ManyToManyField(related_name='teams', to='hero.hero')),
],
),
]
18 changes: 0 additions & 18 deletions toh/hero/migrations/0004_hero_age.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Generated by Django 3.2.6 on 2021-10-04 17:15
# Generated by Django 4.1.2 on 2022-10-12 17:35

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('hero', '0005_team'),
('hero', '0003_team'),
]

operations = [
Expand Down
2 changes: 1 addition & 1 deletion toh/hero/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Hero(models.Model):

def __str__(self):
return self.name

def introduce(self):
print(f'Hello, my name is {self.name} and my score is {self.score}')

Expand Down
10 changes: 4 additions & 6 deletions toh/hero/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from django.urls import path

from . import views

urlpatterns = [
path('', views.index, name='index'),
path('<int:id>/', views.id, name='hero_id'),
path('<str:name>/', views.name, name='hero_name'),
path('info/', views.hero_list, name='hero_list'),
path('info/<int:id>/', views.hero_info, name='hero_info')
path('<str:name>/', views.hero_name, name='hero_name'),
path('<int:id>/', views.hero_id, name='hero_id'),
path('info/<int:id>/', views.hero_info, name='hero_info'),
path('', views.hero_list)
]
57 changes: 27 additions & 30 deletions toh/hero/views.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
from django.http import HttpResponse, JsonResponse, HttpResponseBadRequest
from django.http.response import HttpResponseNotAllowed
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotAllowed, JsonResponse
from django.views.decorators.csrf import csrf_exempt

import json
from json.decoder import JSONDecodeError

from .models import Hero

def index(request):
return HttpResponse('Hello, world!\n')
return HttpResponse('Hello, world')

def id(request, id):
return HttpResponse(f'Your id is {id}!')
def hero_name(request, name):
return HttpResponse(f'Your name is {name}')

def hero_id(request, id):
return HttpResponse(f'Your id is {id}')

@csrf_exempt
def hero_list(request):
if request.method == 'GET':
# dictionary를 list로
hero_all_list = [hero for hero in Hero.objects.all().values()]
return JsonResponse(hero_all_list, safe=False)
elif request.method == 'POST':
try:
body = request.body.decode()
hero_name = json.loads(body)['name']
except (KeyError, JSONDecodeError) as e:
return HttpResponseBadRequest()
hero = Hero(name=hero_name)
hero.save()
response_dict = {'id': hero.id, 'name': hero.name}
return JsonResponse(response_dict, status=201)
else:
return HttpResponseNotAllowed(['GET', 'POST'])

def name(request, name):
return HttpResponse(f'Your name is {name}!')

@csrf_exempt
def hero_info(request, id):
if request.method == 'GET':
hero = Hero.objects.get(id=id)
return JsonResponse({"id": hero.id, "name": hero.name, "age": hero.age})

elif request.method == 'PUT':
try:
body = request.body.decode()
Expand All @@ -36,23 +52,4 @@ def hero_info(request, id):
response_dict = {"id": hero.id, "name": hero.name, "age": hero.age}
return JsonResponse(response_dict, status=200)
else:
return HttpResponseNotAllowed(['GET', 'PUT'])

@csrf_exempt
def hero_list(request):
if request.method == "GET":
hero_all_list = [hero for hero in Hero.objects.all().values()]
return JsonResponse(hero_all_list, safe=False)
elif request.method == "POST":
try:
body = request.body.decode()
hero_name = json.loads(body)['name']
hero_age = json.loads(body)['age']
except (KeyError, JSONDecodeError) as e:
return HttpResponseBadRequest()
hero = Hero(name=hero_name, age=hero_age)
hero.save()
response_dict = {'id': hero.id, 'name': hero.name, 'age': hero.age}
return JsonResponse(response_dict, status=201)
else:
return HttpResponseNotAllowed(["GET", "POST"])
return HttpResponseNotAllowed(['GET', 'PUT'])
Empty file modified toh/manage.py
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion toh/toh/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/
"""

import os
Expand Down
26 changes: 12 additions & 14 deletions toh/toh/settings.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""
Django settings for toh project.

Generated by 'django-admin startproject' using Django 3.2.6.
Generated by 'django-admin startproject' using Django 4.1.1.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
https://docs.djangoproject.com/en/4.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
https://docs.djangoproject.com/en/4.1/ref/settings/
"""

from pathlib import Path
Expand All @@ -17,15 +17,15 @@


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-_7mcsvpz575w%1&4#(u=x(@xoi*33hj^yzqbs&dn3a3bb8x5ri'
SECRET_KEY = 'django-insecure-hw_@1xn(f55-53nf^5hio!wd)+5f6ko_bjtt23x--#+%3y5mrw'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['0.0.0.0','localhost','127.0.0.1']


# Application definition
Expand Down Expand Up @@ -72,7 +72,7 @@


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

DATABASES = {
'default': {
Expand All @@ -83,7 +83,7 @@


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
Expand All @@ -102,25 +102,23 @@


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
# https://docs.djangoproject.com/en/4.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
# https://docs.djangoproject.com/en/4.1/howto/static-files/

STATIC_URL = '/static/'
STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
4 changes: 2 additions & 2 deletions toh/toh/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""toh URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
Expand All @@ -14,7 +14,7 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
from django.urls import path, include

urlpatterns = [
path('hero/', include('hero.urls')),
Expand Down
2 changes: 1 addition & 1 deletion toh/toh/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/
"""

import os
Expand Down