16강 GET, POST 프로토콜 실습
16강
GET,POST 실습
FORM이란것은 서버한테 보내는 요청명세서같은 것이다.
어떤글이나 파일을 첨부한다거나 할때 모든 것들이 폼안에 들어간다. 액션 벨류에는
요청을 하려는 URL을 넣는다.
<input type="submit" class btn-primary> 강의초반 부트스트랩 라이브러리 안에 있는 것 중 하나
프라이머리 색을 가진 버튼이 그럴듯하게 잡힌다.
{% extends 'base.html' %}
{% block content %}
<div style="height: 20rem; background-color: #38df81; border-radius: 1rem; margin: 2rem;">
<h1>
testing
</h1>
<form action="/account/hello_world" method="post">
<input type="submit" class="btn-primary" value="POST" >
</form>
</div>
{% endblock %}
위와같이 코딩을하고 서버를 열어 들어가면 FORBIDDEN 에러가 난다.
그러므로 항상 CSRF토큰을 명시 해줘야한다 . 장고에서 제공하는 보안기닁
{% extends 'base.html' %}
{% block content %}
<div style="height: 20rem; background-color: #38df81; border-radius: 1rem; margin: 2rem;">
<h1>
testing
</h1>
<form action="/account/hello_world" method="post">
{% csrf_token %}
<input type="submit" class="btn-primary" value="POST" >
</form>
</div>
{% endblock %}
뷰단에서 포스트와 겟을 나눠서 처리해줄 알고리즘을 적용한다ㅣ
account views
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
#view 단의 완성
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 !!!!'})
#view는 만들었고 이제 route해줘야한다.
#특정 주소를 만들어 주는 작업이 필요하다.
'Computer Science > Django_pinterest' 카테고리의 다른 글
210819 18강 DB 정보 접근 및 장고 템플릿 내 for loop (0) | 2021.08.19 |
---|---|
210819 장고 17강 POST 통신을 이용한 DB 데이터 저장 실습 (0) | 2021.08.19 |
20210818 장고 핀터레스트 14강Model, DB 연동,~15강 서버와 통신하는 HTTP 프로토콜 메서드 중에서도 GET, POST (0) | 2021.08.18 |
20210817 장고 핀터레스트 12강 ~13강 (0) | 2021.08.18 |
210816 장고 : 핀터레스트 11강 (static 설정 및 css 파일 분리) (0) | 2021.08.16 |