์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/19

 

[python ์‹ฌํ™”] 2. ํด๋ž˜์Šค ๊ธฐ์ดˆ, ํด๋ž˜์Šค ๋ณ€์ˆ˜ , ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/18 [python ์‹ฌํ™”] 1. ๊ฐ์ฒด์ง€ํ–ฅ(OOP), ํด๋ž˜์Šค ๊ธฐ์ดˆ ์‚ฌ์šฉ ์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  1. ๊ฐ์ฒด์ง€ํ–ฅ vs ์ ˆ์ฐจ์ง€ํ–ฅ ๊ฐ„๋‹จํ•œ..

silvercoding.tistory.com

 

 

- ํด๋ž˜์Šค ํ™•์žฅ 

์ด๋ฒˆ์—๋Š” ํด๋ž˜์Šค์— ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ, ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ , ์Šคํ…Œ์ดํ‹ฑ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„์„ ํ•ด ๋ณธ๋‹ค. 

class Student(object):
    """
    Student Class
    Author : Kim
    Date : 2021. 6. 28 
    Description : Class, Static, Instance Method
    """

    # Class Variable
    tuition_per = 1.0

    def __init__(self, id, first_name, last_name, email, grade, tuition, gpa):
        self._id = id
        self._first_name = first_name
        self._last_name = last_name
        self._email = email
        self._grade = grade
        self._tuition = tuition
        self._gpa = gpa

    # Instance Method 
    def full_name(self):
        return "{}, {}".format(self._first_name, self._last_name)
    
    # Instance Method
    def detail_info(self):
        return 'Student Detail Info : {}, {}, {}, {}, {}, {}'.format(self._id, self.full_name(), self._email, self._grade, self._tuition, self._gpa)

    # Instance Method
    def get_fee(self):
        return 'Beforoe Tuition -> Id : {}, fee : {}'.format(self._id, self._tuition)

    # Instance Method
    def get_fee_culc(self):
        return 'After tuition -> Id : {}, fee : {}'.format(self._id, self._tuition * Student.tuition_per) 
    
    # Instance Method
    def __str__(self):
        return 'Student Info -> name : {} grade : {} email : {}'.format(self.full_name(), self._grade, self._email)
        
    # Class Method
    @classmethod
    def raise_fee(cls, per):
        if per <= 1:
            print('Please Enter 1 or More')
            return 
        Student.tuition_per = per
        print('Succeed! tuition increased.')

    # Class Method
    @classmethod
    def student_const(cls, id, first_name, last_name, email, grade, tuition, gpa):
        return cls(id, first_name, last_name, email, grade, tuition * cls.tuition_per, gpa)

    # Static Method
    @staticmethod # ---> ์š”์ƒˆ๋Š” ์ž˜ ์“ฐ์ง„ ์•Š๋Š”๋‹ค. 
    def is_scolarship_st(inst):
        if inst._gpa >= 4.3:
            return '{} is a scholarship recipient'.format(inst._last_name)
        return 'Sorry, Not a scholarship recipient'

Instance Method๋Š” self์˜ ์ ‘๊ทผ์ž(๊ฐœ๊ฐœ์ธ, ๊ณ ์œ ํ•œ ๊ฐ์ฒด)๋ฅผ ์ด์šฉํ•˜์—ฌ ์†์„ฑ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฒซ๋ฒˆ์งธ ์ธ์ž์— self๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์€ ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ๋ผ๊ณ  ์ƒ๊ฐ์„ ํ•˜๋ฉด ๋œ๋‹ค. 

 

Class Method ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ํด๋ž˜์Šค๋ฅผ ๋ฐ›์•„์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๋Œ€๋ถ€๋ถ„ cls๋ผ๊ณ  ์“ด๋‹ค. ๋˜ , ๋ฉ”์†Œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์ „์— ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. (@classmethod) ํด๋ž˜์Šค ์ž์ฒด๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

Static Method ์ผ๋ฐ˜ ํ•จ์ˆ˜์˜ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. ์ฆ‰, ํด๋ž˜์Šค ์•ˆ์— ๊ตณ์ด ๋„ฃ์ง€ ์•Š์•„๋„ ๋˜๊ณ  ํด๋ž˜์Šค ๋ฐ–์—์„œ ์ƒ์„ฑํ•ด๋„ ๋˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. ๊ทธ๋ž˜๋„ ์œ„์—์ฒ˜๋Ÿผ ์žฅํ•™๊ธˆ ๋ฐ›๋Š”์ง€์— ๋Œ€ํ•œ ์—ฌ๋ถ€๋Š” studentํด๋ž˜์Šค์™€ ์—ฐ๊ด€์ด ๋˜์–ด์žˆ์œผ๋ฏ€๋กœ ์•ˆ์— ์จ์ฃผ๋Š” ๊ฒƒ์ด ๋” ๋ณด๊ธฐ ํŽธํ•  ๊ฒƒ์ด๋‹ค. 

 

 

 

- ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ 

student_1 = Student(1, 'Kim', "silver", 'student1@naver.com', '1', 400, 3.5)
student_2 = Student(2, 'Han', "golye", 'student2@daum.net', '2', 500, 4.3)
# ๊ธฐ๋ณธ์ •๋ณด 
print(student_1)
print(student_2)

print()

# ์ „์ฒด ์ •๋ณด
print(student_1.detail_info())
print(student_2.detail_info())

 

 

- ๋“ฑ๋ก๊ธˆ 

# ํ•™๋น„ ์ •๋ณด (๋“ฑ๋ก๊ธˆ ์ธ์ƒ ์ „)
print(student_1.get_fee())
print(student_2.get_fee())

* ํ•™๋น„ ์ธ์ƒ (ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ)

# ํ•™๋น„ ์ธ์ƒ (ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ ๋ฏธ์‚ฌ์šฉ) ---> ์ด๋ ‡๊ฒŒ ์ง์ ‘ ์ ‘๊ทผํ•˜๋Š” ๊ฑฐ ์ข‹์ง€ ์•Š๋‹ค. 
# Student.tuition_per = 1.2

# ํ•™๋น„ ์ธ์ƒ (ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ) 
Student.raise_fee(1.2)

* ์ธ์ƒ ์‹œํ‚จ ํ›„์˜ ํ•™๋น„ ์ •๋ณด 

print(student_1.get_fee_culc())
print(student_2.get_fee_culc())

 

 

- ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑํ•˜๊ธฐ 

student_3 = Student.student_const(3, 'Park', 'silver', 'Student3@gmail.com', '3', 550, 4.5)
student_4 = Student.student_const(4, 'Cho', 'golye', 'student4@gmail.com', '4', 600, 4.1)

Class Method๋กœ ์ƒ์„ฑํ•ด ๋†“์•˜๋˜ student_const๋ฅผ ์ด์šฉํ•˜์—ฌ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค.  

 

*** ๋˜‘๊ฐ™์ด ์‹ค์Šต 

# ์ „์ฒด ์ •๋ณด 
print(student_3.detail_info())
print(student_4.detail_info())
print()

# ํ•™์ƒ ํ•™๋น„ ๋ณ€๊ฒฝ ํ™•์ธ
print(student_3._tuition)
print(student_4._tuition)
print()

 

 

- Static Method ๋ฅผ ์ด์šฉํ•˜์—ฌ ์žฅํ•™๊ธˆ ํ˜œํƒ ์—ฌ๋ถ€ ์•Œ์•„๋ณด๊ธฐ 

(1) static method ๋ฏธ์‚ฌ์šฉ 

def is_scholarship(inst):
    if inst._gpa >= 4.3:
        return '{} is a scholarship recipient'.format(inst._last_name)
    return 'Sorry, Not a scholarship recipient'

์ด์ฒ˜๋Ÿผ ํด๋ž˜์Šค ์•ˆ์— ๊ตณ์ด ๋„ฃ์ง€ ์•Š์•„๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

* ์‚ฌ์šฉ

print(is_scholarship(student_1))
print(is_scholarship(student_2))
print(is_scholarship(student_3))
print(is_scholarship(student_4))

์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜์— ์ธ์Šคํ„ด์Šค๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ์žฅํ•™๊ธˆ ๋Œ€์ƒ ์—ฌ๋ถ€๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค. 

 

(2) static method ์‚ฌ์šฉ 

Student ํด๋ž˜์Šค ์•ˆ์— ๊ตฌํ˜„ํ•ด ๋†“์€ static method์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. 

print(Student.is_scolarship_st(student_1))
print(Student.is_scolarship_st(student_2))
print(Student.is_scolarship_st(student_3))
print(Student.is_scolarship_st(student_4))


print(student_1.is_scolarship_st(student_1))
print(student_2.is_scolarship_st(student_2))
print(student_3.is_scolarship_st(student_3))
print(student_4.is_scolarship_st(student_4))

Student ํด๋ž˜์Šค ์ž์ฒด๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๊ณ , ๊ฐ์ฒด๋ฅผ ํ†ตํ•œ ์ ‘๊ทผ ๋˜ํ•œ ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/18

 

[python ์‹ฌํ™”] 1. ๊ฐ์ฒด์ง€ํ–ฅ(OOP), ํด๋ž˜์Šค ๊ธฐ์ดˆ ์‚ฌ์šฉ

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  1. ๊ฐ์ฒด์ง€ํ–ฅ vs ์ ˆ์ฐจ์ง€ํ–ฅ ๊ฐ„๋‹จํ•œ ๋น„๊ต ํŒŒ์ด์ฌ์€ OOP (๊ฐ์ฒด์ง€ํ–ฅ) ์–ธ์–ด์ด๋‹ค.  ๊ฐ์ฒด์ง€ํ–ฅ๊ณผ ์ ˆ์ฐจ์ง€ํ–ฅ์€ ๋•Œ์— ๋”ฐ๋ผ ์žฅ์ ๊ณผ ๋‹จ์ ์ด ์žˆ๋‹ค. ๊ฐ์ฒด์ง€ํ–ฅ์„ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์ฝ”๋“œ์˜ ์žฌ

silvercoding.tistory.com

 

 

 

ํด๋ž˜์Šค ํ™•์žฅ 

