[Python3.6][Django2.2][Ubuntu18.04] Django 앱생성하여 간단한 웹페이지 띄우기 - 2
1. 앱 생성
polls라는 app 생성
./manage.py startapp polls
2. views.py 수정
vim polls/views.py
================================
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index2(request):
return HttpResponse("Hello, world. Yor're at the polls index2.")
================================
3. urls.py 생성 및 수정
위에서 작성한 뷰를 호출하려면 이와 연결된 URL 이 있어야 하는데, 이를 위해 URLconf가 사용됩니다.
polls 디렉토리에서 URLconf를 생성하려면, urls.py라는 파일을 생성해야 합니다.
polls/urls.py 생성하여 아래 코드 작성 후 저장
vim polls/urls.py
================================
from django.urls import path
from . import views
urlpatterns = [
path('', views.index2, name='sample_name_index'),
]
================================
최상위 URLconf 에서 polls.urls 모듈을 바라보게 설정합니다.
test001/urls.py 파일을 열고, django.urls.include를 import 하고, urlpatterns 리스트에 include() 함수를 다음과 같이 추가합니다.
vim test001/urls.py
================================
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
]
================================
4. 서버 가동 후 polls 앱의 웹페이지 정상 출력 확인
./manage.py runserver 0:8080
WEB PAGE : 999.888.777.666:8080/polls
정상적으로 ` Hello, world. Yor're at the polls index2. ` 출력 확인되면 완료!