[go: up one dir, main page]

OKdevTV Docs
Home OKdevTV
ESC
django 2 min read 2.3 KB Raw Markdown
On this page (9)

Django

요구사항

virtualenv 환경

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

or

text
python3 -m venv myvenv

django 설치

text
sudo pip install django

django-admin startproject mysite
  • set django version
text
pip install django==1.8

setup

text
django-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
text
python manage.py migrate
  • run django server
text
python manage.py runserver

Create Django App

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

django admin

  • blog/admin.py
text
from django.contrib import admin
from .models import Post

admin.site.register(Post)
  • create super user
text
python manage.py createsuperuser

Version Control with git(optional)

참고