Student ํด๋ž˜์Šค์— ํด๋ž˜์Šค ๋ณ€์ˆ˜์™€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ด์— ๋Œ€ํ•œ ๊ฐœ๋…์„ ์ดํ•ด ํ•ด๋ณด์ž.

- ํด๋ž˜์Šค ์ƒ์„ฑ 

class Student():
    """
    Student Class
    Author : silver
    Date : 2021. 06. 30 
    """

    # ํด๋ž˜์Šค ๋ณ€์ˆ˜ 
    student_count = 0 

    def __init__(self, name, number, grade, details, email=None):
        # ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜
        self._name = name
        self._number = number
        self._grade = grade
        self._details = details
        self._email = email

        Student.student_count += 1 

    def __str__(self):
        return 'str {}'.format(self._name)

    def __repr__(self):
        return 'repr {}'.format(self._name)

    def detail_info(self):
        print('Current Id : {}'.format(id(self)))
        print('Student Detail Info : {} {} {}'.format(self._name, self._email, self._details))

    def __del__(self):
        Student.student_count -= 1 

์šฐ์„  ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ด ์ค€๋‹ค. ์ฃผ์„์œผ๋กœ ํด๋ž˜์Šค ๋ณ€์ˆ˜์™€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ๊ตฌ๋ถ„ํ•ด ๋†“์•˜๋‹ค. 

๋ง๊ทธ๋Œ€๋กœ ํด๋ž˜์Šค ๋ณ€์ˆ˜๋Š” ํด๋ž˜์Šค ์ž์ฒด์—์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•œ ๋ณ€์ˆ˜, ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋Š” ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•œ ๋ณ€์ˆ˜์ด๋‹ค. ๋”ฐ๋ผ์„œ ํด๋ž˜์Šค ์•ˆ์— student_count ํด๋ž˜์Šค ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ , ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ์—์„œ ์“ด ๊ฒƒ๊ณผ ๊ฐ™์ด Student.student_count += 1 ์ฒ˜๋Ÿผ ํด๋ž˜์Šค๋กœ ์ ‘๊ทผํ•˜์—ฌ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋Š” self๊ฐ€ ๋ถ™์–ด ์žˆ๋‹ค. ๊ฐ์ฒด๋ฅผ ํ†ตํ•˜์—ฌ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

 

 

 

- ๊ฐ์ฒด ์ƒ์„ฑ 

studt1 = Student('Cho', 2, 3, {'gender': 'Male', 'score1': 65, 'score2': 44})
studt2 = Student('chang', 4, 1, {'gender': 'Female', 'score1': 85, 'score2': 74}, "onestein@google.com")

 ๋‘ ๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ์ค€๋‹ค. 

 

 

- id ํ™•์ธ 

print(id(studt1))
print(id(studt2))

 

 

*** == , is ์˜ ์ฐจ์ด 

== : ๊ฐ’์„ ๋น„๊ต 

print(studt1._name == studt2._name)

is : id ๊ฐ’์„ ๋น„๊ต 

print(studt1 is studt2)

 

 

- dir & __dict__ & __doc__

(1) ๋‚ด์žฅํ•จ์ˆ˜ dir 

https://wikidocs.net/10307

 

์œ„ํ‚ค๋…์Šค

์˜จ๋ผ์ธ ์ฑ…์„ ์ œ์ž‘ ๊ณต์œ ํ•˜๋Š” ํ”Œ๋žซํผ ์„œ๋น„์Šค

wikidocs.net

dir() ๋‚ด์žฅ ํ•จ์ˆ˜๋Š” ์–ด๋–ค ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์–ด๋–ค ๋ณ€์ˆ˜์™€ ๋ฉ”์†Œ๋“œ(method)๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ๋‚˜์—ดํ•ด ์ค€๋‹ค. 

print(dir(studt1))
print(dir(studt2))

๋”ฐ๋ผ์„œ Student์˜ ๊ฐ์ฒด studt1, studt2๋ฅผ dir ์ธ์ž์— ๋„ฃ์–ด์„œ ๋ณ€์ˆ˜์™€ ๋ฉ”์†Œ๋“œ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ํด๋ž˜์Šค๋ณ€์ˆ˜๊นŒ์ง€ ๋ชจ๋‘ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

(2) __dict__ 

https://wikidocs.net/1743

 

์œ„ํ‚ค๋…์Šค

์˜จ๋ผ์ธ ์ฑ…์„ ์ œ์ž‘ ๊ณต์œ ํ•˜๋Š” ํ”Œ๋žซํผ ์„œ๋น„์Šค

wikidocs.net

ํด๋ž˜์Šค ๋˜๋Š” ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•œ ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ํ™•์ธํ•œ๋‹ค. 

print(studt1.__dict__)
print(studt2.__dict__)

์ด๋ ‡๊ฒŒ dictionary ๊ตฌ์กฐ๋กœ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. 

 

 

 

(3) __doc__ 

print(Student.__doc__)

__doc__ ํด๋ž˜์Šค ์•ˆ์—์žˆ๋Š” ์„ค๋ช… ์ฃผ์„์„ ๋ณด์—ฌ์ค€๋‹ค. 

 

 

 

- ๋ฉ”์†Œ๋“œ ์‹คํ–‰ํ•ด๋ณด๊ธฐ 

# ์ธ์Šคํ„ด์Šค๋กœ ์ ‘๊ทผ 
studt1.detail_info()
studt2.detail_info()


# Student.detail_info() ---> ์—๋Ÿฌ ๋‚จ 


# ํด๋ž˜์Šค๋กœ ์ ‘๊ทผ
Student.detail_info(studt1)
Student.detail_info(studt2)

์ธ์Šคํ„ด์Šค๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์ด๊ณ , ํด๋ž˜์Šค๋กœ๋„ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ํด๋ž˜์Šค๋กœ ์ ‘๊ทผํ•˜๋ฉด ์ธ์ž์— ๊ฐ์ฒด๋ฅผ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. self๋ฅผ ์ฑ„์›Œ์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

 

 

 

 

- __class__ 

print(studt1.__class__, studt2.__class__)
print(id(studt1.__class__) == id(studt2.__class__))

Student ํด๋ž˜์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ฐ์ฒด๋Š” ๊ฐ๊ฐ ๋‹ค๋ฅธ id๊ฐ’์„ ๊ฐ€์ง€์ง€๋งŒ, ํด๋ž˜์Šค๋Š” ๊ฐ™์€ id๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. (๋‹น์—ฐํ•œ ์†Œ๋ฆฌ) 

 

 

 

- ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ํด๋ž˜์Šค ๋ณ€์ˆ˜์˜ ์ ‘๊ทผ 

(1) ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ์ ‘๊ทผ

print(studt1._name, studt2._name)
print(studt1._email, studt2._email)

์ด๋ ‡๊ฒŒ ์ธ์Šคํ„ด์Šค๋ฅผ ํ†ตํ•˜์—ฌ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. 

studt1._name = "Lee" ์ด๋Ÿฐ ์‹์œผ๋กœ ์ง์ ‘ ์ ‘๊ทผ์œผ๋กœ ๊ฐ’์„ ๋ฐ”๊พธ๋Š” ์ฝ”๋“œ๋Š” ๋ฌธ๋ฒ•์ ์œผ๋กœ ๊ถŒ์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•œ๋‹ค. 

 

 

 

(2) ํด๋ž˜์Šค ๋ณ€์ˆ˜ ์ ‘๊ทผ 

print(studt1.student_count)
print(studt2.student_count)
print(Student.student_count)

ํด๋ž˜์Šค ๋ณ€์ˆ˜๋Š” ์ธ์Šคํ„ด์Šค๋กœ๋„ , ํด๋ž˜์Šค ์ž์ฒด๋กœ๋„ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ธ์Šคํ„ด์Šค๋กœ ์ ‘๊ทผ์„ ํ•  ๋•Œ์—๋Š” ์ธ์Šคํ„ด์Šค ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ์—†์œผ๋ฉด ์ƒ์œ„(ํด๋ž˜์Šค) ์—์„œ ๊ฒ€์ƒ‰ํ•œ๋‹ค. ์ธ์Šคํ„ด์Šค ๊ฒ€์ƒ‰ -> ์ƒ์œ„ ๊ฒ€์ƒ‰ (ํด๋ž˜์Šค, ๋ถ€๋ชจ ํด๋ž˜์Šค) ์ˆœ์„œ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋™์ผํ•œ ์ด๋ฆ„์œผ๋กœ ๋ณ€์ˆ˜ ์ƒ์„ฑ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

 

- __del__ ํ˜ธ์ถœ

del studt2

print(studt1.student_count)
print(Student.student_count)

__del__ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ student_count๋„ ์ค„์–ด๋“ค๋„๋ก ๋งŒ๋“ค์—ˆ์œผ๋ฏ€๋กœ ์ถœ๋ ฅํ•ด ๋ณด๋ฉด 1์ด ๋˜์–ด์žˆ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ 

 

 

1. ๊ฐ์ฒด์ง€ํ–ฅ vs ์ ˆ์ฐจ์ง€ํ–ฅ ๊ฐ„๋‹จํ•œ ๋น„๊ต

ํŒŒ์ด์ฌ์€ OOP (๊ฐ์ฒด์ง€ํ–ฅ) ์–ธ์–ด์ด๋‹ค.  

๊ฐ์ฒด์ง€ํ–ฅ๊ณผ ์ ˆ์ฐจ์ง€ํ–ฅ์€ ๋•Œ์— ๋”ฐ๋ผ ์žฅ์ ๊ณผ ๋‹จ์ ์ด ์žˆ๋‹ค. 

๊ฐ์ฒด์ง€ํ–ฅ์„ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ๊ณผ ์ฝ”๋“œ ์ค‘๋ณต์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. 

์ ˆ์ฐจ์ง€ํ–ฅ์€ ์œ„์—์„œ๋ถ€ํ„ฐ ์•„๋ž˜๋กœ ์‹คํ–‰ํ•œ๋‹ค. ์‹คํ–‰์†๋„๊ฐ€ ๋น ๋ฅด์ง€๋งŒ ์œ ์ง€๋ณด์ˆ˜ , ๊ฐœ์„ ์‹œํ‚ค๋Š” ์ž‘์—…์ด ์–ด๋ ต๋‹ค. ๊ทธ๋ž˜์„œ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉฐ, ๋””๋ฒ„๊น…๋„ ์–ด๋ ต๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค. 

