DB save
1. send POST data
2. receive POST data
3. save DB
<form action="/account/hello_world/" method="post">
{% csrf_token %}
<div>
<input type="text" name="hello_world_input">
<div>
<input type="submit" class="btn btn-primary" value="POST">
</div>
</div>
</form>
hello_world.html
def hello_world(request):
if request.method == "POST":
temp = request.POST.get('hello_world_input')
return render(request, 'accountapp/hello_world.html', context={'text' :temp})
else:
return render(request, 'accountapp/hello_world,html', cotext={'text': 'GET METHOD'})
view.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', cotext={'text': 'GET METHOD'})
저장된 데이터 if문으로 다시 html에서 출력
<form action="/account/hello_world/" method="post">
{% csrf_token %}
<div>
<input type="text" name="hello_world_input">
<div>
<input type="submit" class="btn btn-primary" value="POST">
</div>
</div>
</form>
{% if hello_world_output %}
<h1>
{{text}}
</h1>
{% endif %}
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()
hello_world_list = HelloWorld.objects.all()
return render(request, 'accountapp/hello_world.html', context={'hello_world_list' :hello_world_list})
else:
hello_world_list = HelloWorld.objects.all()
return render(request, 'accountapp/hello_world,html', cotext={'hello_world_list': hello_world_list})
view.py
<form action="/account/hello_world/" method="post">
{% csrf_token %}
<div>
<input type="text" name="hello_world_input">
<div>
<input type="submit" class="btn btn-primary" value="POST">
</div>
</div>
</form>
{% if hello_world_list %}
<h1>
{{ hello_world_list }}
</h1>
{% endif %}
hello_world.html
-> 쿼리셋 형태로 객체들이 출력된다
{% if hello_world_list %}
{% for hello_world in hello_world_list %}
<h4>
{{ hello_world.text }}
</h4>
{% endfor %}
{% endif %}
hello_world.html
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 HttpResponseRedirect(reverse('accountapp:hello_world'))
else:
hello_world_list = HelloWorld.objects.all()
return render(request, 'accountapp/hello_world,html', cotext={'hello_world_list': hello_world_list})
HttpRespenseRedirect: 해당 주소로 재접속 하라
reverse() : accoutapp:helo_world를 accountapp/hello_world로 바꿔주는 함수

class based view를 사용해서 가독성도 좋고 사용하기 편리함

CRUD
