Django) 간단한 웹 애플리케이션 만들기 (1)
2022. 7. 18. 21:38ㆍ코딩/Django
728x90
반응형

아직 여기까지는 이해 가능.
views.py 모듈에서 요청값이 들어오면 HttpResponse를 이용해 http값으로 반환해주는 index, create 함수를 생성.
메인 페이지에는 Welcome!, 그리고 /create 를 붙이면 Create가 뜨도록 했다.


굿굿


<값>을 통해 views.py 에서 request 와 값을 정해줌으로써,
값에 대한 페이지의 출력값을 바꿀 수 있었다.

Web Server의 경우에는 정적으로 반응하고,
Web Application Server는 무겁고 느린 대신 동적이라 한다.


따라서 여러가지 기능을 이용해 웹페이지에 접속할 때마다 random으로 값이 변하는 페이지 또한 만들 수 있다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render, HttpResponse | |
import random | |
topics = [ | |
{'id':1, 'title':'routing', 'body':'Routing is ...'}, | |
{'id':2, 'title':'view', 'body':'View is ...'}, | |
{'id':3, 'title':'model', 'body':'Model is ...'}, | |
] | |
# Create your views here. | |
def index(request): | |
global topics | |
ol = '' | |
for topic in topics: | |
ol += f'<li><a href="/read/{topic["id"]}">{topic["title"]}</a></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(request, id): | |
return HttpResponse('Read!'+id) |
이를 응용해서 html을 이용해 코드를 만들었다.
topics 란 dictionary로 묶인 리스트를 만들어, for문에 ol을 집어넣어
ol구문에 하나씩 리스트가 나오도록 만들었다.
또한 해당 리스트에 링크를 걸었는데, read페이지로 넘어가 id 값에 해당하는 넘버가 뒤이어 출력된다.

728x90
반응형
'코딩 > Django' 카테고리의 다른 글
Django) 간단한 웹 애플리케이션 만들기 (3) (0) | 2022.07.20 |
---|---|
Django) 간단한 웹 애플리케이션 만들기 (2) (0) | 2022.07.19 |
Django) django 설치 및 오류 끝에 실행 완료! (0) | 2022.07.18 |