ํ•˜์ง€๋งŒ ๋ฌด์กฐ๊ฑด ๊ฐ์ฒด์ง€ํ–ฅ์ด ์ข‹์€ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๋น ๋ฅธ ์†๋„๋ฅผ ๋ณด์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์ˆœํ•˜๊ณ  ๋ชฉ์ ์ด ๋ช…ํ™•ํ•œ ๊ฒฝ์šฐ์—๋Š” ์ ˆ์ฐจ์ง€ํ–ฅ์ด ์œ ๋ฆฌํ•˜๋‹ค๊ณ  ํ•œ๋‹ค. 

 

 

 

2.  ๊ฐ์ฒด์ง€ํ–ฅ ์–ธ์–ด ํŒŒ์ด์ฌ - ํด๋ž˜์Šค ์‚ฌ์šฉํ•˜๊ธฐ 

๋งŒ์•ฝ ํ•™์ƒ์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๋ฅผ ์งœ์•ผ ํ•œ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ, ๋ฒˆํ˜ธ, ํ•™๋…„์„ ํฌํ•จํ•œ๋‹ค๊ณ  ํ•˜๋ฉด , ํ•™์ƒ๋งˆ๋‹ค ๋”ฐ๋กœ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฆฌ์ŠคํŠธ ๊ตฌ์กฐ๋กœ ์ง ๋‹ค๋ฉด ๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ๋„ ๋งŽ์•„์ง€๊ณ , ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์‰ฝ์ง€ ์•Š๋‹ค. ๋”ฐ๋ผ์„œ class๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑ์„ ํ•ด๋ณด๋„๋ก ํ•œ๋‹ค. 

class Student():
    def __init__(self, name, number, grade, details):
        self._name = name 
        self._number = number
        self._grade = grade
        self._details = details

    def __str__(self):  
        return 'str : {} - {}'.format(self._name, self._number)

    ### str ์ด ์—†์„ ๋–ˆ ์ด๊ฒƒ์ด ํ˜ธ์ถœ ๋จ 
    def __repr__(self):
        return 'repr : {} - {}'.format(self._name, self._number)

 Student ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋Š” name, number, grade, details๋ฅผ ๊ธฐ์ž…ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

__str__ ์€ ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ํ˜ธ์ถœ์ด ๋˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. __repr__ ์€ ๋น„์Šทํ•œ๋ฐ , __str__์ด ์—†์œผ๋ฉด ์ด ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค. 

 

 

 

- ๊ฐ์ฒด ์ƒ์„ฑ 

student1 = Student('Kim', 1, 1, {'gender' : 'Male', 'score1' : 95, 'score2' : 88})
student2 = Student('Lee', 2, 2, {'gender' : 'Female', 'score1' : 77, 'score2' : 92})

ํ•™์ƒ ๋‘๋ช…์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ์ค€๋‹ค. 

 

 

- ๊ฐ์ฒด์˜ ์ •๋ณด๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ๋ณด๊ธฐ

print(student1.__dict__)
print(student2.__dict__)

 

 

- __str__, __repr__ ํ˜ธ์ถœ

students_list = []
students_list.append(student1)
students_list.append(student2)
students_list.append(student3)


for x in students_list:
    print(repr(x))
    print(x) # ์ง€์ •ํ•œ str์ด ๋‚˜์˜ด 

ํ•œ๋ฒˆ์— ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ์ฒด๋ฅผ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์ฃผ๊ณ , for๋ฌธ์œผ๋กœ ์ถœ๋ ฅํ•ด์ค€๋‹ค. ๊ทธ๋ƒฅ ์ถœ๋ ฅ์„ ํ•˜๋ฉด str์ด ๋‚˜์˜ค๊ณ , repr์„ ํ˜ธ์ถœํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด repr(๊ฐ์ฒด)๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

 

 

 

 

 

 

 

๋ณธ ํ”„๋กœ์ ํŠธ๋Š” ํŒจ์บ  ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/16

 

[DJANGO ๊ธฐ์ดˆ] 6. ์„ธ์…˜, ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ๊ตฌํ˜„

๋ณธ ํ”„๋กœ์ ํŠธ๋Š” ํŒจ์บ  ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/15 https://silvercoding.tistory.com/14 https://silvercoding.tistory.com/13 https://silvercoding.tistory.com/12 htt..

silvercoding.tistory.com

 

์ด์ „ ๊ธ€๊นŒ์ง€ ํšŒ์›๊ฐ€์ž…๊ณผ ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ, ๋‘๊ฐœ์˜ template html์ด ๊ฑฐ์˜ ์œ ์‚ฌํ–ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ ์ด๋ ‡๊ฒŒ ๊ฒน์น˜๋Š” html์„ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๊ฐ„๋‹จํžˆ ์ƒ์†๋ฐ›๋Š” ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด๋ณธ๋‹ค. 

 

 

 

1. ๊ธฐ๋ณธ์ด ๋˜๋Š” html ํŒŒ์ผ ์ƒ์„ฑ 

community ---> user ---> templates ---> base.html ์ƒ์„ฑ 

<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous"> -->
    <link rel="stylesheet" href="/static/bootstrap.min.css" />
    <script src="https://cdn.jsdelivr.net/npms/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script>
</head>

<body>
    <div class="container">
        {% block contents %}
        {% endblock %}
    </div>
</body>

</html>

body๋ถ€๋ถ„์˜ container ๊นŒ์ง€๋งŒ ๋‚จ๊ฒจ๋‘๊ณ , 

{% block contents %} {% endblock %} ๋ฅผ ์ž‘์„ฑํ•ด์ค€๋‹ค. 

 

์ด ์‚ฌ์ด์— ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

2. ์ƒ์†๋ฐ›๊ธฐ 

community ---> user ---> templates ---> login.html

{% extends "base.html" %} 
{% block contents %}
<div class="row mt-5">
    <div class="col-12 text-center">
        <h1>๋กœ๊ทธ์ธ</h1>
    </div>
</div>
<div class="row mt-5">
    <div class="col-12">
        {{ error }}
    </div>
</div>
<div class="row mt-5">
    <div class="col-12">
        <form method="POST" , action=".">
            {% csrf_token %}
            <div class="form-group mb-3">
                <label for="username" class="form-label">์‚ฌ์šฉ์ž ์ด๋ฆ„</label>
                <input type="text" class="form-control" id="username" placeholder="์‚ฌ์šฉ์ž ์ด๋ฆ„" name="username">
            </div>
            <div class="form group mb-3">
                <label for="password" class="form-label">๋น„๋ฐ€๋ฒˆํ˜ธ</label>
                <input type="password" class="form-control" id="password" placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" name="password">
            </div>
            <button type="submit" class="btn btn-primary">๋กœ๊ทธ์ธ</button>
        </form>
    </div>
</div>
{% endblock %}

์šฐ์„  {% extends "base.html" %} ๋ฅผ ๊ฐ€์žฅ ์ƒ๋‹จ์— ์ž‘์„ฑํ•ด์ค€๋‹ค. 

๊ทธ๋‹ค์Œ ์ง€์ •ํ•˜๋Š” ๋ถ€๋ถ„์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก base.html์—์„œ ์ž‘์„ฑํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ž…๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

์„œ๋ฒ„์— ์ ‘์†ํ•˜์—ฌ ํ™•์ธํ–ˆ์„ ๋•Œ ์ „๊ณผ ๊ฐ™์ด ์ž˜ ๋™์ž‘ํ•œ๋‹ค๋ฉด ์„ฑ๊ณต! 

 

 

 

 

 

๋ณธ ํ”„๋กœ์ ํŠธ๋Š” ํŒจ์บ  ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/15

 

[DJANGO ๊ธฐ์ดˆ] 5. Static ํŒŒ์ผ ๊ด€๋ฆฌ, ๋ถ€ํŠธ์ŠคํŠธ๋žฉ CSS ํ…Œ๋งˆ ์ ์šฉ

๋ณธ ํ”„๋กœ์ ํŠธ๋Š” ํŒจ์บ  ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/14 https://silvercoding.tistory.com/13 https://silvercoding.tistory.com/12 https://silvercoding.tistory.com/11 [D..

silvercoding.tistory.com

 

 

์„ธ์…˜ 

ํด๋ผ์ด์–ธํŠธ <------------------> ์„œ๋ฒ„ 

ํด๋ผ์ด์–ธํŠธ ์•ˆ์— ์ฟ ํ‚ค๊ฐ€ ๋“ค์–ด์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฟ ํ‚ค๋Š” ์ €์žฅ์†Œ์™€ ๊ฐ™๋‹ค.  ์›น ๋ธŒ๋ผ์šฐ์ € ์•ˆ์— ์ฟ ํ‚ค๋ผ๋Š” ์ €์žฅ๊ณต๊ฐ„์ด ์žˆ๋Š”๋ฐ ์ด ์ €์žฅ๊ณต๊ฐ„์„ ํ™œ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ผœ์„œ ์„œ๋ฒ„์— ์š”์ฒญ์„ํ•˜๊ณ , ์‘๋‹ต์„ ๋ฐ›๋Š”๋‹ค. ์„œ๋ฒ„์—์„œ๋Š” ํ—ค๋”์— ์ฟ ํ‚ค ์ •๋ณด๋ฅผ ๋„ฃ์–ด์„œ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค.  ํด๋ผ์ด์–ธํŠธ๋Š” ๊ทธ ์ •๋ณด๋ฅผ ์ฟ ํ‚ค์— ์ €์žฅํ•œ๋‹ค. 

 

