본문 바로가기

Computer Science/Django_pinterest

210818 장고 16강 GET, POST 프로토콜 실습

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해줘야한다.
#특정 주소를 만들어 주는 작업이 필요하다.