📎 what is Read?
👉 Read
📎 Read 구현
👉 홈페이지 구현
views.py(part1)
결과
views.py(part2)
👉 상세보기 구현
아무리 복잡한 애플리케이션이라고 4가지 작업, Create, Read, Update, Delete 안에서 이루어진다. 이중에 우리는 오늘 Read에 대해서 살펴본다.
from django.shortcuts import render, HttpResponse
def index(request):
return HttpResponse('''
<html>
<body>
<h1>Django</h1>
<ol>
<li>routing</li>
<li>routing</li>
<li>routing</li>
</ol>
<h2>Welcome</h2>
Hello, Django
</body>
</html>
''')
def create(request):
return HttpResponse('Create')
def read(requeat, id):
return(HttpResponse('read!'+id)
Django
- routing
- view
- model
Welcome
Hello, Django
위와 같은 코드를 통해 웹페이지의 전체적인 틀을 잡아준다.
from django.shortcuts import render, HttpResponse
topics = [
{'id':1, 'title':'routing', 'body':'Routing is...'},
{'id':2, 'title':'view', 'body':'View is...'},
{'id':3, 'title':'model', 'body':'Model is...'}
]
def index(request):
global topics
for topic in topics:
ol += f'<li><a></a href="/read/{topics["id"]}">{topics["title"]}</li>'
return HttpResponse(f'''
<html>
<body>
<h1>Django</h1>
<ol>
{ol}
</ol>
<h2>Welcome</h2>
Hello, Django
</body>
</html>
''')
def create(request):
return HttpResponse('Create')
def read(requeat, id):
return(HttpResponse('read!'+id)
홈페이지의 정보를 딕셔너리에 담고, 이를 리스트로 담아둔다. 이 작업을 하는 이유는 이를 통해, 코드를 효과적으로 재사용할 수 있기 때문이다.
그리고 바로 반복문과 이를 이용해서 ol태그 안의 내용을 간단하게 나타날 수 있게 되었다. 결과는 위와 같게 되지만 코드를 좀 더 효율적으로 작성할 수 있게 되었다.
from django.shortcuts import render, HttpResponse
topics = [
{'id':1, 'title':'routing', 'body':'Routing is...'},
{'id':2, 'title':'view', 'body':'View is...'},
{'id':3, 'title':'model', 'body':'Model is...'}
]
def HTMLTemplate(articleTag):
global topics
for topic in topics:
ol += f'<li><a></a href="/read/{topics["id"]}">{topics["title"]}</li>'
return f'''
<html>
<body>
<h1><a href="/">Django</a></h1>
<ol>
{ol}
</ol>
{articleTag}
</body>
</html>
'''
def index(request):
article = '''<h2>Welcome</h2>
Hello, Django'''
return HttpResponse(HTMLTemplate(article))
def create(request):
return HttpResponse('Create')
def read(requeat, id):
return(HttpResponse('read!'+id)
우리가 이전까지 딱히 아무 경로없이 들어오게 될 때(path('', views.index)를 통해 라우팅 했었음)보여줄 페이지를 index함수 안에 만들어 줬었는데 이제 그 코드를 다른 HTMLTemplate()이라는 함수로 따로 만들어 코드를 재사용할 수 있게 만들었다. 그리고 이전에 h2태그는 각 경로마다 변해야할 본문의 내용이므로 변수로 만들었다. 이를 index()함수에서는 article이라는 변수이름으로 설정하여 HTMLTemplate()함수에 매개변수로 넣는다. 이 함수에서는 articleTag라는 매개변수로 받아서 작동한다.
from django.shortcuts import render, HttpResponse
topics = [
{'id':1, 'title':'routing', 'body':'Routing is...'},
{'id':2, 'title':'view', 'body':'View is...'},
{'id':3, 'title':'model', 'body':'Model is...'}
]
def HTMLTemplate(articleTag):
global topics
for topic in topics:
ol += f'<li><a></a href="/read/{topics["id"]}">{topics["title"]}</li>'
return f'''
<html>
<body>
<h1><a href="/">Django</a></h1>
<ol>
{ol}
</ol>
{articleTag}
</body>
</html>
'''
def index(request):
article = '''<h2>Welcome</h2>
Hello, Django'''
return HttpResponse(HTMLTemplate(article))
def create(request):
return HttpResponse('Create')
def read(request, id):
global topics
article = ''
for topic in topics:
if topic['id'] == int(id):
article = f'<h2>{topic["title"]}</h2>{topic["body"]}'
return HttpResponse(HTMLTemplate(article))
read도 위와 같은 방법으로 구현하면 된다.
이제 웹페이지의 흐름을 다시 상기시켜보자
routing을 클릭했을 때, http://127.0.0.1/read/1/ 로 접속하게된다. 이때 id는 1의 값을 가지게 된다. 이 id값은 read()함수의 매개변수로 들어가고 반복문에서 읽은 리스트에서의 튜플안의 id값과 비교하게 된다. 이렇게 비교한 값이 일치하면 조건문에 따라 튜플의 데이터를 이용해 상세보기를 구현하게 되는 것이다.