์„œ๋ฒ„ ์•ˆ์—์„œ๋Š” ์ฟ ํ‚ค๋กœ ์‚ฌ์šฉํ•  ํ‚ค๋ฅผ ๋งŒ๋“ ๋‹ค. ์ด๊ฑธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•˜๊ณ  ํด๋ผ์ด์–ธํŠธ์— ์•Œ๋ ค์ค€๋‹ค. ํด๋ผ์ด์–ธํŠธ๋Š” ์ž๊ธฐ ์ฟ ํ‚ค ์ €์žฅ์†Œ์— ์ €์žฅํ•œ๋‹ค. ์›น์‚ฌ์ดํŠธ ๋ณ„๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ €์žฅํ•˜๊ฒŒ ๋œ๋‹ค.  

 

 ์ด์ •๋ฆฌ --->  1. (ํด๋ผ์ด์–ธํŠธ) ์‚ฌ์ดํŠธ์— ๋“ค์–ด๊ฐ€๋ฉด ์„œ๋ฒ„์— ์š”์ฒญ

2. (์„œ๋ฒ„) ์„œ๋ฒ„ ์•ˆ์—์„œ๋Š” ์ƒˆ๋กœ์šด ํ‚ค๋ฅผ ์ƒ์„ฑํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋„ฃ์€ ํ›„, ๊ทธ ํ‚ค๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌํ•œ๋‹ค. 

3. (ํด๋ผ์ด์–ธํŠธ) ํด๋ผ์ด์–ธํŠธ๋Š” ๊ฐ๊ฐ์˜ ์›น์‚ฌ์ดํŠธ์— ํ•ด๋‹นํ•˜๋Š” ํ‚ค๋ฅผ ์ฟ ํ‚ค์— ์ €์žฅํ•œ๋‹ค.

4. (ํด๋ผ์ด์–ธํŠธ) ๋‹ค์Œ๋ถ€ํ„ฐ ์›น์‚ฌ์ดํŠธ์— ๋“ค์–ด๊ฐ€๋ฉด ํด๋ผ์ด์–ธํŠธ๊ฐ€ ํ•˜๋Š” ๋ชจ๋“  ์š”์ฒญ์— ์ด ์ฟ ํ‚ค๋ฅผ ๊ฐ™์ด ๋ณด๋‚ธ๋‹ค.

5. (์„œ๋ฒ„) ๊ทธ๋Ÿผ ์„œ๋ฒ„๋Š” ๊ทธ ์ฟ ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๊บผ๋‚ด์™€์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

์–ด๋ ต๊ณ  ๋ณต์žกํ•ด๋ณด์ธ๋‹ค. ํ•˜์ง€๋งŒ ์žฅ๊ณ ์—์„œ ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๊ต‰์žฅํžˆ ์‰ฌ์›Œ๋ณด์ธ๋‹ค.

 

 

 

 

๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ๊ตฌํ˜„

1. Template ์ž‘์„ฑ 

templates ---> login.html  ํŒŒ์ผ ์ƒ์„ฑ 

ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€์™€ ๋น„์Šทํ•œ ๊ตฌ์„ฑ์ด๋ฏ€๋กœ register.html  ์„ ๋ณต๋ถ™ํ•ด์™€์„œ ์ˆ˜์ •์„ ์กฐ๊ธˆ ํ•ด์ค€๋‹ค. 

<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="/static/bootstrap.min.css" />
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script>
</head>

<body>
    <div class="container">
        <div class="row-mt5">
            <div class="col-12 text-center">
                <h1>๋กœ๊ทธ์ธ</h1>
            </div>
        </div>
        <div class="row mt-5">
            <div class="col-12">
                {{error}}
            </div>
        </div>
        <div class="row mt-5">
            <form method="POST" action=".">
                {% csrf_token %}
                <div class="form-group mb-3">
                    <label for="username" class="form-label">์‚ฌ์šฉ์ž ์ด๋ฆ„</label>
                    <input type="text" class="form-control" id="username" placeholder="์‚ฌ์šฉ์ž ์ด๋ฆ„" name="username">
                </div>
                <div class="form-group mb-3">
                    <label for="password" class="form-label">๋น„๋ฐ€๋ฒˆํ˜ธ</label>
                    <input type="password" class="form-control" id="password" placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" name="password">
                </div>
                <button type="submit" class="btn btn-primary">๋กœ๊ทธ์ธ</button>
            </form>
        </div>
    </div>

</body>

</html>

์‚ฌ์šฉ์ž ์ด๋ฆ„, ๋น„๋ฐ€๋ฒˆํ˜ธ๋งŒ ๋‚จ๊ฒจ๋†“๋Š”๋‹ค. 

 

 

 

 

 

 

2. View ์ž‘์„ฑ

community ---> user ---> views.py 

login ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑ ํ•ด์ค€๋‹ค. 

def login(request):
    return render(request, 'login.html')

์šฐ์„  login.html ๊ณผ ์—ฐ๊ฒฐํ•ด์ฃผ์—ˆ๋‹ค. 

 

 

 

 

 

3. Url ์—ฐ๊ฒฐ 

community ---> user ---> urls.py

urlpatterns = [
	...
    path('login/', views.login),
]

์–ด๋–ค ์ฃผ์†Œ๋ฅผ ์—ฐ๊ฒฐํ–ˆ์„ ๋•Œ ์œ„์—์„œ ๋งŒ๋“  view๋ฅผ ์—ฐ๊ฒฐํ•ด ์ค„๊ฑด์ง€ ์ž‘์„ฑํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค. ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋ฉด .../user/login/ ์ด๋ผ๋Š” ์ฃผ์†Œ๋ฅผ ์น˜๋ฉด ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค. 

 

 

์šฐ์„  ์—ฌ๊ธฐ๊นŒ์ง€ ํ•ด์„œ ํ™•์ธํ•ด๋ณด์ž. 

ํ•ด๋‹น ์ฃผ์†Œ์— ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ๋‹ค. 

 

 

 

 

 

4. ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ ์ž‘์„ฑ 

--- check password 

community ---> user ---> views.py 

check_password importํ•ด์„œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํ™•์ธํ•ด์ค€๋‹ค. 

