Django
- python의 웹프레임워크
- https://www.djangoproject.com/
- 경쟁 제품 : flask
요구사항
- python
- pip
virtualenv 환경
textpip install virtualenv
virtualenv .venv # 환경 생성
source .venv/bin/activate # 환경 진입
or
textpython3 -m venv myvenv
django 설치
textsudo pip install django
django-admin startproject mysite
- set django version
textpip install django==1.8
setup
textdjango-admin startproject mysite .
text# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Seoul'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
- migrate to database
textpython manage.py migrate
- run django server
textpython manage.py runserver
Create Django App
textpython manage.py startapp blog
- append app into settings.py
text INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
)
define model
blog/models.py
text from django.db import models
from django.utils import timezone
class Post(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
- sync table database
textpython manage.py makemigrations blog
python manage.py migrate blog
django admin
blog/admin.py
textfrom django.contrib import admin
from .models import Post
admin.site.register(Post)
- create super user
textpython manage.py createsuperuser
Version Control with git(optional)
참고
- django
- django girls tutorial
- django unittest