hello_world.html
<form action="{% url 'hello_world' %}" method="POST">
<!-- action에는 적용시킬 url을 작성 -->
{% csrf_token %}
<input type="text" name="hello_world_input" id="">
<input type="submit" class="btn btn-primary " value="POST">
</form>
-form action="{% url 'hello_world' %}" ---url 상에서 name=hello_world인 url로 이동
-hello_world_input이 name인 텍스트에 작성하여 hello_world url대로 진행
from django.db import models
# Create your models here.
class HelloWorld(models.Model):
text = models.CharField(max_length=255, null=True)
models.py 클래스 작성
views.py
def hello_world(request):
if request.method == "POST":
text = request.POST.get('hello_world_input')
new_hello_world = HelloWorld()
new_hello_world.text = text
new_hello_world.save()
return render(request, 'accountapp/hello_world.html', {'hello_world_output': new_hello_world})
else:
return render(request, 'accountapp/hello_world.html', {'hello_world_output.text': 'GET METHOD!'})
-hello_world_input을 text로 받음
이때 hello_world_input은 hello_world.html 상에서 입력받은 텍스트의 이름이다
-new_hello_world는 HelloWorld 클래스의 인스턴스이다
new_hello_world.save() 코드를 통해 해당 내용을 db에 저장한다
-return render(request, 'accountapp/hello_world.html', {'hello_world_output': new_hello_world})
{'hello_world_output': new_hello_world} 데이터를 dictionary 형태로 보냄
다시 hello_world.html에 위의 데이터를 보여줄 코드를 작성
hello_world.html
<form action="{% url 'hello_world' %}" method="POST">
<!-- action에는 적용시킬 url을 작성 -->
{% csrf_token %}
<input type="text" name="hello_world_input" >
<input type="submit" class="btn btn-primary " value="POST">
</form>
{% if hello_world_output %}
<h1>
{{ hello_world_output.text }}
</h1>
{% endif %}
--> {% if hello_world_output %}
<h1>
{{ hello_world_output.text }}
</h1>
{% endif %}
hello_world_output이 존재한다면 hello_world_output.text를 보여주는 코드
회원가입 구현 (0) | 2021.07.26 |
---|---|
DB 정보 접근 및 장고 템플릿 내 for loop (0) | 2021.07.22 |
http 프로토콜 (15~16강) (0) | 2021.07.19 |
models.py class 작성 (14강) (0) | 2021.07.15 |
static 설정(11강) (0) | 2021.07.15 |
댓글 영역