Create
Read
Update
Delete
모든 어플리케이션들은 이 기능을 구현한다.
topics = [
{'id':1, 'title':'routing', 'body':'Routing is ..'},
{'id':2, 'title':'View', 'body':'View is ..'},
{'id':3, 'title':'Model', 'body':'Model is ..'},
]
내가 담고 싶은 내용들을 딕셔너리로 만들고 딕셔너리를 쉽게 담을 수 있는 topics이라는 리스트로 구성했다.
def HTMLTemplate(articleTag, id=None):
global topics
for topic in topics:
ol += f'<l.i><a href="/read/{topic["id"]}"> {topic["title"]}</l.i>'
return f'''
<html>
<body>
<h1><a href="/">Django</a></h1>
<ol>
{ol}
</ol>
{articleTag}
<ul>
<li><a href="/create/">create</a></li>
{contextUI}
</ul>
</body>
</html>
'''
HTMLTemplate(): 다른 함수에도 재사용하는 코드를 이 함수 내에 담았다. 이 프로젝트의 전체 웹페이지를 보여주는 함수
def index(request):
article = '''<h2>Welcome</h2> Hello, Django ''' return HttpResponse(HTMLTemplate(article))

따라서 해당 경로로 접속하면 위의 화면이 프린트 된다.
def read(request, id):
global topics
article = ''
for topic in topics:
if topic["id"] == int(id):
article = f'<h.2>{topic["title"]}</h.2>{topic["body"]}'
return HttpResponse(HTMLTemplate(article, id))
홈 화면에 create (글 생성 기능)을 추가하려고 한다.
def HTMLTemplate():
<ul> <li><a href="/create/">create</a></li> </ul>
def create(request):
return HttpResponse('Create')
아까 생성했던 HTMLTemplate 함수 밑에 create 화면을 추가한다.
따라서 /create/ 링크로 이동하면 Create라고 쓰여진 화면을 볼 수 있다.
귀찮아서 캡쳐는 안 함
article = '''
<form action="/create/" method="post"> <p><input type = "text" name="title" placeholder="title"></p> <p><textarea name="body" placeholder="body"></textarea></p> <p><input type = "submit"</p> </form> ''' return HttpResponse(HTMLTemplate(article))