Django

요구사항

virtualenv 환경

pip install virtualenv
virtualenv .venv  # 환경 생성
source .venv/bin/activate  # 환경 진입

or

python3 -m venv myvenv

django 설치

sudo pip install django

django-admin startproject mysite
pip install django==1.8

setup

django-admin startproject mysite .
# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Seoul'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
python manage.py migrate
python manage.py runserver

Create Django App

python manage.py startapp blog
    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'blog',
    )

define model

    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
python manage.py makemigrations blog
python manage.py migrate blog

django admin

from django.contrib import admin
from .models import Post

admin.site.register(Post)
python manage.py createsuperuser

Version Control with git(optional)

참고

What Else?