Python

Python) Django 웹페이지 읽기

로픽 2022. 1. 18. 02:00
300x250

Django 웹페이지 읽기

* 참고 : https://www.youtube.com/watch?v=7ovAmZjDWmk&list=PLuHgQVnccGMDLp4GH-rgQhVKqqZawlNwG&index=7 


장고 웹페이지 읽기를 위한 소스코드

 

* views.py (구성 환경에 따라 소스코드는 달라질 수 있음)

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 ...'}
]

# Create your views here.
def index(request):
    global topics
    li = ''
    for topic in topics:
        li += f'<li><a href="/read/{topic["id"]}">{topic["title"]}</a></li>'

    return HttpResponse(f'''
    <html>
    <body>
        <h1>Django</h1>
        <ol>
        {li}
        </ol>
    </body>
    </html>
    ''')

def create(request):
    return HttpResponse("Create!")

def read(request, id):     # id 파라미터로 인입
    return HttpResponse("Read !!! " + id)

 

* urls.py (구성 환경에 따라 소스코드는 달라질 수 있음)

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myApp.urls'))
]

장고 실행 결과

topic에 포함된 값에 따라 출력

 

view 링크 클릭시 출력 화면

 

 

 

 

반응형