Python/Django

DataBase 설정 방법 뷰(views.py) 에서 데이터베이스에 연결하고 하드 코딩하는 방법은 "현명하지 않은" 방법이다.쿼리가 하드 코딩되어 있으면, 만약 MySQL에서SQLite로 변경된 경우 대부분의 코드를 다시 작성해야 하기 때문이다.장고 데이터베이스 계층은 이러한 문제를 해결해 준다.장고의 데이터베이스 모델을 사용하려면 장고 앱(app)을 만들어야 한다. # settings.py # Database # https://docs.djangoproject.com/en/1.11/ref/settings/#databases # Example 1) Default 옵션 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os..
템플릿 로딩 API 1. BACKEND : 템플릿 엔진 클래스 -내장 템플릿 - django templates : django.template.backends.django.DjangoTemplates - jinja2 templates : django.template.backends.jinja2.Jinja2 2. DIRS : 템플릿 소스 디렉토리 리스트 ( 리스트 순서대로 검색한다. ) 'DIRS' : [ os.path.join(BASE_DIR, 'templates') # 프로젝트 루트에 마스터 템플릿 폴더 ] 1)템플릿 폴더 명을 굳이 templates로 명명하지 않아도 된다. 2)윈도우의 경우 경로를 백슬래시 대신 슬래시를 사용한다. 'DIRS' : [ 'C:/Users/templates', ] 3. ..
For 1.일반 For문 1. 일반 For 문 {% for key in list %} {{ key }} {% endfor %} 2.For문 반전 ( Dictionary 불가능 ) 2. 반전된 For 문 {% for key in list reversed %} {{ key }} {% endfor %} 3.이중 리스트 반복 3. 이중 리스트 반복 {% for key, val in matrix %} Key : {{ key }}, Val : {{ val }} {% endfor %} 4.딕셔너리 반복 4. 딕셔너리 반복 {% for key, val in dict.items %} Key : {{ key }}, Val : {{ val }} {% endfor %} 5. 리스트 비어 있는 경우 예외 처리 5. 리스트 비어..
DjangoJSONEncoder Django에서 json.dumps 시 TypeError 에러 발생할 경우 해결 방법 ex) Mysql 데이터 Select 후 datetime.date Type으로 인한 에러 발생. TypeError: datetime.date(2019, 1, 3) is not JSON serializable DjangoJSONEncoder를 사용하면 해결된다. 1) DjangoJSONEncoder 모듈 추가 2) json.dumps안에 cls=DjangoJSONEncoder를 추가해 주면 된다. from django.core.serializers.json import DjangoJSONEncoder json.dumps([dict], cls=DjangoJSONEncoder) # # Exam..
Django 프로젝트, 앱 생성시 아래와 같은 에러가 발생할 경우 File "C:\Python27\Lib\functools.py", line 56, in '__lt__': [('__gt__', lambda self, other: other < self),RuntimeError: maximum recursion depth exceeded while calling a Python object C:\Python27\Lib\functools.py 에 Convert 부분을 다음과 같이 변경해 주면 된다.def total_ordering(cls): """Class decorator that fills in missing ordering methods""" # convert = { # '__lt__': [('__gt..
Windows 10 64 bit 1. 아래 URL 에서 Apache zip 파일 다운 후 압축 풀기vc_redist_x64 파일 실행https://www.apachelounge.com/download/ 2. Apache zip 파일 압축 푼 후 Apache24 폴더만 자신이 원하는 위치로 이동C:\Program Files\Apache24 이동했음. 3. ..\Apache24\conf\httpd.conf 에서 아래 빨간 부분 알맞게 수정 4. 환경변수 설정 시스템 변수 > Path 에 Apache24 폴더 안의 bin 까지의 경로 추가 5. 아파치 설치 : cmd 를 관리자 모드로 실행 후 "httpd -k install" 6. 아파치 실행 : "httpd -k start" 7. httpd.conf 에 입..
S0PH1A
'Python/Django' 카테고리의 글 목록 (6 Page)