python --version
pip --version
pip3 install virtualenv
virtualenv venv
.\venv\Scripts\activate
(venv) pip3 install django
(venv) pip list
(venv) django-admin startproject do_it_django_prj .
(venv) python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
python manage.py startapp blog
python manage.py startapp single_pages
1. Post 모델 만들기
from django.db import models
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=30)
content = models.TextField()
created_at = models.DateTimeField()
2. 데이터베이스에 Post 모델 반영
python manage.py makemigrations
python manage.py migrate
1. admin.py에 Post 모델 추가
from django.contrib import admin
from .models import Post
# Register your models here.
admin.site.register(Post)
2. 새로운 포스트 생성
1. str() 함수로 포트스 제목과 번호 보여주기(models.py)
from django.db import models
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=30)
content = models.TextField()
created_at = models.DateTimeField()
def __str__(self) :
return f'[]{self.pk}]{self.title}'
2. 특정 지역 기준으로 작성 시각 설정
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Seoul'
USE_I18N = True
USE_L10N = True
USE_TZ = False
3. 자동으로 작성 시각과 수정 시각 저장
DateTimeField
from django.db import models
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=30)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
update_at = models.DateTimeField(auto_now=True)
def __str__(self):
return f'[{self.pk}]{self.title}'
python manage.py makemigrations
python manage.py migrate
python manage.py runserver