본문 바로가기
카테고리 없음

Django 뷰 작성해보기

by HoneyIT 2021. 1. 4.

views.py

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

가장 간단한 형태의 뷰입니다. 뷰를 호출하기 위해 이와 연결된 URL이 필요하며 RULconf가 사용됩니다.

 

 

urls.py 파일을 생성해줍니다.

 

polls/urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

 

django01/urls.py

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

from . import views

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

 

최상위 URLconf에서 polls.urls 모듈을 바라보게 설정합니다.

 

include() 함수는 다른 URLconf들을 참조할 수 있도록 도와줍니다. Django가 include함수를 만나면 URL의 그 시점까지 일치하는 부분을 잘라내고, 남은 문자열 부분을 후속차리하기 위해 include 된 URLconf로 전달합니다.

*다른 URL 패턴을 포함할 때마다 항상 include()를 사용해야 합니다.(admin.site.urls는 예외입니다.)

 

이제 서버를 실행하고 http://localhost:8000/polls/로 접속하면

 

잘 뜨는 것을 확인할 수 있습니다.


https://docs.djangoproject.com/ko/3.1/intro/tutorial01/

 

첫 번째 장고 앱 작성하기, part 1 | Django 문서 | Django

Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate

docs.djangoproject.com