Python(15)
-
[Python 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 ..
2022.04.11 -
[Python Django] Template에서 파일명에 따라 ACTIVE 처리하기, resolver_match
Django Template를 이용하여 메뉴를 구성하고자 할때, 현재 페이지의 URL을 확인해야할때가 있습니다. 특히 LNB나 GNB 메뉴를 구성할 때 사용되는 경우가 많습니다. view 파일에서 context에 현재 페이지에 대한 정보를 주는 방법도 있겠으나, 현재 페이지의 url_name을 가져와 판단하는 것이 가장 효과적이라고 볼 수 있겠습니다. urls.py 세팅 url 패턴의 path에 name을 설정해줍니다. # urls.py urlpatterns = [ path('manage/', ManageView.as_view(), name='manage'), path('create/', BlogCreateView.as_view(), name='create'), path('update/', BlogUpd..
2022.04.06 -
[Python Django] is_authenticated, is_anonymous, is_active
django가 지원하는 사용자 인증 기능을 이용하여 로그인 여부를 판단 is_authenticated 로그인이 되어있다면 True를 아니라면 False를 반환한다 # python 파일 내에서의 사용 if request.user.is_authenicated: print("로그인 되었습니다") else: print("로그인 되지 않았습니다.") {% if user.is_authenticated %} 로그인 되었습니다 {% else %} 로그인 되지 않았습니다. {% endif %} is_anonymouse 로그인되어있다면 False를 아니라면 True를 반환한다 # python 파일 내에서의 사용 if request.user.is_anonymouse: print("로그인 되지 않았습니다.") else: pr..
2022.04.01 -
[Python Django] 4. Decorator 이용하기
decorator 생성 # user/decotators.py from django.shortcuts import redirect def login_required(function): def wrap(request, *args, **kwargs): user_id = request.session.get('user_id') if user_id is None or not user_id: return redirect("/user/login/") return function(request, *args, **kwargs) return wrap Function Base View (FBV)에 적용 # user/views.py from user.decotators import login_required @login_req..
2022.03.26 -
[Python] 문자열 관련 함수
capitalize() 첫글자를 대문자로 만들어준다 v_str = "niceman nice" print("Capitalize: ", v_str.capitalize()) # >>> Capitalize: Niceman nice endswitch({문자}) 마지막 문자를 비교하여 bool값을 리턴한다 v_str = "Orange" print("endswith?: ", v_str.endswith("s")) # >>> False print("endswith?: ", v_str.endswith("ge")) # >>> True join([{앞},{뒤}]) 문자 앞 뒤에 값을 입력한다. v_str = "Niceman" print("join str: ", v_str.join(["I'm ", "!"])) # >> join..
2022.03.14 -
[Python] 문자열 관련 내용 정리
문자열 생성 # 문자열 생성 str1 = "I am Boy." str2 = 'NiceMan' str3 = """How are you?""" str4 = '''Thank you!''' 타입 출력 str1 = "a" print(type(str1)) # 결과 문자열 길이 str1 = "a" print(len(str1)) # 결과 1 빈 문자열 생성 # 빈 문자열 str_t1 = '' str_t2 = str() 이스케이프 문자 사용 # 이스케이프 문자 사용 escape_str1 = "Do you have a \"big collection\"?" escape_str2 = 'What\'s on TV?' escape_str3 = "What's on TV?" escape_str4 = 'This is a "book".'..
2022.03.14