From 5a29493d28153b5b2bb60fde2fb648822824abfe Mon Sep 17 00:00:00 2001 From: Sung Ho Choi Date: Mon, 15 May 2023 21:00:37 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9E=A5=EA=B3=A0=20=EC=84=B8=EC=85=98=20?= =?UTF-8?q?=EA=B3=BC=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- django_project/accounts/__init__.py | 0 django_project/accounts/admin.py | 8 +++ django_project/accounts/apps.py | 6 ++ django_project/accounts/models.py | 7 +++ .../accounts/templates/login_form.html | 23 ++++++++ django_project/accounts/templates/signup.html | 23 ++++++++ django_project/accounts/tests.py | 3 + django_project/accounts/urls.py | 8 +++ django_project/accounts/views.py | 36 ++++++++++++ django_project/django_project/settings.py | 5 +- django_project/django_project/urls.py | 1 + django_project/shopping/templates/base.html | 11 +++- django_project/shopping/templates/index.html | 56 +++++++++++++++++- django_project/shopping/templates/search.html | 58 +++++++++++++++++++ django_project/shopping/urls.py | 1 + django_project/shopping/views.py | 24 +++++++- django_project/static/css/login.css | 26 +++++++++ 17 files changed, 290 insertions(+), 6 deletions(-) create mode 100644 django_project/accounts/__init__.py create mode 100644 django_project/accounts/admin.py create mode 100644 django_project/accounts/apps.py create mode 100644 django_project/accounts/models.py create mode 100644 django_project/accounts/templates/login_form.html create mode 100644 django_project/accounts/templates/signup.html create mode 100644 django_project/accounts/tests.py create mode 100644 django_project/accounts/urls.py create mode 100644 django_project/accounts/views.py create mode 100644 django_project/shopping/templates/search.html create mode 100644 django_project/static/css/login.css diff --git a/django_project/accounts/__init__.py b/django_project/accounts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django_project/accounts/admin.py b/django_project/accounts/admin.py new file mode 100644 index 0000000..bdc87a2 --- /dev/null +++ b/django_project/accounts/admin.py @@ -0,0 +1,8 @@ +from django.contrib import admin +from .models import User + +# Register your models here. +class UserAdmin(admin.ModelAdmin): + pass + +admin.site.register(User, UserAdmin) \ No newline at end of file diff --git a/django_project/accounts/apps.py b/django_project/accounts/apps.py new file mode 100644 index 0000000..3e3c765 --- /dev/null +++ b/django_project/accounts/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class AccountsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'accounts' diff --git a/django_project/accounts/models.py b/django_project/accounts/models.py new file mode 100644 index 0000000..644c09e --- /dev/null +++ b/django_project/accounts/models.py @@ -0,0 +1,7 @@ +from django.db import models +from django.contrib.auth.models import AbstractUser + +# Create your models here. +class User(AbstractUser): + phone = models.CharField(max_length=11) + diff --git a/django_project/accounts/templates/login_form.html b/django_project/accounts/templates/login_form.html new file mode 100644 index 0000000..9fa515a --- /dev/null +++ b/django_project/accounts/templates/login_form.html @@ -0,0 +1,23 @@ + +{% extends 'base.html'%} + + +{% block content %} + +{% load static %} + +
+
+
+
+ {% csrf_token %} + +
+ + +
+
+
+
+{% endblock %} \ No newline at end of file diff --git a/django_project/accounts/templates/signup.html b/django_project/accounts/templates/signup.html new file mode 100644 index 0000000..45b0832 --- /dev/null +++ b/django_project/accounts/templates/signup.html @@ -0,0 +1,23 @@ + +{% extends 'base.html'%} + +{% block content %} + + {% load static %} + +
+
+
+
+ {% csrf_token %} + +
+
+ + +
+
+
+
+{% endblock %} \ No newline at end of file diff --git a/django_project/accounts/tests.py b/django_project/accounts/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/django_project/accounts/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/django_project/accounts/urls.py b/django_project/accounts/urls.py new file mode 100644 index 0000000..3964870 --- /dev/null +++ b/django_project/accounts/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('login/', views.login, name='login'), + path('sign-up/', views.signup, name='signup'), + path('logout/', views.logout, name='logout'), +] \ No newline at end of file diff --git a/django_project/accounts/views.py b/django_project/accounts/views.py new file mode 100644 index 0000000..11ca336 --- /dev/null +++ b/django_project/accounts/views.py @@ -0,0 +1,36 @@ +from django.shortcuts import render, redirect +from django.contrib import auth +from django.contrib.auth import get_user_model + +User = get_user_model() + +# Create your views here. + +def login(request): + if request.method == 'POST': + userid = request.POST['id'] + password = request.POST['password'] + user = auth.authenticate(request, username=userid, password=password) + if user is not None: + auth.login(request, user) + return redirect('list_page') + else: + return render(request, 'login_form.html') + else: # GET 요청 + return render(request, 'login_form.html') + +def signup(request): + if request.method == 'POST': + user = User.objects.create_user( + username = request.POST['id'], + password = request.POST['password'], + phone = request.POST['phone'] + ) + auth.login(request, user) + return redirect('list_page') + else: + return render(request, 'signup.html') + +def logout(request): + auth.logout(request) + return redirect('list_page') \ No newline at end of file diff --git a/django_project/django_project/settings.py b/django_project/django_project/settings.py index aca864f..b9e1660 100644 --- a/django_project/django_project/settings.py +++ b/django_project/django_project/settings.py @@ -41,6 +41,7 @@ 'shopping', 'store', + 'accounts', ] MIDDLEWARE = [ @@ -132,4 +133,6 @@ # media MEDIA_URL = '/media/' -MEDIA_ROOT = os.path.join(BASE_DIR, 'media') \ No newline at end of file +MEDIA_ROOT = os.path.join(BASE_DIR, 'media') + +AUTH_USER_MODEL = 'accounts.User' \ No newline at end of file diff --git a/django_project/django_project/urls.py b/django_project/django_project/urls.py index a2e5d9f..ec1f34e 100644 --- a/django_project/django_project/urls.py +++ b/django_project/django_project/urls.py @@ -22,6 +22,7 @@ path('admin/', admin.site.urls), path('shopping/', include('shopping.urls')), path('store/', include('store.urls')), + path('accounts/', include('accounts.urls')), ] if settings.DEBUG: diff --git a/django_project/shopping/templates/base.html b/django_project/shopping/templates/base.html index bf8eb25..31fc075 100644 --- a/django_project/shopping/templates/base.html +++ b/django_project/shopping/templates/base.html @@ -23,7 +23,16 @@ diff --git a/django_project/shopping/templates/index.html b/django_project/shopping/templates/index.html index 37aee57..4b33277 100644 --- a/django_project/shopping/templates/index.html +++ b/django_project/shopping/templates/index.html @@ -1,4 +1,13 @@ {% extends "base.html" %} + +{% block search %} +
+ {% csrf_token %} + + +
+{% endblock %} + {% block content %}
@@ -6,6 +15,17 @@

