diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..13566b8
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
new file mode 100644
index 0000000..cd4192d
--- /dev/null
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml
new file mode 100644
index 0000000..105ce2d
--- /dev/null
+++ b/.idea/inspectionProfiles/profiles_settings.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..4d5a890
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..97319a1
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/swpp-p6-django-tutorial.iml b/.idea/swpp-p6-django-tutorial.iml
new file mode 100644
index 0000000..2e28675
--- /dev/null
+++ b/.idea/swpp-p6-django-tutorial.iml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
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..e9c983b 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-10 15:37
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..e5ef436 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-10 18:06
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..3551922 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-10 18:25
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..5577e28 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-10 18: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..7384e1e 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.hero_list),
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('info//', views.hero_info, name='hero_info'),
]
\ No newline at end of file
diff --git a/toh/hero/views.py b/toh/hero/views.py
index 38df04b..e0cea11 100644
--- a/toh/hero/views.py
+++ b/toh/hero/views.py
@@ -1,27 +1,37 @@
-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')
-
def id(request, id):
return HttpResponse(f'Your id is {id}!')
def name(request, name):
return HttpResponse(f'Your name is {name}!')
+@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']
+ 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'])
+
@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 +46,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/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..731ead0 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.2.
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-x*cal+4dr^5wqe&0modde7$72#ck2zxe-&e=uhfvdal6(22!o)'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
-ALLOWED_HOSTS = []
+ALLOWED_HOSTS = ['127.0.0.1','0.0.0.0', 'localhost']
# 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..8c57ad4 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
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