def login(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    elif request.method == 'POST':
        username = request.POST.get('username', None)
        password = request.POST.get('password', None)

        res_data = {}
        if not (username and password):
            res_data['error'] = "๋ชจ๋“  ๊ฐ’์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
        else:
            fcuser = User.objects.get(username = username)
            if check_password(password, fcuser.password):
                pass
            
            else: 
                res_data['error'] = "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค."

        return render(request, 'login.html', res_data)

๊ฐ’์„ ๋ชจ๋‘ ์ž…๋ ฅํ•˜์ง€ ์•Š๊ฑฐ๋‚˜, ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํ‹€๋ฆฌ๊ฒŒ ์ž…๋ ฅํ–ˆ์„ ๊ฒฝ์šฐ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ๋„์šด๋‹ค. 

๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ–ˆ์„ ๋•Œ๋Š” ์šฐ์„  pass๋กœ ๋‚จ๊ฒจ๋†“๊ณ  ํ™•์ธ์„ ํ•ด๋ณด์ž. 

 

 

์‹คํ—˜์„ ํ•ด๋ณด๋‹ˆ ๋ชจ๋‘ ์ž˜ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

--- Session ํ™œ์šฉํ•˜์—ฌ ๋กœ๊ทธ์ธ 

๊ทธ๋‹ค์Œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ผ์น˜ํ–ˆ์„ ๋•Œ์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ค€๋‹ค. ์—ฌ๊ธฐ์„œ ์„ธ์…˜์„ ํ™œ์šฉํ•œ๋‹ค.

community ---> user ---> views.py

 ...
 if check_password(password, fcuser.password):
                request.session['user'] = user.id 
                
                return redirect('/')
 ...

์ „ ์ฝ”๋“œ์—์„œ pass๋ถ€๋ถ„๋งŒ ๊ณ ์ณ์ค€๋‹ค. ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜๋ฉด ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋Š” ์ฝ”๋“œ์ด๋‹ค. request.session์€ ๋”•์…”๋„ˆ๋ฆฌ ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. user๋ผ๋Š” key์— user์˜ id ๊ฐ’์„ ๋„ฃ์–ด ์ฃผ๊ณ , ํŽ˜์ด์ง€์˜ ํ™ˆํ™”๋ฉด์œผ๋กœ ๊ฐ€๋Š” ์ฝ”๋“œ redirect('/')๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์—ˆ๋‹ค. ๋‚ด๋ถ€์ ์œผ๋กœ ๋กœ๊ทธ์ธ์ด ๋˜๋Š” ๊ณผ์ •์€ ๋ณต์žกํ•ด๋ณด์˜€์ง€๋งŒ, ์žฅ๊ณ ๋ฅผ ํ™œ์šฉํ•˜๋ฉด ์ด๋ ‡๊ฒŒ ๋ฐ์ดํ„ฐ ์ €์žฅ๋งŒ ํ•ด์ฃผ๋ฉด ์•Œ์•„์„œ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํŽธ๋ฆฌํ•˜๋‹ค. 

 

 

 

community ---> user ---> views.py

home ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ค€๋‹ค. 

def home(request):
    user_id = request.session.get('user')
    if user_id:
        user = User.objects.get(pk=user_id)
        return HttpResponse(user.username + "๋‹˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!")

    return HttpResponse("Home!")

session์—์„œ ์ €์žฅํ•ด๋‘์—ˆ๋˜ user key๋ฅผ ๋ฐ›์•„์™€ user_id์— ์ €์žฅํ•ด์ฃผ๊ณ , ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” User๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ user์— ์ €์žฅํ•œ๋‹ค. h๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ•œ๋‹ค๋ฉด ๊ทธ user์˜ username์„ HttpResponse๋กœ ๋„์›Œ์ฃผ๋Š” ์ฝ”๋“œ์ด๋‹ค. 

 

 

 

 

community ---> urls.py 

์ด๋ฒˆ์—” user์•ˆ์˜ url์ด ์•„๋‹Œ community ์•ˆ์˜ url ํŒŒ์ผ์„ ์—ด์–ด์ค€๋‹ค.

urlpatterns = [
   ...
    path('', home)
]

url์— ์•„๋ฌด๊ฒƒ๋„ ๋ถ™์ด์ง€ ์•Š์•˜์„ ๋•Œ home ๋ทฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ฝ”๋“œ๊นŒ์ง€ ๋งˆ์ณค๋‹ค. ํ™•์ธํ•ด๋ณด์ž!

 

๊ทธ๋ƒฅ ์„œ๋ฒ„์— ๋“ค์–ด๊ฐ€๋ฉด Home!, ๋กœ๊ทธ์ธ์„ ํ•˜๋ฉด username์ด ์ž˜ ๋„์›Œ์ง„๋‹ค. 

 

 

 

 

*** ๋‚ด๋ถ€์ ์œผ๋กœ๋Š”

๊ฐœ๋ฐœ์ž๋„๊ตฌ(F12) ๋ฅผ ํ‚ค๋ฉด application ํƒญ ---> cookie ---> sessionid ---> ํ•˜๋‚˜์˜ ์‹๋ณ„์ž ๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋‹ค.

์›นํŽ˜์ด์ง€๊ฐ€ ํด๋ผ์ด์–ธํŠธ๋ฅผ ์‹๋ณ„ํ•˜๋Š” ์‹๋ณ„์ž์ธ ๊ฒƒ์ด๋‹ค. 

 

 

 

 

 

๊ฐ„๋‹จํ•œ ๋กœ๊ทธ์•„์›ƒ ๊ตฌํ˜„ 

community ---> user ---> view.py 

logout ํ•จ์ˆ˜ ์ž‘์„ฑ 

def logout(request):
    if request.session.get('user'):
        del (request.session['user'])

    return redirect('/')

๊ฐ„๋‹จํ•˜๊ฒŒ ์„ธ์…˜์— user ํ‚ค๊ฐ’์ด ์ €์žฅ๋˜์–ด ์žˆ์œผ๋ฉด ๊ทธ๊ฒƒ์„ ์ง€์šฐ๋Š” ์ฝ”๋“œ์ด๋‹ค. 

 

 

user ---> urls.py 

์•„์ง ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ url์— logout ์„ ์น˜๋ฉด ๋กœ๊ทธ์•„์›ƒ ๋˜๋Š” ์ฝ”๋“œ๋ฅผ ์จ์ค€๋‹ค. 

urlpatterns = [
	...
    path('logout/', views.logout),
]

์ด์ œ .../user/logout ์ด๋ผ๊ณ  ์น˜๋ฉด ๋กœ๊ทธ์•„์›ƒ์ด ๋˜๊ณ  ํ™ˆํ™”๋ฉด์œผ๋กœ ๋Œ์•„๊ฐ€๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. 

 

 

๊ฒฐ๊ณผ

 

 

๋กœ๊ทธ์ธ์ด ๋œ ์ฑ„๋กœ logout ์ฃผ์†Œ๋ฅผ ์ณค๋”๋‹ˆ ์„ฑ๊ณตํ–ˆ๋‹ค! 

 

๋ณธ ํ”„๋กœ์ ํŠธ๋Š” ํŒจ์บ  ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/14

 

[DJANGO ๊ธฐ์ดˆ] 4. ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ (MTV ๊ตฌ์กฐ - T, V ์ž‘์„ฑ)

๋ณธ ํ”„๋กœ์ ํŠธ๋Š” ํŒจ์บ  ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/13 https://silvercoding.tistory.com/12 https://silvercoding.tistory.com/11 [DJANGO ๊ธฐ์ดˆ] ๊ฐ€์ƒํ™˜๊ฒฝ ์„ค์ • ๋ฐ ์•ฑ ์ƒ์„ฑ..

silvercoding.tistory.com

 

 

CDN 

๋ถ€ํŠธ์ŠคํŠธ๋žฉ CDN์€ Content Delivery Network์˜ ์•ฝ์ž๋กœ, ์™ธ๋ถ€์— ์žˆ๋Š” CSS๋˜๋Š” Javascript์ฃผ์†Œ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ์„œ๋น„์Šค์ด๋‹ค. 

 

๊ฐ„๋žตํ•œ ๊ฐœ๋…์„ ๋ณด๊ณ  ๋„˜์–ด๊ฐ€๋„๋ก ํ•˜์ž. CSS๋‚˜ Javascript์™€ ๊ฐ™์€ ํŒŒ์ผ์ด ์›๋ณธ ์„œ๋ฒ„์— ๋“ค์–ด๊ฐ€์žˆ๊ณ  ์—ฌ๋Ÿฌ๊ฐœ์˜ ์„œ๋ฒ„๋กœ ๋ถ„์‚ฐ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ ์šฐ๋ฆฌ๋‚˜๋ผ์—์„œ ๊ธ€๋กœ๋ฒŒ ์„œ๋น„์Šค๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด ์„œ๋ฒ„๊ฐ€ ํ•œ๊ตญ์— ์žˆ๊ฒŒ ๋˜๊ณ , ๊ทธ๋Ÿผ ์›๋ณธ์ด ํ•œ๊ตญ์— ์žˆ๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ๋ฏธ๊ตญ์— ์žˆ๋Š” ์‚ฌ์šฉ์ž๋Š” ๋ฉ€๋ฆฌ ์žˆ๋Š” ์„œ๋ฒ„ ํƒ“์— ๋Š๋ฆฐ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณต๋ฐ›๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ์ค‘๊ฐ„์— ์—ฌ๋Ÿฌ ์„œ๋ฒ„๋“ค์„ ๋‘๋Š” ๊ฒƒ์ด๋‹ค. ๋‚˜๋ผ๋ณ„๋กœ ์„œ๋ฒ„๋ฅผ ๋ฟŒ๋ ค๋†“๋Š” ๊ฒƒ์ด๋‹ค. ์ด์ฒ˜๋Ÿผ ์ž์‹ ์—๊ฒŒ ๋น ๋ฅธ ๊ณณ์œผ๋กœ ์ ‘์†ํ•ด์„œ ๋ฐ›์•„๊ฐ€๋Š” ์„œ๋น„์Šค๊ฐ€ CDN ์ด๋‹ค. 

 

 

๋ณธ ํ”„๋กœ์ ํŠธ์—์„œ ์“ฐ์ธ CSS ๋งํฌ๋„ ์›๋ณธ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ ๊ฒŒ ์•„๋‹ˆ๋ผ ํ˜„์žฌ pc์—์„œ ๊ฐ€์žฅ ๋น ๋ฅธ ์„œ๋ฒ„์—์„œ ๊ฐ€์ ธ์˜จ ๊ฒƒ์ด๋‹ค. 

 

 

(๋” ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด)

CDN ์ฐธ๊ณ  ๋ฌธ์„œ 

https://docs.microsoft.com/ko-kr/azure/cdn/cdn-overview

 

CDN(์ฝ˜ํ…์ธ  ๋ฐฐ๋‹ฌ ๋„คํŠธ์›Œํฌ)์ด๋ž€? - Azure

Azure CDN(Content Delivery Network) ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋ณด๊ณ  ๊ณ ๋Œ€์—ญํญ ์ฝ˜ํ…์ธ ๋ฅผ ๋ฐฐ๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ด…๋‹ˆ๋‹ค.

docs.microsoft.com

 

๊ธฐ์กด์— ์ด๋Ÿฌํ•œ CDN์„ ์‚ฌ์šฉํ•˜์—ฌ CSS๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” CDN์„ ์“ฐ์ง€ ์•Š์œผ๋ฉด์„œ CSS๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. 

 

 

 

 

์ง์ ‘ ๋งŒ๋“  CSS ํŒŒ์ผ์„ ์ ์šฉํ•˜๋ ค๋ฉด  

1. static ํด๋” ๋งŒ๋“ค๊ธฐ 

community ---> static ํด๋” ์ƒ์„ฑ

์ƒ์„ฑํ•œ static ํด๋”์—์„œ css, javascript ํŒŒ์ผ ๊ด€๋ฆฌ๋ฅผ ํ•˜๊ฒŒ๋œ๋‹ค.

 

settings.py 

...
import os

...

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

...

์ด๋ฏธ STATIC_URL ์ž‘์„ฑ๋˜์–ด์žˆ๋‹ค. ์ด์ œ cssํŒŒ์ผ์— ์ ‘๊ทผํ–ˆ์„ ๋•Œ ๊ทธ๊ฒŒ ์–ด๋””ํŒŒ์ผ์— ์žˆ๋Š”์ง€๋ฅผ ์•Œ๋ ค์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

STATICFILES_DIRS = [] ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ํด๋”๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. BASE_DIR ๋Š” ํ”„๋กœ์ ํŠธ ํด๋”์ธ community์ด๊ณ , ์ƒ์„ฑํ•œ static ํด๋”์™€ joinํ•ด์ค€๋‹ค. os๋ฅผ importํ•˜๋Š” ๊ฒƒ๋„ ์žŠ์ง€ ๋ง์ž.

 

 

 

 

 

2. CSS ํŒŒ์ผ ์ค€๋น„

๋ฐฑ์—”๋“œ ์ค‘์‹ฌ์˜ ํ”„๋กœ์ ํŠธ์ด๋ฏ€๋กœ ์ง์ ‘ CSS ํŒŒ์ผ์„ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ๋ถ€ํŠธ์ŠคํŠธ๋žฉ์˜ ํ…Œ๋งˆ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค. 

 

 

<bootstrap 4.3 themes free>

https://bootswatch.com/

 

Bootswatch: Free themes for Bootstrap

Customizable Changes are contained in just two SASS files, enabling further customization and ensuring forward compatibility.

bootswatch.com

์ด๊ณณ์—์„œ ๋งˆ์Œ์— ๋“œ๋Š” ํ…Œ๋งˆ๋ฅผ ๊ณจ๋ผ์„œ 

bootstrap.min.css ๋ฅผ ๋‹ค์šด๋ฐ›์•„์ค€๋‹ค. 

 

๋‹ค์šด๋ฐ›์€ ํŒŒ์ผ์„ staticํด๋”์— ์˜ฎ๊ฒจ์ค€๋‹ค. 

 

 

 

์ด์ œ ๋ชจ๋“  ์ค€๋น„๋ฅผ ๋งˆ์ณค๊ณ , html์— ์ ์šฉํ•ด์ค€ ๋‹ค์Œ ๊ฒฐ๊ณผ ํ™•์ธ์„ ํ•ด๋ณธ๋‹ค.

 

 

3. html์— CSS ์ ์šฉํ•ด์ฃผ๊ธฐ

user ---> templates --->  register.html

 

์›๋ž˜ ์ฝ”๋“œ

...

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script>
</head>

...

 

 

CSS ํ…Œ๋งˆ ์ ์šฉ ์ฝ”๋“œ

...

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="/static/bootstrap.min.css" />
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script>
</head>

...

link ํƒœ๊ทธ๋งŒ ๋ฐ”๊พผ๋‹ค. static ํด๋”์˜ css ํŒŒ์ผ์„ ์—ฐ๊ฒฐ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

 

๋ชจ๋‘ ๋งˆ์นœ ํ›„ ์„œ๋ฒ„์— ๋“ค์–ด๊ฐ€๋ณด๋ฉด ์ด๋ ‡๊ฒŒ ํ™”๋ คํ•œ ํ…Œ๋งˆ๊ฐ€ ์ ์šฉ์ด ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค!

 

 

 

๋‹ค์Œ ๊ธ€์—์„œ๋Š” ์„ธ์…˜ ๋ฐ ๋กœ๊ทธ์ธ์„ ์•Œ์•„ ๋ณด๋„๋ก ํ•œ๋‹ค. 

๋ณธ ํ”„๋กœ์ ํŠธ๋Š” ํŒจ์บ  ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/13

 

[DJANGO ๊ธฐ์ดˆ] 3. Django admin, ๊ฐœ๋ฐœ ์„œ๋ฒ„ ํ™œ์šฉ

๋ณธ ํ”„๋กœ์ ํŠธ๋Š” ํŒจ์บ  ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/12 https://silvercoding.tistory.com/11 [DJANGO ๊ธฐ์ดˆ] ๊ฐ€์ƒํ™˜๊ฒฝ ์„ค์ • ๋ฐ ์•ฑ ์ƒ์„ฑ ํ”„๋ ˆ์ž„ ์›Œํฌ - ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์ฝ”๋“œ๋ฅผ..

silvercoding.tistory.com

 

Tamplate์€ Bootstrap ์„ ์ด์šฉํ•˜์—ฌ ์ž‘์„ฑํ•ด์ค€๋‹ค. 

์ž‘์—…์„ ํ•  ๋•Œ ์ €์žฅํ•˜๋Š” ์Šต๊ด€์„ ๊ผญ ๋“ค์—ฌ์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค. ์ €์žฅ ์•ˆํ•˜๊ณ  ํŽ˜์ด์ง€ ์‹คํ–‰ํ•ด์„œ ์—๋Ÿฌ๋œฌ๊ฒƒ๋งŒ ์˜ค์ฒœ๋งŒ๋ฒˆ์ด๋‹ค.

 

Bootstrap ์ด์šฉ์„ ์œ„ํ•ด ๋‹ค์Œ ๋งํฌ์—์„œ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์˜ค๋ฉด ๋œ๋‹ค. 

https://getbootstrap.com/docs/5.0/getting-started/introduction/

 

Introduction

Get started with Bootstrap, the world’s most popular framework for building responsive, mobile-first sites, with jsDelivr and a template starter page.

getbootstrap.com

 

ํšŒ์›๊ฐ€์ž… ์ฐฝ ๋งŒ๋“ค๊ธฐ 

1. Template ์ž‘์„ฑํ•˜๊ธฐ

  •  user ํด๋” ---> templates ํด๋”  ---> register.html  

Getting started -- CSS ๋ถ€๋ถ„๊ณผ Javascript ๋ถ€๋ถ„์„ headํƒœ๊ทธ ์•ˆ์— ๋„ฃ์–ด์ค€๋‹ค. starter template ์—์„œ metaํƒœ๊ทธ๋„ ๊ฐ€์ ธ์™€ ์ค€๋‹ค. 

<html>

    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">    

        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script>
    </head>

    <body>
    </body> 

</html>

๊ทธ๋Ÿผ ์šฐ์„  ์ด๋Ÿฐ ๋ชจ์–‘์„ ๋ˆ๋‹ค. 

 

 

Forms --- Overview ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค!

<html>

    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script>
    </head>

    <body>
        <div class="container">
            <div class="col-12 text-center">
                <h1>ํšŒ์›๊ฐ€์ž…</h1>
            </div>
        </div>
        <div class="row">
            <form>
                <div class="mb-3">
                    <label for="exampleInputEmail1" class="form-label">Email address</label>
                    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
                    <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
                </div>
                <div class="mb-3">
                    <label for="exampleInputPassword1" class="form-label">Password</label>
                    <input type="password" class="form-control" id="exampleInputPassword1">
                </div>
                <div class="mb-3 form-check">
                    <input type="checkbox" class="form-check-input" id="exampleCheck1">
                    <label class="form-check-label" for="exampleCheck1">Check me out</label>
                </div>
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>
        </div>
    </body>

</html>

ํšŒ์›๊ฐ€์ž… ๊ธ€์”จ ๋„ฃ์–ด์ฃผ๊ณ  ๊ทธ ๋ฐ‘์— ํšŒ์›๊ฐ€์ž… form์„ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค. 

 

ํด๋”์— ์ €์žฅ๋œ register.html ์„ ์—ด์–ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ–‰์ด ๋œ๋‹ค. 

 

๋””์ž์ธ์ด ๋งˆ์Œ์— ์•ˆ๋“œ๋‹ˆ ๋ฐ”๊พธ์–ด ์ค€๋‹ค. 

- ํ•œ๊ธ€๋กœ ๋ฐ”๊พธ๊ธฐ, container ์ ์šฉ, placeholder ์ž‘์„ฑํ•ด์ฃผ๊ธฐ

<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script>
</head>

<body>
    <div class="container">
        <div class="row-mt5">
            <div class="col-12 text-center">
                <h1>ํšŒ์›๊ฐ€์ž…</h1>
            </div>
        </div>
        <div class="row mt-5">
            <form>
                <div class="form-group mb-3">
                    <label for="username" class="form-label">์‚ฌ์šฉ์ž ์ด๋ฆ„</label>
                    <input type="text" class="form-control" id="username" placeholder="์‚ฌ์šฉ์ž ์ด๋ฆ„">
                </div>
                <div class="form-group mb-3">
                    <label for="password" class="form-label">๋น„๋ฐ€๋ฒˆํ˜ธ</label>
                    <input type="password" class="form-control" id="password" placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ">
                </div>
                <div class="form-group mb-3">
                    <label for="re-password" class="form-label">๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ</label>
                    <input type="password" class="form-control" id="re-password" placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ">
                </div>
                <button type="submit" class="btn btn-primary">๋“ฑ๋ก</button>
            </form>
        </div>
    </div>

</body>

</html>

์›ํ•˜๋Š” ๋Œ€๋กœ ์ž‘์„ฑ์ด ์™„๋ฃŒ ๋˜์—ˆ๋‹ค. 

 

 

 

2. View ์ž‘์„ฑํ•˜๊ธฐ 

- community ---> user ---> views.py 

from django.shortcuts import render

def register(request):
    return render(request, "register.html")

register ํ•จ์ˆ˜ ์ž‘์„ฑํ•˜๊ธฐ. request๋ฅผ ๊ผญ ๊ฐ™์ด ์ „๋‹ฌํ•ด ์ฃผ์–ด์•ผ ํ•˜๊ณ , ๋ฐ˜ํ™˜ํ•˜๊ณ ์ž ํ•˜๋Š” html ํŒŒ์ผ์„ ์ž‘์„ฑํ•ด ์ค€๋‹ค.

 

- community --> urls.py 

urlpatterns = [
    ...
    path('user/', include('user.urls'))

]

urlpatterns ์•ˆ์— user ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด์ค€๋‹ค. 

 

 

- community ---> user ---> urls.py 

user ํด๋” ์•ˆ์— urls.py๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. 

from django.urls import path
from . import views


urlpatterns = [
    path('register/', views.register)
]

views.py์˜ register ํ•จ์ˆ˜๋ฅผ import ํ•˜์—ฌ ์ „๋‹ฌํ•ด์ค€๋‹ค. ์ด๋ ‡๊ฒŒ ์ž‘์„ฑ์„ ํ•ด๋†“์œผ๋ฉด .../user/register ๊ฒฝ๋กœ๋ฅผ ์น˜๋ฉด register.html์ด ๋ Œ๋”๋ง ๋  ๊ฒƒ์ด๋‹ค. 

 

 

 

- ๊ฐœ๋ฐœ์ž ์„œ๋ฒ„ ๋“ค์–ด๊ฐ€๋ณด๊ธฐ 

ํ„ฐ๋ฏธ๋„

python manage.py runserver

์ด์ œ ์„œ๋ฒ„๋กœ ๋“ค์–ด๊ฐ€์„œ ํ™•์ธ์„ ํ•ด๋ณด์ž. 

ํ„ฐ๋ฏธ๋„์— ๋œจ๋Š” ์ฃผ์†Œ ๋’ค์— /user/register ์ž‘์„ฑ์„ ํ•ด์ค€๋‹ค. 

 

๊ทธ๋Ÿฌ๋ฉด ์ด๋ ‡๊ฒŒ ์ž˜ ์—ฐ๊ฒฐ์ด ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ด์ฌ ์ชฝ์œผ๋กœ ์ „๋‹ฌ 

์ฃผ์†Œ๋ฅผ ์ณ์„œ ํšŒ์›๊ฐ€์ž… ์ฐฝ์œผ๋กœ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์œผ๋‹ˆ, ์ด์ œ๋Š” POST์š”์ฒญ์„ ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„๋กœ ์ „๋‹ฌํ•ด์ฃผ๋Š” ์ž‘์—…์„ ํ•ด์•ผํ•œ๋‹ค. 

 

1. Template ์ž‘์„ฑํ•˜๊ธฐ

community ---> user ---> register.html  

 

  ...
  		 <form method="POST" , action=".">
                    {% csrf_token %}
                    <div class="form-group mb-3">
                        <label for="username" class="form-label">์‚ฌ์šฉ์ž ์ด๋ฆ„</label>
                        <input type="text" class="form-control" id="username" placeholder="์‚ฌ์šฉ์ž ์ด๋ฆ„" name="username">
                    </div>
                    <div class="form group mb-3">
                        <label for="password" class="form-label">๋น„๋ฐ€๋ฒˆํ˜ธ</label>
                        <input type="password" class="form-control" id="password" placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" name="password">
                    </div>
                    <div class="form group mb-3">
                        <label for="re-password" class="form-label">๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ</label>
                        <input type="password" class="form-control" id="re-password" placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ" name="re-password">
                    </div>
                    <button type="submit" class="btn btn-primary">๋“ฑ๋ก</button>
                </form>
  ...

๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ด์ฌ ์ชฝ์œผ๋กœ ์ „๋‹ฌํ•ด์ฃผ๋Š” ์ฝ”๋“œ์ด๋‹ค. POST method๋กœ ์ „๋‹ฌํ•˜๊ณ , action="."์€ ํ˜„์žฌ ์ฃผ์†Œ๋กœ ์ „๋‹ฌํ•˜๊ฒ ๋‹ค๋Š” ๋œป์ด๋‹ค.           

form ์•ˆ์— {% csrf_token %}  ์ด๊ฒƒ์„ ๊ผญ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

 

form ์€ data ๋ฅผ ์„œ๋ฒ„์— ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ธ๋ฐ, ์•ˆ์ข‹๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์ด ๋‹ค๋ฅธ ์›น์‚ฌ์ดํŠธ์—์„œ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์„ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๋ผ๊ณ  ํ•œ๋‹ค.  ํฌ๋กœ์Šค ๋„๋ฉ”์ธ์„ ๋ง‰๊ธฐ ์œ„ํ•ด ์•”ํ˜ธํ™”๋œ ํ‚ค๋ฅผ ์ˆจ๊ฒจ๋†“๋Š”๋‹ค. ๊ทธ๋ž˜์„œ ํ‚ค๊ฐ€ ์—†์œผ๋ฉด ์ž˜๋ชป๋œ ์š”์ฒญ์œผ๋กœ ๋ฐ›์•„๋“ค์—ฌ ์š”์ฒญ์„ ๊ฑฐ์ ˆํ•˜๊ฒŒ ๋œ๋‹ค. ํ•„์ˆ˜์ ์ธ ์ฝ”๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ์จ์ฃผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค!

 

๊ทธ๋ฆฌ๊ณ  input ํƒœ๊ทธ์— name ์†์„ฑ๋„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. ์ด name ๊ฐ’์œผ๋กœ ์ •๋ณด๊ฐ€ ์ „๋‹ฌ์ด ๋  ๊ฒƒ์ด๋‹ค.

 

 

 

 

2. View ์ž‘์„ฑํ•˜๊ธฐ 

community ---> user ---> view.py 

from django.shortcuts import render
from .models import User

def register(request):
    if request.method == 'GET':
        return render(request, "register.html")
    elif request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        re_password = request.POST['re-password']

    user = User(
                Username = username,
                password = password
            )

    user.save()

    return render(request, "register.html")

์š”์ฒญ ๋ฐฉ๋ฒ•์ด GET์ผ ๋•Œ์™€ POST ์ผ ๋•Œ๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์ž‘์„ฑํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

 

 

์—ฌ๊ธฐ๊นŒ์ง€ ํ•ด์„œ silver 123456 ์œผ๋กœ ํšŒ์›๊ฐ€์ž…์„ ํ•˜๊ณ  admin์— ๋“ค์–ด๊ฐ€ ๋ณด๋‹ˆ user ์ƒ์„ฑ์ด ์ž˜ ๋˜์–ด์žˆ๋‹ค.

 

 

 

 

 

 

3. ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธํ•˜๊ธฐ 

community ---> user ---> view.py .py

from django.shortcuts import render
from .models import User

def register(request):
    if request.method == 'GET':
        return render(request, "register.html")
    elif request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        re_password = request.POST['re-password']

        res_data = {}

        if password != re_password:
            res_data['error'] = "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค."
        else:
            user = User(
                Username = username,
                password = password
            )

            user.save()
            
        return render(request, "register.html", res_data)

res_data๋ผ๋Š” dictionary๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๊ณ  password์™€ re_password์˜ ๊ฐ’์ด ๋‹ค๋ฅด๋ฉด res_data์˜ ํ‚ค๊ฐ’์„ error, value๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ์ €์žฅํ•ด์ค€๋‹ค. ๊ทธ๋‹ค์Œ render ํ•จ์ˆ˜์— res_data๋ฅผ ์ „๋‹ฌํ•ด ์ค€๋‹ค. 

 

 

community ---> user ---> register.html  

...
	<div class="row-mt5">
            <div class="col-12 text-center">
                <h1>ํšŒ์›๊ฐ€์ž…</h1>
            </div>
        </div>
        <div class="row mt-5">
            <div class="col-12">
                {{error}}
            </div>
        </div>
...

๊ทธ๋‹ค์Œ ํšŒ์›๊ฐ€์ž… ๋ฐ‘์— ์—๋Ÿฌ ๋ฌธ์ž์—ด์„ ๊ทธ๋ ค์ค„ ๋ถ€๋ถ„์„ ํ•˜๋‚˜ ๋งŒ๋“ ๋‹ค. {{}} ์•ˆ์— res_data์˜ ํ‚ค ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. 

register.html ์„ ๋žœ๋”๋ง์„ ํ• ๋•Œ res_data๋ผ๋Š” ๊ฐ’์„ ๋„˜๊ธฐ๋ฉด res_data์˜ ํ‚ค์™€ {{error}}๊ฐ€ ๋งคํ•‘์ด ๋œ๋‹ค.

 

 

--- ํ™•์ธํ•ด๋ณด๊ธฐ 

 

๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์น˜๋ฉด ์œ„์™€๊ฐ™์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•ด ๋ณด์•˜๋‹ค. 

 

 

 

 

4. ๋ชจ๋“  ๊ฐ’์ด ์ž…๋ ฅ๋˜์ง€ ์•Š์•˜์„ ๋•Œ ์ฒ˜๋ฆฌ & ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” 

์œ„์—๊นŒ์ง€ ์ž‘์—…์„ ๋งˆ์ณค์„ ๋•Œ ๋ฌธ์ œ์ ์ด ์žˆ๋‹ค. 

์‚ฌ์šฉ์ž ์ด๋ฆ„, ๋น„๋ฐ€๋ฒˆํ˜ธ, ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ์„ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๊ฐ€ ๋œจ์ง€ ์•Š์œผ๋ฉฐ, 

์‚ฌ์ง„๊ณผ ๊ฐ™์ด ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๊ณต๊ฐœ๋˜๋ฏ€๋กœ ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™”๊นŒ์ง€ ์ž‘์„ฑ์„ ํ•ด์ค€๋‹ค. 

 

community ---> user ---> view.py .py

from django.shortcuts import render
from .models import User
from django.contrib.auth.hashers import make_password

def register(request):
    if request.method == 'GET':
        return render(request, "register.html")
    elif request.method == 'POST':
        username = request.POST.get('username', None) ## ๊ธฐ๋ณธ ๊ฐ’ ์„ค์ •
        password = request.POST.get('password', None)
        re_password = request.POST.get('re-password', None)

        res_data = {}


        if not (username and password and re_password):
            res_data['error'] = "๋ชจ๋“  ๊ฐ’์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
        elif password != re_password:
            res_data['error'] = "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค."
        else:
            user = User(
                Username = username,
                password = make_password(password)
            )

            user.save()
            
        return render(request, "register.html", res_data)

์ตœ์ข…์ ์œผ๋กœ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑ ์™„๋ฃŒ. makepassword๋ฅผ importํ•˜์—ฌ password๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™”๊ฐ€ ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  request.POST์— getํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ธฐ๋ณธ ๊ฐ’์„ None์œผ๋กœ ์„ค์ •ํ•ด์ค€๋‹ค. ๊ทธ๋‹ค์Œ username, password, re_password๋ฅผ and๋กœ ์—ฐ๊ฒฐํ•ด์ฃผ์–ด ํ•˜๋‚˜๋ผ๋„ ์ž‘์„ฑ์ด ๋˜์ง€ ์•Š์œผ๋ฉด error ๋ฉ”์‹œ์ง€๊ฐ€ ๋œจ๊ฒŒ ์ž‘์„ฑํ•ด์ค€๋‹ค. 

 

 

๊ฒฐ๊ณผ ํ™•์ธ

๋ชจ๋“  ๊ฐ’์„ ์ž…๋ ฅํ•˜์ง€ ์•Š์•˜๋”๋‹ˆ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๊ฐ€ ๋œฌ๋‹ค. 

silver2๋กœ ๊ฐ€์ž…์„ ๋‹ค์‹œ ํ•ด์ฃผ์—ˆ๋”๋‹ˆ ์•”ํ˜ธํ™”๋œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ๋œจ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

--- ์—ฌ๊ธฐ์„œ model์˜ field(ex, ์‚ฌ์šฉ์ž ์ด๋ฉ”์ผ) ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•œ ์ ˆ์ฐจ (ํฌ์ŠคํŒ…์€ ์ƒ๋žต)

model field ์ž‘์„ฑ ---> migration ---> register.html ์ž‘์„ฑ ---> view.py ์ž‘์„ฑ 

์‚ฌ์šฉ์ž ์ด๋ฉ”์ผ field๋ฅผ ์ถ”๊ฐ€ํ•œ ๊ฒฐ๊ณผ์ด๋‹ค. 

 

 

 

์ •๋ง ๊ธด ์—ฌ์ •์ด์—ˆ๋‹ค. ๋‹ค์Œ์—๋Š” static ํŒŒ์ผ ๊ด€๋ฆฌ์™€ CDN ์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณผ ์˜ˆ์ •์ด๋‹ค. 

 

๋ณธ ํ”„๋กœ์ ํŠธ๋Š” ํŒจ์บ  ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/12

 

[DJANGO ๊ธฐ์ดˆ] 2. MTV์˜ Model ์ƒ์„ฑ, sqlite db ๊ธฐ๋ณธ์ ์ธ ๊ด€๋ฆฌ

๋ณธ ํ”„๋กœ์ ํŠธ๋Š” ํŒจ์บ  ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/11 [DJANGO ๊ธฐ์ดˆ] ๊ฐ€์ƒํ™˜๊ฒฝ ์„ค์ • ๋ฐ ์•ฑ ์ƒ์„ฑ ํ”„๋ ˆ์ž„ ์›Œํฌ - ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์ฝ”๋“œ๋ฅผ ์ฒด๊ณ„ํ™” ํ•˜์—ฌ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„

silvercoding.tistory.com

 

 

 

admin์ด ๋ฌด์—‡์ธ๊ฐ€?

admin์€ django๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์„ฑ์ด ๋˜์–ด์žˆ๋‹ค. ์ด๋ฅผ ํ™•์ธํ•ด ๋ณด์ž.

1. community ํด๋” ---> settings.py 

INSTALLED_APPS์— ๊ธฐ๋ณธ์ ์œผ๋กœ ๋“ค์–ด๊ฐ€ ์žˆ๋‹ค. (๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ ์ค„) 

 

2. community ํด๋” ---> urls.py

์—ฌ๊ธฐ์—๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ admin ์ด ์ƒ์„ฑ๋˜์–ด ์žˆ๋‹ค. 

jango ์•ˆ์˜ admin์ด ๊ฒฝ๋กœ admin ์•„๋ž˜์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค. 

๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€์—์„œ ์—ฌ๋Ÿฌ url๋“ค์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— jango์˜ admin ์•ˆ์— ์—ฌ๋Ÿฌ๊ฐ€์ง€ url ๋“ค์ด ์ •์˜๊ฐ€ ๋˜์–ด์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ์‚ฌ์šฉ๋˜๋Š” url๋“ค์ด jango์˜ admin์•ˆ์— ์ •์˜๊ฐ€ ๋˜์–ด์žˆ๋‹ค๋Š” ์†Œ๋ฆฌ์ด๊ณ , ๊ทธ๊ฒƒ๋“ค์„ ํ”„๋กœ์ ํŠธ ์•ˆ์—์„œ admin/ ๊ฒฝ๋กœ ํ•˜์œ„์— ์ง€์ •์„ ํ•ด๋†“๋Š” ๊ฒƒ์ด๋‹ค. 

 

์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•  ๋•Œ (ex) silver.com/admin/..... ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ---> admin ํ•˜์œ„์— ๋“ค์–ด๊ฐ€๋Š” ์ฃผ์†Œ๋“ค(.....) ์€ admin.site.urls ์•ˆ์— ์ •์˜๋œ url์„ ๋”ฐ๋ผ๊ฐ€๋Š” ๊ฒƒ์ด๋‹ค. 

 

๋‹จ์ˆœํ•˜๊ฒŒ ๋งํ•˜๋ฉด ์ฃผ์†Œ๋’ค์— admin์„ ๋ถ™์ด๋ฉด ์žฅ๊ณ ์˜ ๊ด€๋ฆฌ์ž ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป์ด๋‹ค. ๋ณต์žกํ•˜๋‹ˆ ์ด๋ ‡๊ฒŒ ์ดํ•ดํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค. 

 

 

 

๊ฐœ๋ฐœ ์„œ๋ฒ„์— ๋“ค์–ด๊ฐ€๋ณด์ž

ํ„ฐ๋ฏธ๋„

python manage.py runserver

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๊ฐœ๋ฐœ์„œ๋ฒ„๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

starting development ---- ๋’ค์— ์ฃผ์†Œ๊ฐ€ ๋‚˜์˜จ๋‹ค. ์ด ์ฃผ์†Œ/admin์„ ์ฃผ์†Œ์ฐฝ์— ์น˜๋ฉด

 

์•„์ด๋”” ํŒจ์Šค์›Œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์ค€ ์ ์ด ์—†๋Š”๋ฐ ๋กœ๊ทธ์ธ์„ ํ•˜๋ผ๊ณ  ํ•œ๋‹ค. 

 

 

๊ทธ๋ž˜์„œ ์ด๊ฑธ ๋งŒ๋“ค์–ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค

๊ฐœ๋ฐœ์„œ๋ฒ„๋Š” ctrl + c ๋กœ ์ข…๋ฃŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

- ์•„์ด๋”” ํŒจ์Šค์›Œ๋“œ ๋งŒ๋“ค๊ธฐ

ํ„ฐ๋ฏธ๋„

python manage.py createsuperuser

์ด๊ฑธ ์ณ์„œ ์•„์ด๋””, ํŒจ์Šค์›Œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค. 

 

๊ทธ๋‹ค์Œ ๋‹ค์‹œ ์‹คํ–‰์‹œ์ผœ ๋กœ๊ทธ์ธํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์ด๋Ÿฌํ•œ ์ฐฝ์ด ์ƒ๊ฒจ์ ธ์žˆ๋‹ค. 

Users์— ๋“ค์–ด๊ฐ€๋ณด๋ฉด ๋ฐฉ๊ธˆ ๋งŒ๋“  user์ •๋ณด๊ฐ€ ๋“ค์–ด๊ฐ€์ ธ ์žˆ๋‹ค. 

 

 

 

์žฅ๊ณ  admin์— ๋งŒ๋“  model ๋“ฑ๋ก

1. user ํด๋” --> admin.py 

...
from .models import User

class userAdmin(admin.ModelAdmin):
    pass

admin.site.register(User, userAdmin)

User ํด๋ž˜์Šค๋ฅผ import ํ•ด์ฃผ๊ณ , ๊ด€๋ฆฌ์ž์— ์“ธ ๋‚ด์šฉ๋“ค์„ ๊ธฐ์ž…ํ•œ๋‹ค. 

 

----> runserver ๋‹ค์‹œ ์‹คํ–‰ ํ›„ ๋“ค์–ด๊ฐ€๋ณด๋ฉด 

๋งŒ๋“  ๋ชจ๋ธ์ด ์ ์šฉ๋˜์—ˆ๋‹ค. 

 

User ์˜ Users์— Add ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์„œ ์‚ฌ์šฉ์ž๋ฅผ ํ•œ๊ฐœ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. 

๊ทธ๋Ÿฌ๋ฉด ์ด๋ ‡๊ฒŒ ์ƒ์„ฑ์ด ๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ Users๋Š” ์œ„์˜ Users์™€ ๊ฐ™์•„ ํ˜ผ๋™๋˜๊ธฐ๋„ ํ•˜๊ณ , ๊ด€๋ฆฌํ•˜๊ธฐ ์ข‹๊ฒŒ ํ•œ๊ธ€๋กœ ๋ฐ”๊พธ์–ด์ค„ ๊ฒƒ์ด๋‹ค. User object (1) ์ด๋ผ๊ณ  ์ ํ˜€์žˆ๋Š” ๊ฒƒ์€ Username์œผ๋กœ ๋ฐ”๊พธ์–ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค. 

 

 

2. Users ๊ธ€์”จ ๋ฐ”๊ฟ”์ฃผ๊ธฐ

user ํด๋” ---> models.py ---> class Meta 

from django.db import models

class User(models.Model):
    
    
    ...
    

    class Meta:
        db_table = 'test_user'
        verbose_name = "์‚ฌ์šฉ์ž" 
        verbose_name_plural = "์‚ฌ์šฉ์ž"  ## ๋ณต์ˆ˜ํ˜• ์ง€์ •

์ง€๋‚œ ๊ธ€์—์„œ ์ž‘์„ฑํ–ˆ๋˜ Meta ํด๋ž˜์Šค์—์„œ verbose_name์„ ์„ค์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค! 

 

 

3. User object ๊ธ€์”จ ๋ฐ”๊ฟ”์ฃผ๊ธฐ 

user ํด๋” ---> models.py ---> def __str__(self)

class User(models.Model):
    
	...

    def __str__(self):
        return self.Username

์ด๋ฒˆ์—๋Š” model.py ์— ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜ ์ž‘์„ฑํ•ด์ค€๋‹ค. 

์ „์— User object (1) ๋ผ๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ ์ €์žฅ์ด ๋˜์–ด์žˆ์—ˆ๋Š”๋ฐ, ์ด๋Š” Userํด๋ž˜์Šค๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ–ˆ์„ ๋•Œ ๋‚˜์˜ค๋Š” ๊ฐ’์ด๋‹ค. ์ด ๋•Œ ํด๋ž˜์Šค๋ฅผ ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ ์–ด๋–ค ๋ฌธ์ž๋กœ ๋ณ€ํ™˜์‹œ์ผœ์ค„ ์ง€ ์ •ํ•˜๋Š” ๋‚ด์žฅํ•จ์ˆ˜๊ฐ€ __str__ ์ธ ๊ฒƒ์ด๋‹ค. 

 

 

user ํด๋” ---> admin.py  ---> class userAdmin

...

class userAdmin(admin.ModelAdmin):
    list_display = ('Username', 'password')
    
...

์ „์— pass๋กœ ๋‚จ๊ฒจ๋’€๋˜ ๋ถ€๋ถ„์„ list_display ํŠœํ”Œ์„ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ์ด ํŠœํ”Œ ์•ˆ์— ์žˆ๋Š” ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๊ฐ€ ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋œ๋‹ค. ์‚ฌ์šฉ์ž๋ช…๋งŒ ๋ณด์˜€๋˜ ๊ฒƒ์ด ๋น„๋ฐ€๋ฒˆํ˜ธ๋„ ํ•จ๊ป˜ ๋ณด์—ฌ์งˆ ๊ฒƒ์ด๋‹ค. 

 

 

 

์ด๋ ‡๊ฒŒ ๊นŒ์ง€ ํ•ด์„œ 

์ตœ์ข…ํ™”๋ฉด

์ง€์ •ํ•œ ๋Œ€๋กœ ๋ชจ๋‘ ์ž˜ ๋ณ€ํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

๋‹ค์Œ์—๋Š” MTV ๊ตฌ์กฐ์—์„œ T, V ์ž‘์„ฑ์„ ํ•ด๋ณผ ๊ฒƒ์ด๋‹ค!

 

 

 

+ Recent posts