장고에서 DB와 통신하는 역할이다.
Row, Coulmns, Item, Attributes
테이블 및 필드의 생성, 삭제, 변경 등과 같이 DB에 대한 변경사항을 알려주는 것이다.
클래스 모델을 만들고 makemigrations 명령어를 입력해 데이터베이스에서 테이블을 생성할 수 있도록 migrate 해준다.
models.py
class HelloWorld(models.Model):
text = models.CharField(max_length=255, null=False)
인터넷 상에서 통신하는 과정에서 사용되는 규약이다.
Http method 중 가장 많이 쓰이는 method이다.
Get은 서버에서 어떤 데이터를 가져와서 보여줄 때 사용한다. 어떤 값이나 내용, 상태 등을 바꾸지 않는 경우에 사용한다.
Post는 서버상의 데이터 값이나 상태를 바꾸기 위해 사용한다.
예를 들면, Get 방식은 글의 내용, 목록을 보여주는 경우이고 Post는 글의 내용을 저장하고 수정할 때에 사용한다.
➡️ Get은 가져온다는 개념이고, Post는 수행한다는 개념이다.
클라이언트에서 서버로 어떠한 리소스로부터 정보를 요청하기 위해 사용되는 메소드이다.
데이터를 읽거나, 검색할 때에 사용된다.
요청을 전송할 때 URL 주소 끝에 파라미터로 포함되어 전송된다.
요청은 오로지 데이터를 읽을 때만 사용되고 수정할 때는 사용하지 않는다.
즉, 데이터의 변형의 위험 없이 사용할 수 있다.
리소스를 생성, 업데이트 하기 위해 서버에 데이터를 보낸다.
GET과 달리 전송해야될 데이터를 HTTP 메세지의 body에 담아서 전송한다.
내용이 보이지 않아 GET보다 보안적인 측면에서 안전하다고 생각할 수 있지만 개발자 도구와 같은 툴로 내용을 확인할 수 있기 때문에 암호화가 필요하다.
(출처: https://whales.tistory.com/120#google_vignette)
hello_world.html
<form action="/account/hello_world/" method="post">
{% csrf_token %}
<input type="submit" class="btn btn-primary" value="POST">
</form>
<h1>
{{ text }}
</h1>
views.py
def hello_world(request):
if request.method == "POST":
return render(request, 'accountapp/hello_world.html', context={'text': 'POST METHOD!!!'})
else:
return render(request, 'accountapp/hello_world.html', context={'text': 'GET METHOD!!!'})
hello_world.html
<h1 style="font-family: 'Handjet', cursive;">
Hello World LIST!
</h1>
<form action="/account/hello_world/" method="post">
{% csrf_token %}
<div>
<input type="text" name="hello_world_input">
</div>
<div>
<input type="submit" class="btn btn-primary" value="POST">
</div>
</form>def hello_world(request):
if request.method == "POST":
temp = request.POST.get('hello_world_input')
new_hello_world = HelloWorld()
new_hello_world.text = temp
new_hello_world.save()
return render(request, 'accountapp/hello_world.html', context={'hello_world_output': new_hello_world})
else:
return render(request, 'accountapp/hello_world.html', context={'text': 'GET METHOD!!!'})
{% if hello_world_output %}
<h1>
{{ hello_world_output.text }}
</h1>
{% endif %}
views.py
def hello_world(request):
if request.method == "POST":
temp = request.POST.get('hello_world_input')
new_hello_world = HelloWorld()
new_hello_world.text = temp
new_hello_world.save()
return render(request, 'accountapp/hello_world.html', context={'hello_world_output': new_hello_world})
else:
return render(request, 'accountapp/hello_world.html', context={'text': 'GET METHOD!!!'})
views.py
return HttpResponseRedirect(reverse('accountapp:hello_world'))
else:
hello_world_list = HelloWorld.objects.all()
return render(request, 'accountapp/hello_world.html', context={'hello_world_list': hello_world_list})
디버깅은 모든 소프트웨어에서 소스 코드의 오류 또는 버그를 찾아서 수정하는 과정이다.
브레이크 포인트(Break Point)라고도 부르는데 소스 코드의 특정 지점에서 프로그램의 실행을 멈추는데 사용한다.
중단점을 놓고 이 상태에서 왜 에러가 나는지에 대해 파악이 가능하다.
디버깅을 중단하거나 종료하려면 디버거 패널의 stop 버튼 클릭 or Ctrl+F2
➡️ 개발의 편리함을 위해 디버깅 기능을 적극 활용하자!
장고는 CRUD(Create, Read, Update, Delete) 기능을 개발하기 위해 CRUD를 위한 view 기능을 제공한다.
CBV(Class Based View)는 클래스 기반 뷰이고, FBV(Function Based View)는 함수 기반 뷰이다.
편하게 구현 가능
읽기 편한 로직
데코레이터 사용이 명료함
확장, 재사용이 어려움
temp = request.POST.get('hello_world_input')
new_hello_world = HelloWorld()
new_hello_world.text = temp
new_hello_world.save()
이렇게 이해를 했는데 맞게 이해한 것인지 궁금하다.
CRUD에 대해선 알고 있었는데 장고는 이 기능을 뷰로 제공해 강력한 효과를 낸다.
그리고 클래스 기반 뷰와 함수 기반 뷰가 있는데 함수 기반 뷰는 우리에게 익숙한 형태지만 개발의 생산성에서는 다소 떨어지고 클래스 기반 뷰는 익숙하진 않지만 확장과 재사용이 용이해 효과적인 생산성을 보인다.