My Shopping List

쇼핑할 물건의 목록을 반환하는 페이지입니다

+
+
+ + +

@@ -14,7 +34,7 @@

My Shopping List

- {% for item in item_list %} + {% for item in paginated_list %} +
{% endblock %} \ No newline at end of file diff --git a/django_project/shopping/templates/search.html b/django_project/shopping/templates/search.html new file mode 100644 index 0000000..c1d3b2a --- /dev/null +++ b/django_project/shopping/templates/search.html @@ -0,0 +1,58 @@ +{% extends "base.html" %} + +{% block search %} +
+ {% csrf_token %} + + +
+{% endblock %} + +{% block content %} + +
+
+
+

Search

+

검색 결과를 반환하는 페이지입니다

+
+
+
+ +
+ +
+{% endblock %} \ No newline at end of file diff --git a/django_project/shopping/urls.py b/django_project/shopping/urls.py index fa0690a..f217bb6 100644 --- a/django_project/shopping/urls.py +++ b/django_project/shopping/urls.py @@ -10,4 +10,5 @@ path('formcreate/', views.formcreate, name='formcreate'), # django modelform을 이용해 쇼핑 아이템(Shopping) 객체 만들기 path('modelformcreate/', views.modelformcreate, name='modelformcreate'), + path('search/', views.search, name='search_item'), ] \ No newline at end of file diff --git a/django_project/shopping/views.py b/django_project/shopping/views.py index 4e1f557..1f18334 100644 --- a/django_project/shopping/views.py +++ b/django_project/shopping/views.py @@ -1,11 +1,27 @@ from django.shortcuts import get_object_or_404, render, redirect +from django.contrib.auth.decorators import login_required from .models import Shopping from store.models import Store from .forms import ShoppingForm, ShoppingModelForm +from django.core.paginator import Paginator def shopping_list(request): - qs = Shopping.objects.all() - return render(request, 'index.html', {'item_list': qs,}) + sort_criteria = request.GET.get('sorting', 'descending-price') + if sort_criteria == 'descending-price': + qs = Shopping.objects.order_by('-price') + elif sort_criteria == 'ascending-price': + qs = Shopping.objects.order_by('price') + elif sort_criteria == 'name': + qs = Shopping.objects.order_by('name') + page = request.GET.get('page', '1') + paginator = Paginator(qs, '2') #Paginator(분할될 객체, 페이지 당 담길 객체수) + paginated_qs = paginator.get_page(page) + return render(request, 'index.html', {'paginated_list': paginated_qs, 'sorting': sort_criteria}) + +def search(request): + oq = request.POST['search'] + qs = Shopping.objects.filter(name__contains=oq) + return render(request,'search.html', {'item_list': qs}) def detail(request, pk): item = get_object_or_404(Shopping, pk=pk) @@ -39,6 +55,7 @@ def formcreate(request): form = ShoppingForm() return render(request, 'form_create.html', {'form':form}) +@login_required def modelformcreate(request): if request.method == 'POST': form = ShoppingModelForm(request.POST) @@ -47,4 +64,5 @@ def modelformcreate(request): return redirect('list_page') else: form = ShoppingModelForm() - return render(request, 'form_create.html', {'form':form}) \ No newline at end of file + return render(request, 'form_create.html', {'form':form}) + \ No newline at end of file diff --git a/django_project/static/css/login.css b/django_project/static/css/login.css new file mode 100644 index 0000000..2f8a6a1 --- /dev/null +++ b/django_project/static/css/login.css @@ -0,0 +1,26 @@ +#section { + background-color: white; +} + +#form { + border : 2px solid #eee; + border-radius: 15px; + padding: 50px; +} +#form input { + margin: 10px 0; + width: 100%; + border-radius: 5px; + padding: 5px; + text-indent: 10px; +} +#skhbutton { + background-color: rgba(255, 128, 0, 0.7); + color:white; + border: none; + border-bottom: 4px solid rgba(255,128,0,1); +} +#skhbutton:active{ + border-bottom: none; + border-top: 4px solid white; +} \ No newline at end of file