[python] django – web dev framework 기본 구성

Django Mysql로 설치

 

Pip install django

Pip install python-mysql

 

Django-admin startproject [프로젝트명]

 

Python manage.py help

 

먼저 mysql에 데이터베이스 셍성

생성된 프로젝트 settings.py 에 다음과 같이 데이터베이스를 mysql로 설정 (기본 sqlite3)

 

DATABASES = {

‘default’: {

‘ENGINE’: ‘django.db.backends.mysql’,

‘NAME’ : ‘HONGDB’,

‘USER’ : ‘사용자’,

‘PASSWORD’ : ‘비밀번호’,

‘HOST’ : ‘127.0.0.1’,

‘PORT’ : ‘3306’,

#’ENGINE’: ‘django.db.backends.sqlite3′,

#’NAME’: os.path.join(BASE_DIR, ‘db.sqlite3′),

}

}

 

Python manage.py migrate

디비 생성후

Python manage.py createsuperuser

슈퍼 유저 생성

 

프로젝트의 views.py 를 이용해서 해당 function을 이용하기 위해 import 를 해준다.

여기서 webapps는 나의 프로젝트명이고, views.py 는 그냥 내가 만들어준 function용 py다.

# sungho make views page

from webapps.views import index, sungho

 

Url 패턴 입력시 인덱스 및 특정 펑션 설정을 해준다.

urlpatterns = [

url(r’^admin/’, admin.site.urls),

url(r’^$’, index),  #add index

url(r’^sungho/’, sungho), #add sungho

]

 

Views.py

from django.http import HttpResponse

def index(request):

return HttpResponse(“Index Page”)

def sungho(request):

return HttpResponse(“Hello My Name is sungho”)

 

Django의 http를 이용해 response를 설정해준다.

위에서는 간단하게 스트링을 리턴하여, 화면에 출력하는 용도로 만들었음.

 

 

다음..

 

mysql> grant all privileges on sungho.* to ‘nic2hong’@’127.0.0.1′ identified by ’12Tjdgh#$’;

Query OK, 0 rows affected, 1 warning (0.01 sec)

 

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

 

 

글쓴이