본문 바로가기
파이썬/파이썬기본문법

파이썬 django if tag

by flycoding 2024. 3. 4.
반응형

파이썬 django if 문

if 문은 변수를 평가하고 값이 참이면 코드 블록을 실행한다.

templates.html

<!DOCTYPE html>
<html>
<body>

{% if greeting == 1 %}
  <h1>Hello</h1>
{% endif %}

<p>In views.py you can see what the greeting variable looks like.</p>

</body>
</html>          
views.py

from django.http import HttpResponse
from django.template import loader

def testing(request):
  template = loader.get_template('template.html')
  context = {
    'greeting': 1,
  }
  return HttpResponse(template.render(context, request))           

 

위의 코드를 실행하면 아래 그림과 같다.

파이선 django if 문 활용 예제

 

파이썬 django elif 문

elif 키워드는 "이전 조건이 사실이 아니었으면 이 조건을 시도하십시오"라고 말한다.

templates.html

<!DOCTYPE html>
<html>
<body>

{% if greeting == 1 %}
  <h1>Hello</h1>
{% elif greeting == 2 %}
  <h1>Welcome</h1>
{% endif %} 

<p>In views.py you can see what the greeting variable looks like.</p>

</body>
</html>          
views.py

from django.http import HttpResponse
from django.template import loader

def testing(request):
  template = loader.get_template('template.html')
  context = {
    'greeting': 2,
  }
  return HttpResponse(template.render(context, request))           

위의 코드를 실행하면 greeting값이 2이기 때문에 'Welcome'가 화면에 h1태그로 출력이 된다.

파이선 django elif 문 활용 예제

 

파이썬 django else 문

else 키워드는 앞의 조건에 의해 잡히지 않는 것을 잡는다.

templates.html

<!DOCTYPE html>
<html>
<body>

{% if greeting == 1 %}
  <h1>Hello</h1>
{% elif greeting == 2 %}
  <h1>Welcome</h1>
{% else %}
  <h1>Goodbye</h1>
{% endif %} } 

<p>In views.py you can see what the greeting variable looks like.</p>

</body>
</html>            
views.py

from django.http import HttpResponse
from django.template import loader

def testing(request):
  template = loader.get_template('template.html')
  context = {
    'greeting': 3,
  }
  return HttpResponse(template.render(context, request))           

위의 코드를 실행하면 greeting이 3이기 때문에 else문이 실행이 되어서 Goodbye가 화면에 출력이 된다.

파이선 django else문 활용 예제

 

파이썬 Django 연산자(Operators)

위의 예제에서는 == 연산자를 사용하여 변수가 값과 동일한지 확인할 수 있지만 사용할 수 있는 다른 연산자가 많거나 변수가 비어 있지 않은지 확인하려면 연산자를 삭제할 수도 있다:

template.html

<!DOCTYPE html>
<html>
<body>

{% if greeting %}
  <h1>Hello</h1>
{% endif %} 

<p>In views.py you can see what the greeting variable looks like.</p>

</body>
</html>      
views.py

from django.http import HttpResponse
from django.template import loader

def testing(request):
  template = loader.get_template('template.html')
  context = {
    'greeting': 2,
  }
  return HttpResponse(template.render(context, request))   

위의 코드를 실행하면 greeting이 2이면 참값이기 때문에 'Hello' h1태그로 화면에 그림과 같이 출력이 된다.

파이선 django operator 연산자 활용 예제

 

파이썬 Django 연산자(Operators) : ==

동등하다. 같은지 판단하는 연산자이다.

template.html

<!DOCTYPE html>
<html>
<body>

{% if greeting == 2 %}
  <h1>Hello</h1>
{% endif %}  

<p>In views.py you can see what the greeting variable looks like.</p>

</body>
</html>     
views.py

from django.http import HttpResponse
from django.template import loader

def testing(request):
  template = loader.get_template('template.html')
  context = {
    'greeting': 2,
  }
  return HttpResponse(template.render(context, request))   

greeting이 2와 같기 때문에 h1태그에 Hello 문구를 화면에 아래 그림과 같이 출력한다.

