28. Web이란 무엇인가?
우리가 앞으로 만들 것
#include <iostream>
using namespace std;
int main()
{
cin >> input;
// do something
cout << output;
return 0;
}
C++
29. Web이란 무엇인가?
우리가 앞으로 만들 것
input = raw_input()
# do something
print output
Python
30. Web이란 무엇인가?
우리가 앞으로 만들 것
print "%sn" % "Content-Type:text/html;"
print "<title>HeXA; Hackers' eXciting Academy -</title>n"
print "<script type="text/javascript"
src="./script/base_script.js"></script>“
input = raw_input()
if not input:
print "<p>Error</p>"
else:
print "<p>Welcome</p>"
Python
37. 기본 구조
일단 프로젝트를 만들자
~ $ django-admin.py startproject blog
~ $ cd blog
~/tutorial $ ls
blog manage.py
~/tutorial $ ls blog
__init__.py settings.py urls.py wsgi.py
참고. 터미널 / 쉘 실행은 HeXA 블루
38. 기본 구조
일단 프로젝트를 만들자
blog/
├── blog
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py 프로젝트 관리 도구
각종 설정이 들어있는 파일
URL을 연결해 주는 파일
웹 서버 게이트웨이 인터페이스
39. 기본 구조
일단 프로젝트를 만들자
~/blog $ python manage.py runserver 0.0.0.0:port
port에는 9000 ~ 1000 사이 아무거나 골라 쓰세요.
혹시 누군가와 겹친다면... 두근두근? .
41. 기본 구조
프로젝트를 만들었으니, 이젠 App을 만들 차례!
하나의 Project, 여러 개의 App
blog
post comment account …..
42. 기본 구조
일단 프로젝트를 만들자
~/blog $ django-admin.py startapp helloworld
~/blog $ ls
helloworld manage.py tutorial
~/blog $ ls helloworld
__init__.py admin.py models.py tests.py views.py
43. 기본 구조
일단 프로젝트를 만들자
~/blog $ cd blog
~/blog/blog $ vi settings.py
(...)
INSTALLED_APPS = (
(...)
'django.contrib.messages',
'helloworld',
)
(...)
Project 한테 helloworld App을 추가 시키겠다고 말하기!
settings.py
44. 기본 구조
일단 프로젝트를 만들자
~/blog $ cd ../helloworld
~/blog/helloworld $ vi views.py
from django.http import HttpResponse
# Create your views here.
def hello(request):
return HttpResponse("Hello, World!")
첫 번째 views.py 완성!
views.py
45. 기본 구조
일단 프로젝트를 만들자
~/blog/helloworld $ cd ../
~/blog $ vi blog/urls.py
(...)
urlpatterns = patterns('',
url(r'^helloworld/', 'helloworld.views.hello'),
(...)
urls.py 도 완성!
urls.py
ps. ^ 는주소앞부분의끝을의미→ hexa.iptime.org:9000/helloworld/
바로여기
46. 기본 구조
일단 프로젝트를 만들자
~/blog $ python manage.py runserver 0.0.0.0:port
47. 기본 구조
일단 프로젝트를 만들자
~/blog $ python manage.py runserver 0.0.0.0:port
빠밤!
50. 기본 구조
urls.py
urlpatterns = patterns('',
url(r'^helloworld/', 'helloworld.views.index'),
url(r'^register/', 'account.views.register'),
url(r'^login/', 'account.views.login'),
url(r'^write/', 'post.views.write_post'),
)
blog
post comment account …..
urls.py
51. 기본 구조
urls.py
urlpatterns = patterns('',
url(r'^helloworld/', 'helloworld.views.index'),
url(r'^register/', 'account.views.register'),
url(r'^login/', 'account.views.login'),
url(r'^write/', 'post.views.write_post'),
)
hexa.iptime.org:9000/register/ 는 account App의 register()로
hexa.iptime.org:9000/login/ 은 account App의 login()로
hexa.iptime.org:9000/write/ 는 post App의 write_post()로 가세요!
blog
post comment account …..
urls.py
52. 기본 구조
urls.py
hexa.iptime.org:9000/register/ 는 account App의 register()로
hexa.iptime.org:9000/login/ 은 account App의 login()로
hexa.iptime.org:9000/write/ 는 post App의 write_post()로 가세요!
blog
post comment account …..
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello, World!")
views.py
urlpatterns = patterns('',
url(r'^helloworld/', 'helloworld.views.index'),
url(r'^register/', 'account.views.register'),
url(r'^login/', 'account.views.login'),
url(r'^write/', 'post.views.write_post'),
)
urls.py
54. 기본 구조
urls.py
urlpatterns = patterns('',
url(r'^post/', include(‘post.urls')),
url(r'^account/', include('account.urls')),
)
urls.py
hexa.iptime.org:9000/post/ 는 post App에 있는 urls.py를 찾아 보시고요
hexa.iptime.org:9000/account/ 은 account App의 urls.py를 찾아 보세요!
helloworld/
├── __init__.py
├── admin.py
├── models.py
├── tests.py
├── urls.py
└── views.py
보통 App의 생김새
65. 기본 구조
코딩 타임
~/blog$ vi blog/urls.py
(...)
urlpatterns = patterns('',
url(r'^helloworld/', include('helloworld.urls')),
(...)
urls.py
ps. ^ 는주소앞부분의끝을의미→ hexa.iptime.org:9000/helloworld/
바로여기
66. 기본 구조
코딩 타임
~/blog$ cp blog/urls.py helloworld/urls.py
~/blog$ vi helloworld/urls.py
(...)
urlpatterns = patterns('',
url(r'^$', 'helloworld.views.hello'),
(...)
urls.py
ps. $ 는문장의끝을의미→ hexa.iptime.org:9000/helloworld/
바로여기
70. 기본 구조
Dynamic URL
~/blog$ vi helloworld/urls.py
(...)
urlpatterns = patterns('',
url(r'^$', 'helloworld.views.hello'),
url(r'^([w]+)/', 'helloworld.views.hello'),
(...)
urls.py
ps. w 는모든단어를의미→ /helloworld/123/ or /helloworld/HeXA/
ps2. [w] 는 모든 단어 중 하나를 의미
ps3. [w]+ 는 [w]+가 하나 이상 있다는 의미
ps4. ([w]+) 는 하나의 value 로 취급
71. 기본 구조
~/blog$ vi helloworld/views.py
from django.http import HttpResponse
# Create your views here.
def hello(request, message = "HeXA"):
return HttpResponse("Hello, World! Your message is " + message)
views.py
Dynamic URL
message 가 아무것도 없을 때 자동으로 HeXA가 됨
80. 나는 (UNIST) (컴공)의 (칼페디엠)이다!
나이는 (23)살, 여자친구는 (없)다!
야 (신난)다!
...
이런 게 대충 한 (300)줄쯤 된다고
생각해보자! 야 (신난)다!
이쯤 되면 (항상 똑같은 부분)은
(분리)하는게 (좋)다는 걸 다들 느꼈겠지?
기본 구조
이제는 template를 알아보자
81. 나는 (UNIST) (컴공)의 (턴즈)이다!
나이는 (22)살, 여자친구는 (있)다!
야 (행복하)다!
...
이런 게 대충 한 (999)줄쯤 된다고
생각해보자! 야 (싫)다!
이쯤 되면 (항상 똑같은 부분)은
(분리)하는게 (좋)다는 걸 다들 느꼈겠지?
기본 구조
이제는 template를 알아보자
82. 나는 (UNIST) (컴공)의 (턴즈)이다!
나이는 (22)살, 여자친구는 (있을리가 없)다!
야 (행복하)다!
...
이런 게 대충 한 (999)줄쯤 된다고
생각해보자! 야 (싫)다!
이쯤 되면 (항상 똑같은 부분)은
(분리)하는게 (좋)다는 걸 다들 느꼈겠지?
기본 구조
이제는 template를 알아보자
83. 기본 구조
반복되는 구조를 template로 만듦
tempate == 틀
이제는 template를 알아보자
84. 기본 구조
그럼 한번 적용해 보자!
즐겁게 틀을 만들어 봅시다
이제는 template를 알아보자
85. 기본 구조
~/blog$ mkdir templates
~/blog$ vi templates/index.html
안녕하세요. 저는 {{ first }} 이고, {{ second}} 의 {{ third }} 입니다.
index.html
이제는 template를 알아보자
89. 정답
틀리면 때릴거다
기본 구조
이제는 template를 알아보자
2. 추가한 template이 사용될 URL을 정해야 하고1. 방금 추가한 template를
장고가 찾을 수 있게 알려줘야 하고
90. 정답
틀리면 때릴거다
기본 구조
이제는 template를 알아보자
2. 추가한 template이 사용될 URL을 정해야 하고
3. 그 URL에 대한 View를 정애야 함
1. 방금 추가한 template를
장고가 찾을 수 있게 알려줘야 하고
91. 기본 구조
~/blog$ vi blog/settings.py
(...)
TEMPLATE_DIRS = (
'./templates',
)
settings.py
이제는 template를 알아보자
Django 에게 아까 만든 tempates 디렉토리에서 tempate를 찾으라고 알려준다
1. 추가한 template를 장고가 찾을 수 있게 알려줘야 하고
92. 기본 구조
~/blog$ vi helloworld/urls.py
(...)
urlpatterns = patterns('',
url(r'^me/([w]+)/([w]+)/([w]+)/', 'helloworld.views.me'),
(...)
settings.py
이제는 template를 알아보자
name group role
2. 추가한 template이 사용될 URL을 정해야 하고
93. 기본 구조
~/blog$ vi helloworld/views.py
from django.http import HttpResponse
from django.template.loader
import get_template from django.template
import Context
def hello(request, message= 'HeXA'):
return HttpResponse('Hello, World! Your number is ' + message)
def me(request, name, group, role):
t = get_template('index.html')
context = Context({'first': name,
'second': group,
'third': role,})
html = t.render(context)
return HttpResponse(html, mimetype = 'text/html;charset=UTF-8')
index.html
이제는 template를 알아보자
보통 가독성을 위해서 이렇게 이쁘게 쓴다
3. URL에 대한 View를 정애야 함
94. 정답
틀리면 때릴거다
기본 구조
이제는 template를 알아보자
2. 추가한 template이 사용될 URL을 정해야 하고
3. 그 URL에 대한 View를 정애야 함
1. 방금 추가한 template를
장고가 찾을 수 있게 알려줘야 하고
122. 필수 잡담 2. Python 핵심 가이드
넘어가면 너 손해야... 난 몰라 (2)
123. Python 철학
못생긴 것 보다 아름다운 것이 더 낫다.
함축적인 것 보다 분명한 것이 더 낫다.
복잡한 것 보다 간단한 것이 더 낫다.
중첩된 것 보다 일렬로 있는게 더 낫다.
빡빡한 것 보다 널널한게 더 낫다.
가독성이 중요하다.
...
모든 프로그래밍 언어에는 각자의 철학이 있다
by Tim Peters
Python 핵심 가이드
124. Python 철학
빠르게 짜고, 빠르게 확인하고, 빠르게 고친다
코딩은 즐거워야 한다
Quick & Fun
모든 프로그래밍 언어에는 각자의 철학이 있다
by carpedm20
Python 핵심 가이드
127. (blog)carpedm30@HeXA:~$ python
>>> 3 + 4 * (5 - 6)
-1
>>> 8 / 5
1
>>> 8.0 / 5
1.6
>>> 8 % 5
3
>>> 'carpedm20' * 4
'carpedm20carpedm20carpedm20carpedm20'
>>> 'carpedm20' + "carpedm20"
'carpedm20carpedm20'
코드
결과값
Python 핵심 가이드
인터프린터 : 프로그래밍 언어의 소스 코드를 바로 실행하는 컴퓨터 프로그램 또는 환경
잡다한 계산
128. >>> grade = 4
>>> if grade < 3.3:
... if grade < 2.7:
... print "장짤"
... else:
... print "반장"
... else:
... print "완장"
...
완장
>>>
Python 핵심 가이드
조건문 if
129. >>> sum = 0
>>> for i in range(10):
... sum += I
...
>>> print sum
45
>>> print range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
Python 핵심 가이드
반복문 for
↔ for (int i = 0; i < 10; i++)
131. >>> a=(1,2,3)
>>> a[0]+a[1]+a[2]
6
>>> a[1]=5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> a
(1, 2, 3)
>>>
Python 핵심 가이드
Tuple
List 와 달리 = 연산자로 수정이 불가능!
132. >>> oldbie = [
... ('김태훈', 2011, 23),
... ('한충우', 2011, 22),
... ('김도경', 2013, 23),
... ]
>>> for person in oldbie:
... name, year, old = person
... print name, year, '학번', old, '살'
...
김태훈 2011 학번 23 살
한충우 2011 학번 22 살
김도경 2013 학번 23 살
>>>
Python 핵심 가이드
List & Tuple
이걸 c++로 짠다면?!
133. >>> def isOld(old):
... if old == 22:
... return True
... else:
... return False
...
>>> for person in oldbie:
... name, _, old = person
... if isOld(old):
... print name + ' is old‘
... else:
... print name + ' is young‘
...
김태훈 is young
한충우 is old
도경 is young
Python 핵심 가이드
Function
C++ 과 달리 return 타입을 지정해 주지 않아도 됨
↔ Skip 하고 싶은 변수는 _ 로