[T30] 장고(Django) 07강_학사관리프로그램 만들기-III
🏆 학사관리 프로그램에서 학생 정보보기 기능, 정보수정 기능, 정보삭제 기능을 만들어본다.<!-- in students/templates/students/readStudents.html -->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-Type" content="text/html"; charset="UTF-8">
<title>title</title>
</head>
<body>
<p>Read Students</p>
{% if student_list %}
<ol>
{% for s in student_list %}
<li>
<a href="{% url 'students:stuDet' s.s_name %}">{{s.s_name}}</a>
<a href="#none">수정</a>
<a href="#none">삭제</a>
</li>
{% endfor %}
</ol>
{% else %}
<p>student: 0</p>
{% endif %}
</br>
<a href="{% url 'students:reg' %}">신규학생등록</a>
</body>
</html>
#in students/urls.py
from django.urls import path
from . import views
app_name = 'students'
urlpatterns = [
path('reg/', views.regStudent, name='reg'),
path('regCon/', views.regConStudent, name='regCon'),
path('all/', views.reaStudentAll, name='stuAll'),
path('<str:name>/det/', views.detStudent, name='stuDet'), #추가!
path('<str:name>/mod/', views.reaStudentOne, name='stuMod'), #추가!
path('modCon/', views.modConStudent, name='modCon'), #추가!
path('<str:name>/del/', views.delConStudent, name='stuDel'), #추가!
]
#in students/views.py
from http.client import HTTPResponse
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from students.models import Student
def regStudent(request): #학생 등록
return render(request, 'students/registerStudent.html')
def regConStudent(request): #학생정보 DB에 넣기
name = request.POST['name']
major = request.POST['major']
age = request.POST['age']
grade = request.POST['grade']
gender = request.POST['gender']
qs = Student(s_name=name, s_major=major, s_age=age, s_grade=grade, s_gender=gender)
qs.save()
return HttpResponseRedirect(reverse('students:stuAll'))
def reaStudentAll(request): #학생정보 출력
qs = Student.objects.all()
context = {'student_list' : qs}
return render(request, 'students/readStudents.html', context) #context까지 넘겨준다.
def detStudent(request, name): #추가된 부분, 학생 정보의 디테일로 name도 매개변수로 받는다.
qs = Student.objects.get(s_name = name)
context = {'student_info' : qs}
return render(request, 'students/detailStudent.html', context)
def reaStudentOne(request, name): #추가된 부분, 수정을 위해 학생 하나를 읽는다.
qs = Student.objects.get(s_name = name)
context = {'student_info' : qs}
return render(request, 'students/modifyStudent.html', context)
def modConStudent(request): #추가된 부분, 수정 내용을 반영한다.
name = request.POST['name']
major = request.POST['major']
age = request.POST['age']
grade = request.POST['grade']
gender = request.POST['gender']
s_qs = Student.objects.get(s_name = name)
s_qs.s_name = name
s_qs.s_major = major
s_qs.s_age = age
s_qs.s_grade = grade
s_qs.s_gender = gender
s_qs.save()
return HttpResponseRedirect(reverse('students:stuAll'))
def delConStudent(request, name): #추가된 부분, 학생 정보를 삭제한다.
s_qs = Student.objects.get(s_name = name)
s_qs.delete()
return HttpResponseRedirect(reverse('students:stuAll'))
<!-- in students/templates/students/detailStudent.html -->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-Type" content="text/html"; charset="UTF-8">
<title>title</title>
</head>
<body>
<p>Detail Students</p>
{% if student_info %}
name: {{student_info.s_name}}</br>
major: {{student_info.s_major}}</br>
age: {{student_info.s_age}}</br>
grade: {{student_info.s_grade}}</br>
gender: {{student_info.s_gender}}</br>
{% else %}
<p>student: 0</p>
{% endif%}
</br>
<a href="{% url 'students:stuMod' student_info.s_name %}">수정</a>
<a href="{% url 'students:stuDel' student_info.s_name %}">삭제</a>
<a href="{% url 'students:stuAll' %}">학생리스트</a>
<a href="{% url 'students:reg' %}">신규학생등록</a>
</body>
</html>
<!-- in students/templates/students/modifyStudent.html -->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-Type" content="text/html"; charset="UTF-8">
<title>title</title>
</head>
<body>
<p>Modify Student</p>
{% if student_info %}
<form action="{% url 'students:modCon' %}" method="post">
{% csrf_token %}
name : {{student_info.s_name}}</br>
<input type="hidden" name="name" value={{student_info.s_name}}></br>
major : <input type="text" name="major" value={{student_info.s_major}}></br>
age : <input type="text" name="age" value={{student_info.s_age}}></br>
grade : <input type="text" name="grade" value={{student_info.s_grade}}></br>
gender : <input type="text" name="gender" value={{student_info.s_gender}}></br>
<input type="submit" name="Modify"></br>
</form>
{% else %}
<p>student: 0</p>
{% endif %}
</body>
</html>