데이터 표현을 제어하면서, 표현과 관련된 부분을 담당
Ex) HTML 콘텐츠를 변수 값으로 변경
{% comment %} index.html {% endcomment %}
<body>
<h1>Hello, django!</h1>
</body>
# views.py
def index(request):
context = {
'name': 'Jane'
}
return render(request, 'articles/index.html', context)
{% comment %} index.html {% endcomment %}
<body>
<h1>Hello, {{name}}</h1>
</body>
{{variable}}
{{variable.attribute}} {{ variable|filter }}
{{ name|truncatewords:30 }} {% tag %}
{% if %} {% endif %}Ex)

<!-- dinner.html -->
{% extends "articles/base.html" %}
{% block content %}
<p>{{ picked }} 메뉴는 {{ picked|length }}글자입니다.</p>
<h2>메뉴판</h2>
<ul>
{% for food in foods %}
<li>{{ food }}</li>
{% endfor %}
</ul>
{% if foods|length == 0 %}
<p>메뉴가 소진되었습니다.</p>
{% else %}
<p>아직 메뉴가 남았습니다.</p>
{% endif %}
{% endblock content %}
# views.py
def dinner(request):
foods = [
'국밥',
'국수',
'카레',
'탕수육',
]
picked = random.choice(foods)
context = {
'foods': foods,
'picked': picked,
}
return render(request, 'articles/dinner.html', context)
urlpatterns = [
path('admin/', admin.site.urls),
path('dinner/', views.dinner),
]
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
{% block style %}
{% endblock style %}
</head>
<body>
{% block content %}
{% endblock content %}
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>
form elementaction & methodactionmethodinput elementname attribute사용자의 입력 데이터를 URL 주소에 파라미터를 통해 서버로 보내는 방법
문자열은 앰퍼샌드('&')로 연결된 key=value 쌍으로 구성되며, 기본 URL과는 물음표('?')로 구분됨
https://host:port/path?key=value&key=value
# urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('throw/', views.throw),
]
# views.py
def throw(request):
return render(request, 'articles/throw.html')
<!-- throw.html -->
{% extends "articles/base.html" %}
{% block content %}
<h1>Throw</h1>
<form action="/catch/" method="GET">
<input type="text" name="message">
<input type="submit">
</form>
{% endblock content %}
# urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('catch/', views.catch),
]
# views.py
def catch(request):
print(request) # <WSGIRequest: GET '/catch/?message=hello'>
print(type(request)) # <class 'django.core.handlers.wsgi.WSGIRequest'>
print(dir(request)) #
print(request.GET) # <QueryDict: {'message': ['hello']}>
print(request.GET.get('message')) # hello
message = request.GET.get('message')
context = {
'message': message,
}
return render(request, 'articles/catch.html', context)
<!-- catch.html -->
{% extends "articles/base.html" %}
{% block content %}
<h1>Catch</h1>
<h2>{{ message }}를 받았습니다!!!</h2>
{% endblock content %}
PS : 경로 지정시 settings.py에 들어가 TEMPLATES의 DIRS 경로를 지정하면 쉽게 할 수 있다.
#urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('greeting/<str:name>/', views.greeting),
path('articles/<int:num>/', views.detail),
]