def index(request):
context = {
'name': 'Jane',
}
return render(request, 'articles/index.html', context)<body>
<h1>Hello, {{ name }}!</h1>
</body>.)을 사용하여 변수 속성에 접근할 수 있음context = {
'variable1': 'value1', # {{ variable1 }}
'variable2': {
'attribute': 'value2', # {{ variable2.attribute }}
},
}| + 필터){% if login %}
‹h1>Hello, User!!!</h1>
{% else %}
<h1>Please, login.‹/h1>
{% endif %}<ul>
{% for num in nums %}
<li>{{ num }}</li>
{% endfor %}
</ul>{# name #}{% comment %} {% endcomment %}⇒ 여러 템플릿이 공통요소를 공유할 수 있게 해주는 기능
<!-- articles/base.html -->
<!DOCTYPE html>
<html lang="en">
<head>
...
</head>
<body>
{% block content %}
{% endblock content %}
</body>
</html><!-- articles/index.html -->
{% extends 'articles/base.html' %}
{% block content %}
<h1>Hello, {{ name }}</h1>
{% endblock content %}extends tag{% extends 'articles/base.html' %}block tag{% block content %} {% endblock content %}form element를 통해 사용자와 애플리케이션 간의 상호작용 이해하기<form action="#" method="GET">
<div>
<label for="name">아이디 : </label>
<input type="text" name="name" id="name">
</div>
<div>
<label for="password">패스워드 : </label>
<input type="password" name="password" id="password">
</div>
<input type="submit" value="로그인">
</form>form element<!-- articles/search.html -->
{% extends 'articles/base.html' %}
{% block content %}
<form action="https://search.naver.com/search.naver" method="GET">
<label for="message">검색어</label>
<input type="text" name="query" id="message">
<input type="submit" value="submit">
</form>
{% endblock content %}input elementnamename attribute<input type="text" name="query" id="message">&)로 연결된 key=value 쌍으로 구성되며, 기본 URL과는 물음표(?)로 구분됨http://host:port/path?key=value&key=value<form action="/search" method="get">
<input type="text" name="query">
<input type="text" name="page">
<button type="submit">검색</button>
</form>/search?query=chatgpt&page=2
app_name = 'articles'
urlpatterns = [
path('throw/', views.throw, name='throw'),def throw(request):
return render(request, 'articles/throw.html'){% extends "articles/base.html" %}
{% block content %}
<h1>Throw</h1>
<form action="{% url "articles:catch" %}" method="GET">
<input type="text" id="message" name="message">
<input type="submit">
</form>
{% endblock content %}app_name = 'articles'
urlpatterns = [
path('catch/', views.catch, name='catch'),def catch(request):
message = request.GET.get('message')
context = {
'message': message,
}
return render(request, 'articles/catch.html', context){% extends "articles/base.html" %}
{% block content %}
<h1>Catch</h1>
<h2>{{ message }}를 잘 받았습니다.</h2>
{% endblock content %}http://127.0.0.1:8000/throw/를 입력하면 발생하는 일
