티스토리 뷰
Intro
지금 보고있는 티스토리 jinozblog.tistory.com 첫화면의 구성은 다음과 같다.
3개의 포스트가 리스트 형식으로 나열되어있다.
포스트 미리보기에 구성된 항목을 보면 다음과 같다.
1. title : 글쓰기 제목
2. text : 글쓰기 내용
3. date : 발행 시간
4. category : post 구분
5. thumbnail : 미리보기 사진, 엄지손톱만한 그림에서 유래
여기서 1번~3번 항목에 category 항목에 user를 표시하는 형태로 구성하려고 합니다.
사전작업으로 http://127.0.0.1:8000/admin/에 로그인하여 사용자(들) 추가를 하였고 새로생성한 user로 로그인하여 2개의 포스트를 작성한 후 superuser로 재로그인하여 1개의 포스트를 작성하였으며 결과는 다음과 같다.
참고 : 현재 models.py에 user(author)항목을 담당할 필드(field)가 구분되어있지 않다. DB 확인 용으로 만들어 보았다.
포스트는 최근 순서로 위에 정렬된다.
DB Browser for SQLite : Table 확인
auth_user table을 확인해보면 superuser외 jinoz_user1이 추가된 것을 확인할 수 있다.
post_post table을 확인해보면 3개 post가 추가된 것을 확인할 수 있다. 기존 모델에서 선언한 3개의 필드를 확인할 수 있다.
jinozblog2019 tree
.
├── db.sqlite3
├── jblog
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
└── post
├── __init__.py_
├── admin.py
├── apps.py
├── migrations
├── models.py
├── templates
│ └── home.html
├── tests.py
└── views.py
1. models.py 수정
--post/models.py
from django.conf import global_settings # 초기모델에 추가, 앱설치포스트참고
from django.db import models
# Create your models here.
class Post(models.Model):
author = models.ForeignKey(global_settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True) # 초기모델에 추가
pub_date = models.DateTimeField('date published')
title = models.CharField(max_length=20)
text = models.CharField(max_length=300)
def __str__(self):
return self.text
--
2. models.py DB 반영
참고로 models.py 수정 후 반영/적용(migration/migrate)해주어야 한다.
개발서버가 실행 중에 module이 수정되면 오류 발생을 알려주지만 반영/적용여부를 알려주지는 않는다.
(jbe)$ python3 manage.py makemigrations post
Migrations for 'post':
post/migrations/0002_post_author.py
- Add field author to post
(jbe)$ python3 manage.py showmigrations post
post
[X] 0001_initial
[ ] 0002_post_author
python3 manage.py migrate post
Operations to perform:
Apply all migrations: admin, auth, contenttypes, post, sessions
Running migrations:
Applying post.0002_post_author... OK
$ python3 manage.py showmigrations post
post
[X] 0001_initial
[X] 0002_post_author
적용된 부분을 확인하기 위해 관리자로 재로그인 후 post 추가를 실행하면 Author 부분이 추가로 생성되어있는 것을 확인 할 수 있다.
Author 부분에서 superuser와 user를 이용해 post를 1개씩 더 추가하여 post 5개를 만든다.
DB Browser for SQLite : Table 확인
post_post table을 확인해보면 2개 post가 추가되어 5개가 된 것을 확인할 수 있다.
하지만 models.py에서 author 필드가 추가되어있지 않다.
DB Browser에서 데이터베이스를 닫고 새로열면 아래처럼 author_id가 반영되어있는 것을 확인 할 수 있다.
지금까지 신규 유저를 추가하고 post를 작성하여 SQLite DB에 적용하였다.
다음 포스트에서는 DB의 Table(post_post)를 views.py를 통해 post_list를 구현해보도록 하겠다.
'python > Django' 카테고리의 다른 글
[python] django project 구성 및 용어 정리 - 진행중.... (0) | 2019.10.15 |
---|---|
(macOS)[python] django post_list site에 구현하기 - 2/2 (0) | 2019.10.14 |
(macOS)[python] localhost(127.0.0.1:8000) main page에 "Welcome to visit django:jinoblog2019" 출력하기 (0) | 2019.10.10 |
[python] django(장고, 웹 프레임워크), MTV Architecture: Model-Template-View (0) | 2019.10.08 |
(macOS)[python] django superuser 등록 및 app 설치 (0) | 2019.10.07 |
- Total
- Today
- Yesterday
- Django
- sublime text
- git
- 코로나19
- MacOS
- 확진
- COVID-19
- 라즈베리파이
- Python
- DS18B20
- vscode
- analysis
- DAQ
- template
- raspberrypi
- Templates
- 자가격리
- Regression
- Raspberry Pi
- server
- arduino
- github
- Pandas
- ERP
- r
- SSH
- pyserial
- 코로나
- CSV
- Model
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |