[T28] 장고(Django) 05강_학사관리프로그램 만들기-I
🏆 학사관리 프로그램을 만들기 위한 프로젝트, 애플리케이션, 데이터베이스를 생성한다.$ django-admin startproject studentsProject #프로젝트 생성
$ cd studentsProject
$ python manage.py startapp students #students 애플리케이션 생성
#in studentsProject/settings.py
INSTALLED_APPS = [
#...
'students.apps.StudentsConfig', #애플리케이션 등록
]
#...
#TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Seoul' #타임존 변경
#in students/models.py
from django.db import models
class Student(models.Model): #모델(테이블) 작성
s_name = models.CharField(max_length=100)
s_major = models.CharField(max_length=100)
s_age = models.IntegerField(default=0)
s_grade = models.IntegerField(default=0)
s_gender = models.CharField(max_length=30)
def __str__(self):
return self.s_name
#in students/admin.py
from django.contrib import admin
from students.models import Student
admin.site.register(Student) #Student 모델(테이블) 등록
$ python manage.py makemigrations #변경사항 마이그레이션
...
$ python manage.py migrate #데이터베이스에 마이그레이션 적용
$ python manage.py createsuperuser #관리자 계정 생성
Username:
Email:
Password:
$ python manage.py runserver 0.0.0.0:8000 #웹서버 구동
[T29] 장고(Django) 06강_학사관리프로그램 만들기-II
🏆 학생 등록 및 리스트 페이지를 만들어본다.#in studentsProject/urls.py
from django.contrib import admin
from django.urls import path, include #include까지 import
urlpatterns = [
path('admin/', admin.site.urls),
path('students/', include('students.urls')),
]
#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'),
]
#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']
#DB에 넣을 Student 인스턴스 만들기
qs = Student(s_name=name, s_major=major, s_age=age, s_grade=grade, s_gender=gender)
qs.save() #DB에 반영
return HttpResponseRedirect(reverse('students:stuAll'))
def reaStudentAll(request): #학생정보 출력
qs = Student.objects.all()
context = {'student_list' : qs}
return render(request, 'students/readStudents.html', context) #context까지 넘겨준다.
<!-- in students/templates/students/registerStudent.html -->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-Type" content="text/html"; charset="UTF-8">
<title>title</title>
</head>
<body>
<p>Register Student</p>
<form action="{% url 'students:regCon' %}" method="post">
{% csrf_token %}
name : <input type="text" name="name"></br>
major : <input type="text" name="major"></br>
age : <input type="text" name="age"></br>
grade : <input type="text" name="grade"></br>
gender : <input type="text" name="gender"></br>
<input type="submit" name="Register"></br>
</form>
</body>
</html>
<!-- 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="">{{s.s_name}}</a>
<a href="#none">수정</a></br>
<a href="#none">삭제</a>
</li>
{% endfor %}
</ol>
{% else %}
<p>student: 0</p>
{% endif %}
</br>
<a href="{% url 'students:reg' %}">신규학생등록</a>
</body>
</html>