00. Intro
00. Intro
03강_개발 환경 setup : Pycharm
•
Django 설치
Microsoft Windows [Version 10.0.19042.1415]
(c) Microsoft Corporation. All rights reserved.
(220106) C:\Users\jhe00\OneDrive\바탕 화면\jhe226\2022\Django\작정하고 장고!\220106>pip list
Package Version
---------- -------
pip 21.3.1
setuptools 60.2.0
(220106) C:\Users\jhe00\OneDrive\바탕 화면\jhe226\2022\Django\작정하고 장고!\220106>pip install django
Collecting django
Using cached Django-4.0.1-py3-none-any.whl (8.0 MB)
Collecting asgiref<4,>=3.4.1
Using cached asgiref-3.4.1-py3-none-any.whl (25 kB)
Collecting tzdata
Using cached tzdata-2021.5-py2.py3-none-any.whl (339 kB)
Collecting sqlparse>=0.2.2
Using cached sqlparse-0.4.2-py3-none-any.whl (42 kB)
Installing collected packages: tzdata, sqlparse, asgiref, django
Successfully installed asgiref-3.4.1 django-4.0.1 sqlparse-0.4.2 tzdata-2021.5
(220106) C:\Users\jhe00\OneDrive\바탕 화면\jhe226\2022\Django\작정하고 장고!\220106>cd..
(220106) C:\Users\jhe00\OneDrive\바탕 화면\jhe226\2022\Django\작정하고 장고!>django-admin
(220106) C:\Users\jhe00\OneDrive\바탕 화면\jhe226\2022\Django\작정하고 장고!>django-admin startproject pragmatic
Python
복사
•
Open Project → pragmatic 폴더 열기
•
가상환경 Setting
- Setting → Python Interpreter → 톱니바퀴 → Add → OK
•
프로젝트 생성
Microsoft Windows [Version 10.0.19042.1415]
(c) Microsoft Corporation. All rights reserved.
(pragmatic) C:\Users\jhe00\OneDrive\바탕 화면\jhe226\2022\Django\작정하고 장고!\pragmatic>pip list
Package Version
---------- -------
pip 21.3.1
setuptools 60.2.0
(pragmatic) C:\Users\jhe00\OneDrive\바탕 화면\jhe226\2022\Django\작정하고 장고!\pragmatic>pip install django
Collecting django
Using cached Django-4.0.1-py3-none-any.whl (8.0 MB)
Collecting asgiref<4,>=3.4.1
Using cached asgiref-3.4.1-py3-none-any.whl (25 kB)
Collecting tzdata
Using cached tzdata-2021.5-py2.py3-none-any.whl (339 kB)
Collecting sqlparse>=0.2.2
Using cached sqlparse-0.4.2-py3-none-any.whl (42 kB)
Installing collected packages: tzdata, sqlparse, asgiref, django
Successfully installed asgiref-3.4.1 django-4.0.1 sqlparse-0.4.2 tzdata-2021.5
(pragmatic) C:\Users\jhe00\OneDrive\바탕 화면\jhe226\2022\Django\작정하고 장고!\pragmatic>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin,
auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
January 06, 2022 - 23:08:45
Django version 4.0.1, using settings 'pragmatic.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[06/Jan/2022 23:09:01] "GET / HTTP/1.1" 200 10697
[06/Jan/2022 23:09:02] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[06/Jan/2022 23:09:02] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
[06/Jan/2022 23:09:02] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184
[06/Jan/2022 23:09:02] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692
Not Found: /favicon.ico
[06/Jan/2022 23:09:02] "GET /favicon.ico HTTP/1.1" 404 2113
Python
복사
04강_Django 개발 패턴
•
MVT 방식
1) Model
- Django에서 DB 언어를 사용하지 않고도 간편하게 DB를 다룰 수 있는 패턴
- Data와 쉽게 연결
- ex. Row / Columns Item / Attributes
(Article / Title, article, image...)
⇒ 중간 과정 : model
- Model 설정만 해주면 Django가 알아서 DB를 관리
2) View
- Django에서 계산의 대부분을 수행
- User가 Server를 통해 웹서비스에 Request ⇒ Request
3) Template
- JS, HTML, CSS ⇒ 실제로 볼 수 있는 작업과 관련
- User가 바라보는 User Interface 구현
- ex. User = Request Article ⇒ Server
내부에서 Article 구현 (by. HTML)
•
Django
Template View Model
01. Django Tutorial
01. Django Tutorial
05강_첫 앱 시작, 그리고 기본적인 view 만들기
•
•
pragmatic\pragmatic\settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accountapp',
]
Python
복사
•
서버 접속 시 바로 보이는 화면 구현 (view 생성)
- pragmatic\accountapp\migrations\views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def hello_world(request):
return HttpResponse('Hello world!')
'''
return HttpResponse -> alt+Enter
=> import 위치 찾아줌
'''
Python
복사
•
view를 특정 주소에 연결(라우팅)
- pragmatic\pragmatic\urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('account/', include('accountapp.urls')),
]
Python
복사
•
accountapp 하위 주소에서 hello_world에 접근
⇒ 해당 view를 return
from django.urls import path
from accountapp.views import hello_world
app_name = "accountapp"
# "accountapp:hello_world" 라는 간단한 함수도 존재
urlpatterns = [
path('hello_world/', hello_world, name = 'hello_world'),
# import할 때 alt+Enter를 잘 이용하도록 하자
]
Python
복사
•
확인하기
Microsoft Windows [Version 10.0.19042.1415]
(c) Microsoft Corporation. All rights reserved.
(pragmatic) C:\Users\jhe00\OneDrive\바탕 화면\jhe226\2022\Django\작정하고 장고!\pragmatic>python
manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migra
tions for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
January 07, 2022 - 00:59:24
Django version 4.0.1, using settings 'pragmatic.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Python
복사
01. Django Tutorial
05강_첫 앱 시작, 그리고 기본적인 view 만들기
•
•
pragmatic\pragmatic\settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accountapp',
]
Python
복사
•
서버 접속 시 바로 보이는 화면 구현 (view 생성)
- pragmatic\accountapp\migrations\views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def hello_world(request):
return HttpResponse('Hello world!')
'''
return HttpResponse -> alt+Enter
=> import 위치 찾아줌
'''
Python
복사
•
view를 특정 주소에 연결(라우팅)
- pragmatic\pragmatic\urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('account/', include('accountapp.urls')),
]
Python
복사
•
accountapp 하위 주소에서 hello_world에 접근
⇒ 해당 view를 return
from django.urls import path
from accountapp.views import hello_world
app_name = "accountapp"
# "accountapp:hello_world" 라는 간단한 함수도 존재
urlpatterns = [
path('hello_world/', hello_world, name = 'hello_world'),
# import할 때 alt+Enter를 잘 이용하도록 하자
]
Python
복사
•
확인하기
Microsoft Windows [Version 10.0.19042.1415]
(c) Microsoft Corporation. All rights reserved.
(pragmatic) C:\Users\jhe00\OneDrive\바탕 화면\jhe226\2022\Django\작정하고 장고!\pragmatic>python
manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migra
tions for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
January 07, 2022 - 00:59:24
Django version 4.0.1, using settings 'pragmatic.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Python
복사
06강_Git의 소개
•
Version Control ⇒ Branch
- Main Branch / Additional branch / Branch Merge
→ 기존 Branch에 영향X → Branch 병합
(분리 개발)
•
Team Work
07강_Gitignore 설정, 환경변수 분리, 첫 커밋
•
New File → .gitignore 생성 → 첨부된 gitignore 파일 입력
→ 마지막에 venv/ 입력(추적X)
•
Django-environ
1) Terminal에 $pip install django-environ 입력 후 install
2) pragmatic\pragmatic\settings.py 에 첨부된 django-environ 파일 입력
•
.gitignore에 .env,db.sqlite3, .idea 입력 ⇒ 추적 방지
•
Git 활성화 후 Terminal 입력
Microsoft Windows [Version 10.0.19042.1415]
(c) Microsoft Corporation. All rights reserved.
(venv) C:\Users\jhe00\OneDrive\바탕 화면\jhe226\2022\Django\작정하고 장고!\prag
matic>git commit -m "Initial commit"
[master (root-commit) d98057c] Initial commit
26 files changed, 361 insertions(+)
create mode 100644 .gitignore
create mode 100644 accountapp/__init__.py
create mode 100644 accountapp/__pycache__/__init__.cpython-39.pyc
create mode 100644 accountapp/__pycache__/admin.cpython-39.pyc
create mode 100644 accountapp/__pycache__/apps.cpython-39.pyc
create mode 100644 accountapp/__pycache__/models.cpython-39.pyc
create mode 100644 accountapp/__pycache__/urls.cpython-39.pyc
create mode 100644 accountapp/__pycache__/views.cpython-39.pyc
create mode 100644 accountapp/admin.py
create mode 100644 accountapp/apps.py
create mode 100644 accountapp/migrations/__init__.py
create mode 100644 accountapp/migrations/__pycache__/__init__.cpython-39.pyc
create mode 100644 accountapp/models.py
create mode 100644 accountapp/tests.py
create mode 100644 accountapp/urls.py
create mode 100644 accountapp/views.py
create mode 100644 manage.py
create mode 100644 pragmatic/__init__.py
create mode 100644 pragmatic/__pycache__/__init__.cpython-39.pyc
create mode 100644 pragmatic/__pycache__/settings.cpython-39.pyc
create mode 100644 pragmatic/__pycache__/urls.cpython-39.pyc
create mode 100644 pragmatic/__pycache__/wsgi.cpython-39.pyc
create mode 100644 pragmatic/asgi.py
create mode 100644 pragmatic/settings.py
create mode 100644 pragmatic/urls.py
create mode 100644 pragmatic/wsgi.py
(venv) C:\Users\jhe00\OneDrive\바탕 화면\jhe226\2022\Django\작정하고 장고!\prag
matic>git status
On branch master
nothing to commit, working tree clean
Python
복사
•
pragmatic\accountapp\__pycache__와 pragmatic\pragmatic\__pycache__ 삭제
•
두 폴더 삭제 후 Terminal 입력
(venv) C:\Users\jhe00\OneDrive\바탕 화면\jhe226\2022\Django\작정하고 장고!\pragmatic>git add .
(venv) C:\Users\jhe00\OneDrive\바탕 화면\jhe226\2022\Django\작정하고 장고!\pragmatic>git com
mit -m "Delete redundant files"
[master 291dfe8] Delete redundant files
11 files changed, 1 insertion(+)
delete mode 100644 accountapp/__pycache__/__init__.cpython-39.pyc
delete mode 100644 accountapp/__pycache__/admin.cpython-39.pyc
delete mode 100644 accountapp/__pycache__/apps.cpython-39.pyc
delete mode 100644 accountapp/__pycache__/models.cpython-39.pyc
delete mode 100644 accountapp/__pycache__/urls.cpython-39.pyc
delete mode 100644 accountapp/__pycache__/views.cpython-39.pyc
delete mode 100644 pragmatic/__pycache__/__init__.cpython-39.pyc
delete mode 100644 pragmatic/__pycache__/settings.cpython-39.pyc
delete mode 100644 pragmatic/__pycache__/urls.cpython-39.pyc
delete mode 100644 pragmatic/__pycache__/wsgi.cpython-39.pyc
(venv) C:\Users\jhe00\OneDrive\바탕 화면\jhe226\2022\Django\작정하고 장고!\pragmatic>git status
On branch master
nothing to commit, working tree clean
Python
복사
•
pragmatic\.gitignore에 __pycache__/ 추가
•
django-environ
import environ
import os
env = environ.Env(
# set casting, default value
DEBUG=(bool, False)
)
# Set the project base directory
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Take environment variables from .env file
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))
# False if not in os.environ because of casting above
DEBUG = env('DEBUG')
# Raises Django's ImproperlyConfigured
# exception if SECRET_KEY not in os.environ
SECRET_KEY = env('SECRET_KEY')
# Parse database connection url strings
# like psql://user:pass@127.0.0.1:8458/db
DATABASES = {
# read os.environ['DATABASE_URL'] and raises
# ImproperlyConfigured exception if not found
#
# The db() method is an alias for db_url().
'default': env.db(),
# read os.environ['SQLITE_URL']
'extra': env.db_url(
'SQLITE_URL',
default='sqlite:////tmp/my-tmp-sqlite.db'
)
}
CACHES = {
# Read os.environ['CACHE_URL'] and raises
# ImproperlyConfigured exception if not found.
#
# The cache() method is an alias for cache_url().
'default': env.cache(),
# read os.environ['REDIS_URL']
'redis': env.cache_url('REDIS_URL')
}
Python
복사
08강_Django Template의 extends, include 구문과 render 함수
•
H(yper) T(ext) M(arkup) L(anguage)
•
Django Template에서 자주 쓰이는 구문
1) extends
-Pre-made Template html
-미리 만들어둔 html 파일을 가져와 이를 바탕으로 나머지 내용을 채워나감
-바탕을 깔아주는 느낌
2) include
-html 파일을 만들면서 다른 내용을 가져와 붙임
-덩이를 가져와서 갖다 붙이는 느낌
→ extends로 바탕을 가져와서 include로 채움
⇒ Response View 생성
•
render(request, import할 file 이름)
-pragmatic\accountapp\migrations\views.py
from django.shortcuts import render
def hello_world(request):
# return HttpResponse("안녕하세요") => view에서 직접 response를 만들어 return
return render(request, 'accountapp\helloworld.html')
# html 파일(template)을 가지고 와서 채워넣는 형식
Python
복사
•
pragmatic\pragmatic\settings.py
# TEMPLATES
'DIRS' : {os.path.join(BASE_DIR, 'templates')},
Python
복사
⇒ view의 base.html과 연결
•
pragmatic\templates\base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Hello world!
</body>
</html>
HTML
복사
09강_include / extends / block 구문을 이용한 뼈대 html 만들기
•
pragmatic\templates\base.html
<!DOCTYPE html>
<html lang="ko">
{% include 'head.html' %}
<body>
{% include 'header.html' %}
{% block content %}
{% endblock %}
{% include 'footer.html' %}
</body>
</html>
HTML
복사
Pycharm 기능 : 커서 + alt + 커서 ⇒ 다중 커서 입력 가능!
•
pragmatic\templates\head.html
<head>
<meta charset="UTF-8">
<title>Pragmatic</title>
</head>
HTML
복사
•
pragmatic\templates\header.html
<div style = "height : 10rem; background-color : #38df81; border-radius : 1rem; margin : 2rem;">
</div>
HTML
복사
•
pragmatic\templates\footer.html
<div style = "height : 10rem; background-color : #38df81; border-radius : 1rem; margin : 2rem;">
</div>
HTML
복사
•
pragmatic\accountapp\templates\accountapp\helloworld.html
{% extends 'base.html' %}
{% block content %}
<div style = "height : 20rem; background-color : #38df81; border-radius : 1rem; margin : 2rem;">
<h1>
testing
<\h1>
</div>
{% endblock %}
HTML
복사
10강_style, 구글 폰트를 통해 Header, Footer 꾸미기
•
pragmatic\templates\header.html
<div style = "text-align : center, margin : 2rem 0;">
<div>
<h1 style = "font-family: 'Lobster', cursive;">Pragmatic<\h1>
</div>
<div>
<span>nav1<\span>
<span>nav2<\span>
<span>nav3<\span>
<span>nav4<\span>
</div>
</div>
HTML
복사
margin : value1, value2 ⇒ value 1 : 상하, value2 : 좌우
•
pragmatic\templates\footer.html
<div style = "text-align : center, margin-top : 2rem;">
<div style = "font-size : .6rem;">
<span>공지사항</span> |
<span>제휴문의</span> |
<span>서비스 소개</span>
</div>
<div style = "margin-top 1rem;">
<h6 style = "font-family: 'Lobster', cursive;">Pragmatic</h6>
</div>
</div>
HTML
복사
•
127.0.0.1:8000/account/helloworld ⇒ F12
⇒ Chrome에서 임시로 설정해보기
element.style{
text-align : center;
}
HTML
복사
•
pragmatic\templates\base.html
<!DOCTYPE html>
<html lang="ko">
{% include 'head.html' %}
<body>
{% include 'header.html' %}
<hr>
{% block content %}
{% endblock %}
<hr>
{% include 'footer.html' %}
</body>
</html>
HTML
복사
•
bootstrap 적용 ⇒ head.thml에 css 링크 복붙
•
pragmatic\templates\head.html
<head>
<meta charset="UTF-8">
<title>Pragmatic</title>
<!-- BOOTSTRAP LINK -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<!-- GOOGLE FONTS LINK -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Lobster&display=swap" rel="stylesheet">
</head>
HTML
복사
•
폰트 → 구글 폰트 이용