본문 바로가기

Django

(13)
Python / 삼항연산자 사용방법 (Ternary Operator) 그동안 사용했던, 삼항연산자의 기초 문법과 달라서 당황했던 파이썬 문법 기본적으로 대부분의 삼항연산자의 경우 if문을 편리하게 사용하기 위해 쓰는데 아래와 같은 공식을 사용한다. [Condition] ? [True] : [False] Condition이 참이라면 True의 값 반환, False 라면 False 값 반환. (True, False 자리에 반환할 값을 적으면 된다) 당연히, Python도 될줄 알았는데. python 문법은 달랐다. [True] if [Condition] else [False]
django / template에서 1000단위(천단위) comma 표시 php를 쓰다가 django로 넘어오다보니 구글링이 더 늘었다. 1. php, python 포맷 비교 php에서 사용하는 천단위 콤마 포맷 {{ number_format($price) }} {{ number_format(1000) }} // 1,000 python에서 사용하는 천단위 콤마 포맷 print(format(price, ",")) print(format(1000, ",")) # 1,000 2. django에서 효율적으로 사용하기 위한 포맷 Add 1. Settings.py 파일에 'mathfilters' 추가 INSTALLED_APPS = [ . . 'django.contrib.humanize', . . ] 2. 사용할 template에 {% load humanize %} 추가 {% load h..
django / template 연산 사용법 django-mathfilters Install terminal 창에 입력, django-mathfilters를 설치 pip install django-mathfilters Add 1. Settings.py 파일에 'mathfilters' 추가 INSTALLED_APPS = [ . . 'mathfilters', . . ] 2. 사용할 template에 {% load mathfilters %} 추가 {% load mathfilters %} Usage sub – subtraction mul – multiplication div – division intdiv – integer (floor) division abs – absolute value mod – modulo addition – replacement for the add filter ..
django / html(웹페이지)를 pdf로 변환하는 방법 pdfkit Install (terminal 창에 입력, pdfkit을 설치) pip install pdfkit Download (사이트에서 OS 맞는 버전을 설치) https://wkhtmltopdf.org/downloads.html 난 윈도우 로컬에서 돌릴땐 Windows 64-bit 사용 Usage 더보기 pdfkit.from_url : url 주소사용 pdfkit.from_file : html 파일 사용 pdfkit.from_string : 문자 사용 import pdfkit #local config = pdfkit.configuration(wkhtmltopdf='C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe') #server config = pdfkit.configu..
django / template에 html 태그 적용 django 사용 중 html파일을 서버에서 받아서 template에서 보여줘야하는 상황인데 자꾸 태그가 안녕 이런식으로 그대로 노출되는 상황이 발생 해당 문제를 해결하기 위한 방법 template에 html 태그 적용하기 {% autoescape off %} 사용할 내용 넣기 {% endautoescape %} 기타 {% autoescape off %} {{ etc }} {% endautoescape %} 참고사이트 https://docs.djangoproject.com/en/3.2/ref/templates/builtins/