diff --git a/toh/README.md b/toh/README.md deleted file mode 100644 index 6e3b7b7..0000000 --- a/toh/README.md +++ /dev/null @@ -1,17 +0,0 @@ -# SWPP Practice Session #6 - -**Due: 10/3 (Thur), 21:00** - -### Today's Task -1. Fork this repo. -2. Implement today's tasks (total 3). -3. Send PR to this repo's main branch. - -Note that you need to start from creating a new Django project. (No skeleton code) -Run `django-admin startproject toh` at the root of this repo. - -``` -git clone https://github.com/{YOUR_GITHUB_ID}/swpp-p6-django-tutorial.git -cd swpp-p6-django-tutorial -django-admin startproject toh -``` diff --git a/toh/hero/migrations/0001_initial.py b/toh/hero/migrations/0001_initial.py index 707c62f..2baafdc 100644 --- a/toh/hero/migrations/0001_initial.py +++ b/toh/hero/migrations/0001_initial.py @@ -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 diff --git a/toh/hero/migrations/0002_hero_age.py b/toh/hero/migrations/0002_hero_age.py index 0f78298..f7f1c7a 100644 --- a/toh/hero/migrations/0002_hero_age.py +++ b/toh/hero/migrations/0002_hero_age.py @@ -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 diff --git a/toh/hero/migrations/0003_remove_hero_age.py b/toh/hero/migrations/0003_remove_hero_age.py deleted file mode 100644 index 2602050..0000000 --- a/toh/hero/migrations/0003_remove_hero_age.py +++ /dev/null @@ -1,17 +0,0 @@ -# Generated by Django 3.2.6 on 2021-10-04 14:49 - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('hero', '0002_hero_age'), - ] - - operations = [ - migrations.RemoveField( - model_name='hero', - name='age', - ), - ] diff --git a/toh/hero/migrations/0005_team.py b/toh/hero/migrations/0003_team.py similarity index 85% rename from toh/hero/migrations/0005_team.py rename to toh/hero/migrations/0003_team.py index da724b3..bf4027a 100644 --- a/toh/hero/migrations/0005_team.py +++ b/toh/hero/migrations/0003_team.py @@ -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 @@ -7,7 +7,7 @@ class Migration(migrations.Migration): dependencies = [ - ('hero', '0004_hero_age'), + ('hero', '0002_hero_age'), ] operations = [ @@ -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')), ], ), ] diff --git a/toh/hero/migrations/0004_hero_age.py b/toh/hero/migrations/0004_hero_age.py deleted file mode 100644 index 0fa9f9d..0000000 --- a/toh/hero/migrations/0004_hero_age.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 3.2.6 on 2021-10-04 14:49 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('hero', '0003_remove_hero_age'), - ] - - operations = [ - migrations.AddField( - model_name='hero', - name='age', - field=models.IntegerField(blank=True, null=True), - ), - ] diff --git a/toh/hero/migrations/0006_hero_score.py b/toh/hero/migrations/0004_hero_score.py similarity index 78% rename from toh/hero/migrations/0006_hero_score.py rename to toh/hero/migrations/0004_hero_score.py index 6644fd8..e1acbc5 100644 --- a/toh/hero/migrations/0006_hero_score.py +++ b/toh/hero/migrations/0004_hero_score.py @@ -1,4 +1,4 @@ -# 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 @@ -6,7 +6,7 @@ class Migration(migrations.Migration): dependencies = [ - ('hero', '0005_team'), + ('hero', '0003_team'), ] operations = [ diff --git a/toh/hero/models.py b/toh/hero/models.py index 03e678a..b10832f 100644 --- a/toh/hero/models.py +++ b/toh/hero/models.py @@ -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}') diff --git a/toh/hero/urls.py b/toh/hero/urls.py index 0563bad..87065f5 100644 --- a/toh/hero/urls.py +++ b/toh/hero/urls.py @@ -1,11 +1,9 @@ from django.urls import path - from . import views urlpatterns = [ - path('', views.index, name='index'), - path('/', views.id, name='hero_id'), - path('/', views.name, name='hero_name'), - path('info/', views.hero_list, name='hero_list'), - path('info//', views.hero_info, name='hero_info') + path('/', views.hero_name, name='hero_name'), + path('/', views.hero_id, name='hero_id'), + path('info//', views.hero_info, name='hero_info'), + path('', views.hero_list) ] \ No newline at end of file diff --git a/toh/hero/views.py b/toh/hero/views.py index 38df04b..8cc52af 100644 --- a/toh/hero/views.py +++ b/toh/hero/views.py @@ -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() @@ -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"]) \ No newline at end of file + return HttpResponseNotAllowed(['GET', 'PUT']) \ No newline at end of file diff --git a/toh/manage.py b/toh/manage.py old mode 100755 new mode 100644 diff --git a/toh/toh/asgi.py b/toh/toh/asgi.py index db671d6..940b9f8 100644 --- a/toh/toh/asgi.py +++ b/toh/toh/asgi.py @@ -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 diff --git a/toh/toh/settings.py b/toh/toh/settings.py index 8e283cd..7d6a61f 100644 --- a/toh/toh/settings.py +++ b/toh/toh/settings.py @@ -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 @@ -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 @@ -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': { @@ -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 = [ { @@ -102,7 +102,7 @@ # Internationalization -# https://docs.djangoproject.com/en/3.2/topics/i18n/ +# https://docs.djangoproject.com/en/4.1/topics/i18n/ LANGUAGE_CODE = 'en-us' @@ -110,17 +110,15 @@ 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' diff --git a/toh/toh/urls.py b/toh/toh/urls.py index edd5052..52afd2c 100644 --- a/toh/toh/urls.py +++ b/toh/toh/urls.py @@ -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 @@ -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')), diff --git a/toh/toh/wsgi.py b/toh/toh/wsgi.py index e54327b..97b4d40 100644 --- a/toh/toh/wsgi.py +++ b/toh/toh/wsgi.py @@ -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