파이선 django operator 연산자 == 같다 활용 예제

 

파이썬 Django 연산자(Operators) : !=

같지 않다, 다르다 연산자이다.

template.html

<!DOCTYPE html>
<html>
<body>

{% if greeting 1= 1 %}
  <h1>Hello</h1>
{% endif %}  

<p>In views.py you can see what the greeting variable looks like.</p>

</body>
</html>     
views.py

from django.http import HttpResponse
from django.template import loader

def testing(request):
  template = loader.get_template('template.html')
  context = {
    'greeting': 2,
  }
  return HttpResponse(template.render(context, request))   

greeting이 1과  같지 않기 때문에 h1태그에 Hello 문구를 화면에 아래 그림과 같이 출력한다.

파이선 django operator 연산자 != 다르다 활용 예제

 

파이썬 Django 연산자(Operators) : <

작다.

template.html

<!DOCTYPE html>
<html>
<body>

{% if greeting < 3 %}
  <h1>Hello</h1>
{% endif %}  

<p>In views.py you can see what the greeting variable looks like.</p>

</body>
</html>     
views.py

from django.http import HttpResponse
from django.template import loader

def testing(request):
  template = loader.get_template('template.html')
  context = {
    'greeting': 2,
  }
  return HttpResponse(template.render(context, request))   

greeting이 3보다 적기 않기 때문에 h1태그에 Hello 문구를 화면에 아래 그림과 같이 출력한다.

파이선 django operator 연산자 < 작다 활용 예제

 

파이썬 Django 연산자(Operators) : <= 이하

이하, 작거나 같다.

template.html

<!DOCTYPE html>
<html>
<body>

{% if greeting <= 3 %}
  <h1>Hello</h1>
{% endif %}  

<p>In views.py you can see what the greeting variable looks like.</p>

</body>
</html>     
views.py

from django.http import HttpResponse
from django.template import loader

def testing(request):
  template = loader.get_template('template.html')
  context = {
    'greeting': 2,
  }
  return HttpResponse(template.render(context, request))   

greeting이 3보다 이하이기 때문에 h1태그에 Hello 문구를 화면에 아래 그림과 같이 출력한다.

파이선 django operator 연산자 <= 이하 활용 예제

 

파이썬 Django 연산자(Operators) : > 

크다

template.html

<!DOCTYPE html>
<html>
<body>

{% if greeting > 1 %}
  <h1>Hello</h1>
{% endif %}  

<p>In views.py you can see what the greeting variable looks like.</p>

</body>
</html>     
views.py

from django.http import HttpResponse
from django.template import loader

def testing(request):
  template = loader.get_template('template.html')
  context = {
    'greeting': 2,
  }
  return HttpResponse(template.render(context, request))   

greeting이 1보다 크기 때문에 h1태그에 Hello 문구를 화면에 아래 그림과 같이 출력한다.

파이선 django operator 연산자 > 크다 활용 예제

 

파이썬 Django 연산자(Operators) : >= 

이상, 크거나 같다

template.html

<!DOCTYPE html>
<html>
<body>

{% if greeting >= 1 %}
  <h1>Hello</h1>
{% endif %}  

<p>In views.py you can see what the greeting variable looks like.</p>

</body>
</html>     
views.py

from django.http import HttpResponse
from django.template import loader

def testing(request):
  template = loader.get_template('template.html')
  context = {
    'greeting': 2,
  }
  return HttpResponse(template.render(context, request))   

greeting이 1보다 크기 때문에 h1태그에 Hello 문구를 화면에 아래 그림과 같이 출력한다.

파이선 django operator 연산자 >= 이상 활용 예제

 

이번 글에서는 파이썬 Django if, elif, else 문과 비교연산자 같다, 다르다, 작다, 작거나 같다, 크다, 크거나 같다 등의 연산자를 실습하며 살펴보았다.

꼭 손으로 눈으로 머리로 익히며 실습하기를 바란다.

모두 화이팅입니다.!!!

 

출처 : 이 글의 출처는 w3schools사이트를 참고하였으며 필자가 추가하여 정리한 글입니다.

반응형

댓글