반응형

Django 기반에서 HelloWorld를 화면에 출력하기 위한 내용 임

Django 설치는 Django 테스트 환경 구축 #1을 참조


  1. 설치된 django에서 새로운 app을 생성
    • #./manage.py startapp firstworld
    • firstworld가 생성 됨
  2. views.py 작성
    • from django.http import HttpResponse      #HttpResponse로 객체를 반환하기 위해 import
    • def index(request):                                       # index 함수 생성
    •       return HttpResponse("Hello. Welcome First World of Django")       # 문자열 객체 반환
    • 를 추가
  3. urls.py 작성
    • urls.py가 없는 경우 새로 생성
    • from django.urls import path
    • from . import views
    • urlpatterns = [
    •         path('', views.index, name='index')    #views의 index를 호출함
    • ]
    • 를 추가
  4. 최상위를 가지는 디렉토리로 이동하여 urls.py에 firstworld를 연결
    • path('firstworld/', include('firstworld.urls'))     # URI에 firstworld가 추가되는 경우 firstworld의 urls.py를 참조한다는 의미
  5. 서비스 재 시작 
    • 웹 브라우저에서 접속 
    • /firstworld/까지 입력