본문 바로가기

Python and Django13

Django File upload Django에서 CSV 파일을 서버에 업로드를 할 때 이용한 방법이다. view에서 input type='file'의 name을 이용해서 파일 정보를 받는다. Url에서 /uploadcsvfile의 주소는 view의 uploadCSVfile 함수이다. 참고사이트: http://hanjiq.egloos.com/2373084 JQuery Form Plugin: http://jquery.malsup.com/form/ Plugin download site: http://jquery.malsup.com/form/#download 1. Html {% csrf_token %} 올리기 2. JavaScript 3. View.py def uploadCSVfile(request): response = {} if reque.. 2013. 5. 28.
PHP's explode() 함수를 Python으로 처리할 경우 string을 분할 하는 함수를 사용하면 된다. import string string.split(the_string, the_separator) 예를 들어 보면 아래와 같다. >>>print "this is a test string!".split(' ') ['this', 'is', 'a', 'test', 'string!'] >>>print "this is a test string!".split(' ', 2) ['this', 'is', 'a test string!'] 2013. 1. 7.
Apache와 Django 연결 1. Apache를 다운 받아 설치 한다. http://www.apache.org2. Python, Django를 다운 받아 설치 한다. http://www.python.org/download/ python27폴더를 속성(내컴퓨터) -> 고급 -> 환경변수 -> path에 추가 https://www.djangoproject.com/download/ command line에서 django 압축해제한 디렉토리로 이동 c:\>cd c:\Django-1.4 c:\Django-1.4>python setup.py install Django-1.4\django\bin 디렉토리에 있는 django-admin.py를 c:\windows나 파이썬이 설치된 디렉토리에 복사한다. 3. Django 프로젝트를 생성한다. com.. 2012. 11. 29.
Python 이차원배열 생성 방법 가로, 세로 크기 만큼의 2차원 배열을 만들 경우 아래와 같이 테스트를 할 수 있다. #create width x height matrix matrix = [[0 for col in range(width)] for row in range(height)] for row in range(height): for col in range(width): matrix[row][col] = 0 if col == 0 and row == 0: matrix[row][col] = 1 if col == width-1 and row == height-1: matrix[row][col] = 20 for row in matrix: print row 만약 가로 크기 3, 세로 5의 크기를 가지고 있을 경우 아래와 같은 결과가 출력된다.. 2012. 10. 18.
PIL을 이용한 그리기 part. 1 Example import Image, ImageDraw im = Image.open("test.png") draw = ImageDraw.Draw(im) draw.line((0, 0) + im.size, fill = 128) draw.line((0, im.size[1], im.size[0], 0), fill = 128) del draw im.save(sys.stdout, "PNG") - Imge.new("1", size) 1 bit 이미지를 만들면 0으로 채워진다. bmp로 저장하면 검은색으로 채워져 있다. - polygon으로 그린다면 아래와 같이 1로 채운다. draw.polygon(xy, fill = 1) 1로 채우고 bmp로 저장하면 polygon 부분이 흰색으로 채워져 있다. xy는 list로 .. 2012. 9. 6.
PIL을 이용한 이미지 편집 part.1 1. open - Image.open(infile) - Image.open(infile, mode) //특정 mode로 이미지를 열수 있음. ex) Image.open("test.jpg", "RGB") Image.open("test.jpg", "RGBA") mode: 1(1-bit), L(8-bit / B&W), P(8-bit / mapped to any other palette), RGB, RGBA, CMYK, YCbCr, I, F 2. new - Image.new(mode, size) - Image.new(mode, size, color) ex) from PIL import Image im = Image.open("test.png") size = (180, 180) im2 = Image.new("RG.. 2012. 9. 5.
파이썬 스레드 프로그래밍 참고:파이썬을 사용한 실전 스레드 프로그래밍 http://www.ibm.com/developerworks/kr/library/au-threadingpython/index.html 1. hello thread example import threading import datetime class ThreadClass(threading.Thread): def run(self): now = datetime.datetime.now() print "%s says Hello World at time: %s" % \ (self.getName(), now) for i in range(2): t = ThreadClass() t.start() Thread 이용은 참고 사이트를 이용해서 봐야 할 것 같다. 2012. 5. 30.
NON-ASCII character `\xec` in file 에러 발생 해결책 한글을 파이썬 소스에서 사용하기 위해서는 맨 위에 아래와 같이 입력을 해야 한다. #-*- coding: utf-8 -*- 인코딩 문제로 자주 잊는 부분이다. 꼭 한글을 사용할 경우에는 넣길~ 2012. 5. 17.
CSRF verification failed. Request aborted. 장고책을 보고 샘플 코드를 이용해서 만들어 테스트 하다가 에러가 발생했다."로그인(Login)" 부분을 처리 하다가 발생하는 것으로 세션처리와 관계가 있는 것 같다. Forbidden (403)CSRF verification failed. Request aborted. 이 부분은 장고에서 보안처리를 하는것 때문에 문제가 발생한다고 한다. 해결 방법은 post form 뒤에 {% csrf_token %}를 넣어주면 된다. 예를들어 내가 만든 샘플 소스에 넣는다면 아래와 같이 넣어주면 된다. {% csrf_token %} 참고 사이트:https://docs.djangoproject.com/en/1.4/ref/contrib/comments/ 2012. 5. 16.
LIST