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

파이썬 Django 주석(Comments)

by flycoding 2024. 3. 8.
반응형

파이썬 Django 주석(Comments)

주석을 사용하면 무시해야 할 코드 섹션을 가질 수 있다.

template.html

<!DOCTYPE html>
<html>
<body>

<h1>Welcome Everyone</h1>
{% comment %}
  <h1>Welcome ladies and gentlemen</h1>
{% endcomment %}

</body>
</html>
views.py

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

def testing(request):
  template = loader.get_template('template.html')
  return HttpResponse(template.render())

{% comment %} ~ {% endcomment %} 사이의 <h1> 태그의 'Welcom ladies and gentlemen' 문구를 화면에 표시되지 않는다. 위의 코드를 실행하면 아래 그림과 같다.

파이썬 Django 주석 comments 활용 예제

 

파이썬 Django 주석 설명(Comment Description)

댓글에 메시지를 추가하여 댓글을 작성한 이유를 기억하거나 코드를 읽는 다른 사람에게 메시지로 사용할 수 있다.

 

설명을 주석에 추가한다:

template.html

<!DOCTYPE html>
<html>
<body>

<h1>Welcome Everyone</h1>
{% comment "this was the original welcome message" %}
    <h1>Welcome ladies and gentlemen</h1>
{% endcomment %}

</body>
</html>
views.py

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

def testing(request):
  template = loader.get_template('template.html')
  return HttpResponse(template.render())

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

파이썬 Django 주석 comments 활용 예제

 

파이썬 Django 짧은 주석

또한 코드를 주석으로 달 때 {#...#} 태그를 사용할 수도 있으며, 이 태그는 더 작은 주석에 대해 더 쉽게 사용할 수 있다:

 

Everyone 단어에 주석을 달아보자:

template.html

<!DOCTYPE html>
<html>
<body>

<h1>Welcome{# Everyone#}</h1>

</body>
</html>
views.py

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

def testing(request):
  template = loader.get_template('template.html')
  return HttpResponse(template.render())  

{# Everyone #} 구문은 짧은 주석으로 코드 실행에 반영이 되지 않는다.

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

파이썬 Django 짧은 주석 comments {# #} 활용 예제

 

파이썬 Django Views에서 주석

뷰는 Python으로 작성되며 Python 코멘트는 # 문자로 작성된다:

template.html

<!DOCTYPE html>
<html>
<body>

<h1>Welcome Everyone</h1>

</body>
views.py

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

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

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

파이썬 Django views,.py comment 주석 #&nbsp; 활용 예제

 

이번 글에서는 파이썬 Django 주석(comments)에 대해서 실습하며 살펴보았다.

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

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

 

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

반응형

'파이썬 > 파이썬기본문법' 카테고리의 다른 글

파이썬 Django QuerySet  (0) 2024.03.10
파이썬 Django include 태그  (0) 2024.03.09
파이썬 Django for loop variables  (0) 2024.03.07
파이썬 Django for loop  (0) 2024.03.06
파이썬 Django Operator-1  (0) 2024.03.05

댓글