- 종류
- ✨Function views_
- path("사용자요청주소", mapping할 응답객체)path("index", views.indexMap)
- Class-based views_
- path("사용자요청주소", mapping할 클래스.as_view())# 메인 urls.py path("app/callget", views.CallView.as_view()) # app의 views.py class CallView(TemplateView): template_name = "callget.html"
- ✨Including another URLconf
- path("app이름/", include("app이름.urls"))
- app 폴더 안에 urls.py을 생성한다.
- 사용 목적 : 한 프로젝트 내에 여러개의 app이 있을 경우 app마다 url mapping 분기하여 사용한다.path("app/", include("app.urls"))
- 방법
- 같은 요청주소로 요청하지만 방식을 GET, POST로 나누어 같은 블록 안에서 받을 수 있다.# main.html # GET 요청 <a href="test"> # POST 요청 <form action="test" method="post">{% csrf_token %} <button type="submit">제출</button> </form> # views.py def testFunc(request): if request.method == "GET": print("a 태그") # request.GET['데이터명'] elif request.method == "POST": print("form 태그") # request.POST['데이터명']
- CSRF 문제
- POST 방식으로 form 전송 시, CSRF 공격 방지로 인한 블로킹 발생
- CSRF 문제 해결방법
- POST 방식을 사용하는 html 구간에 csrf_token 적용
- {% csrf_token %}<form action="test" method="post">{% csrf_token %} <button type="submit">제출</button> </form>
- 위치
- 프로젝트 폴더 -> settings.py -> MIDDLEWARE 리스트
- 토큰명 : "django.middleware.csrf.CsrfViewMiddleware"
- 위 토큰으로 인해 POST 요청 시, 웹 블로킹 발생
- 세션 값 추가 및 삭제
- 값 추가 : request.session['세션 key값'] = value값
- 특정 세션 삭제 : del request.session['세션 key값']
- 전체 세션 삭제 : request.session.clear()
- 세션 유효시간
- 세션 유효시간 : request.session.get_expiry_age()
- 세션 유효시간 설정 : request.session.set_expiry(sec)
- 세션 특정 Key 확인
- 세션 내부 특정 key 확인 : if '특정key값' in request.session: