반응형
첫 번째 장고 앱 작성하기, part 1
간단한 설문조사(Polls) 어플리케이션을 만드는 과정을 따라해보면
- 요구사항
- 사람들이 설문 내용을 보고 직접 투표할 수 있는 개방된 사이트
- 관리자가 설문을 추가, 변경, 삭제할 수 있는 관리용 사이트
$ python -m django --version
django 프로젝트 만들기
$ django-admin startproject mysite
startproject를 수행하면 아래와 같은 파일이 생성된다.
- mysite/ 그냥 프로젝트를 담는 폴더
- manage.py
- 커맨드 유틸리티
- mysite/init.py
- mystie/settings.py
- project의 환경/구성
- mysite/urls.py
- 사이트의 목차
- mysite/wsgi.py
- 현재 프로젝트를 서비스하기 위한 WSGI 호환 웹 서버 진입
프로젝트 시작하기
$ python manage.py runserver
$ python manage.py runserver 8080
$ python manage.py runserver 0:8000
ec2-13-59-125-30.us-east-2.compute.amazonaws.com:8000
AWS를 이용한다면, inbound 설정을 해야한다 접속이 안되면, ALLOWHOST를 추가해야 한다. mysite/setting.py에 allowhost 추가
- 이제 설문조사 앱을 만들어 보자
앱을 만들기 위해서 아래와 같이 명령어를 실행
$ python manage.py startapp polls
위 명령어로 polls의 앱이 생성되었다.
polls/view.py 내부에 다음과 같이 코드를 붙여 넣고 polls/urls.py을 생성하고, 마지막으로 mysite/urls.py에 polls 앱을 연결하면 된다.
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
# polls/views.py | |
from django.http import HttpResponse | |
def index(request): | |
return HttpResponse("Hello, world. You're at the polls index.") | |
# polls/urls.py | |
from django.urls import path | |
from . import views | |
urlpatterns = [ | |
path('', views.index, name='index'), | |
] | |
# mysite/urls.py | |
from django.urls import include, path | |
from django.contrib import admin | |
urlpatterns = [ | |
path('polls/', include('polls.urls')), | |
path('admin/', admin.site.urls), | |
] |
[참고]
- https://docs.djangoproject.com/ko/2.0/intro/tutorial01/
반응형
'Programming > Web' 카테고리의 다른 글
[Django] 03.데이터베이스 연동하기(migration, model 생성) (0) | 2017.12.17 |
---|---|
[Django] 04. 어플리케이션 View 만들기 (0) | 2017.12.17 |
[Django] 01. 설치 및 환경 설정 (0) | 2017.12.17 |
[Web] React 시작하기 (0) | 2017.02.07 |
[HTTP] HTTP란? 특징 및 구성요소 - Request, Response, Structure, Method, Reponse Code (2) | 2016.04.15 |