떠올리는 공간

210106 Django - 첫번째 앱 장고앱 작성하기 튜토리얼 part3까지 완료

오리_꿀꿀 2021. 1. 6. 20:34

MVC & MTV

Model : 안전하게 데이터를 저장

View : 데이터를 적절하게 유저에게 보여줌

Control, Template : 사용자의 입력과 이벤트에 반응하여 Model과 View를 업데이트

 

MVC 패턴

 

" 특정 영역을 분리를 하는 것이 중요 "

 

 

Django 설치 및 시작

$ python -m pip install Django #Django 설치
django-admin startproject djpy # cd (원하는 경로)에서 djpy프로젝트 생성
//cd djpy
python manage.py startapp community # community app 생성
python manage.py migrate #database 생성 (db.sqlite3)
python manage.py createsuperuser #superuser 생성
python manage.py runserver # server 실행
python manage.py runserver 8080 # 8080포트로 서버 실행
#서버 종료 : ctrl + c
# http://127.0.0.1:8080/admin/ 어드민계정으로 관리할 수 있음

setting.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'community', #community app 추가
]

models.py

from django.db import models

# Create your models here.
class Article(models.Model):
    name = models.CharField(max_length=50)
    title = models.CharField(max_length=50)
    contents = models.TextField()
    url = models.URLField()
    email = models.EmailField()
    cdate = models.DateTimeField(auto_now_add=True) #auto_now_add=True 게시물 생성시 자동 입력

community app에 변화가 있는지 확인

python manage.py makemigrations community

모델에 대한 데이터 테이블을 dbsqlite에 생성

python manage.py migrate

이후 튜토리얼을 따라해본다.

첫 번째 장고 앱 작성하기, part 1 | Django 문서 | Django (djangoproject.com)

 

첫 번째 장고 앱 작성하기, part 1 | Django 문서 | Django

Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate

docs.djangoproject.com

인프런 - Django 초보 가이드 - 실습을 통해 알아보는 장고 입문 (inflearn.com)

 

Django 초보 가이드 - 실습을 통해 알아보는 장고 입문 - 인프런

웹프레임 워크 장고(Django) 를 소개부터 기본적인 사용법을 알아봅니다. 입문 웹 개발 프로그래밍 언어 Django 온라인 강의 Django 초보

www.inflearn.com

 

**Models의 Object 인식 못할 시 처리

python - Class has no objects member - Stack Overflow

 

Class has no objects member

def index(request): latest_question_list = Question.objects.all().order_by('-pub_date')[:5] template = loader.get_template('polls/index.html') context = {'latest_question_list':

stackoverflow.com