Mẫu là một thành phần thiết yếu trong phát triển web toàn diện. Với Jinja, bạn có thể xây dựng các mẫu phong phú cung cấp năng lượng cho giao diện người dùng của các ứng dụng web Python của bạn
Nhưng bạn không cần sử dụng web framework để trải nghiệm khả năng của Jinja. Khi bạn muốn tạo các tệp văn bản có nội dung có lập trình, Jinja có thể giúp bạn
Trong hướng dẫn này, bạn sẽ học cách
- Cài đặt công cụ mẫu Jinja
- Tạo mẫu Jinja đầu tiên của bạn
- Kết xuất mẫu Jinja trong Flask
- Sử dụng
7 vòng lặp và câu điều kiện với Jinja[venv] $ python -m pip list Package Version ---------- ------- Jinja2 3.x ...
- Mẫu Nest Jinja
- Sửa đổi các biến trong Jinja bằng các bộ lọc
- Sử dụng macro để thêm chức năng vào giao diện người dùng của bạn
Bạn sẽ bắt đầu bằng cách tự mình sử dụng Jinja để bao quát những kiến thức cơ bản về tạo khuôn mẫu Jinja. Sau đó, bạn sẽ xây dựng một dự án web Flask cơ bản với hai trang và một thanh điều hướng để tận dụng hết tiềm năng của Jinja
Xuyên suốt hướng dẫn, bạn sẽ xây dựng một ứng dụng mẫu giới thiệu một số tính năng đa dạng của Jinja. Để xem những gì nó sẽ làm, bỏ qua phía trước để
Bạn cũng có thể tìm thấy mã nguồn đầy đủ của dự án web bằng cách nhấp vào liên kết bên dưới
Mã nguồn. Nhấp vào đây để tải xuống mã nguồn mà bạn sẽ sử dụng để khám phá các khả năng của Jinja
Hướng dẫn này dành cho bạn nếu bạn muốn tìm hiểu thêm về ngôn ngữ mẫu Jinja hoặc nếu bạn đang bắt đầu với Flask
Bắt đầu với Jinja
Jinja không chỉ là một thành phố ở Khu vực phía Đông của Uganda và là một ngôi đền Nhật Bản, mà còn là một công cụ tạo mẫu. Bạn thường sử dụng các công cụ mẫu cho các mẫu web nhận nội dung động từ giao diện người dùng và hiển thị nội dung đó dưới dạng trang tĩnh ở giao diện người dùng
Nhưng bạn có thể sử dụng Jinja mà không cần khung web chạy ngầm. Đó chính xác là những gì bạn sẽ làm trong phần này. Cụ thể, bạn sẽ cài đặt Jinja và xây dựng các mẫu đầu tiên của mình
Loại bỏ các quảng cáoCài đặt Jinja
Trước khi khám phá bất kỳ gói mới nào, bạn nên tạo và kích hoạt một môi trường ảo. Bằng cách đó, bạn đang cài đặt bất kỳ phụ thuộc dự án nào trong môi trường ảo của dự án thay vì trên toàn hệ thống
Chọn hệ điều hành của bạn bên dưới và sử dụng lệnh dành riêng cho nền tảng của bạn để thiết lập môi trường ảo
PS> python -m venv venv
PS> .\venv\Scripts\activate
[venv] PS>
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
Với các lệnh trên, bạn tạo và kích hoạt một môi trường ảo có tên
[venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
8 bằng cách sử dụng mô-đun [venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
8 tích hợp sẵn của Python. Dấu ngoặc đơn [>>> import Jinja2
Traceback [most recent call last]:
...
ModuleNotFoundError: No module named 'Jinja2'
>>> import jinja2
>>> # No error
0] xung quanh [venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
8 phía trước lời nhắc cho biết bạn đã kích hoạt thành công môi trường ảoSau khi bạn đã tạo và kích hoạt môi trường ảo của mình, đã đến lúc cài đặt Jinja với
>>> import Jinja2
Traceback [most recent call last]:
...
ModuleNotFoundError: No module named 'Jinja2'
>>> import jinja2
>>> # No error
2________số 8_______
Đừng quên
>>> import Jinja2
Traceback [most recent call last]:
...
ModuleNotFoundError: No module named 'Jinja2'
>>> import jinja2
>>> # No error
3 ở cuối tên gói. Nếu không, bạn sẽ cài đặt phiên bản cũ không tương thích với Python 3Điều đáng chú ý là mặc dù phiên bản chính hiện tại thực sự lớn hơn
>>> import Jinja2
Traceback [most recent call last]:
...
ModuleNotFoundError: No module named 'Jinja2'
>>> import jinja2
>>> # No error
3, gói mà bạn sẽ cài đặt vẫn được gọi là >>> import Jinja2
Traceback [most recent call last]:
...
ModuleNotFoundError: No module named 'Jinja2'
>>> import jinja2
>>> # No error
5. Bạn có thể xác minh rằng bạn đã cài đặt phiên bản Jinja hiện đại bằng cách chạy >>> import Jinja2
Traceback [most recent call last]:
...
ModuleNotFoundError: No module named 'Jinja2'
>>> import jinja2
>>> # No error
6[venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
Để làm cho mọi thứ trở nên khó hiểu hơn, sau khi cài đặt Jinja với chữ hoa
>>> import Jinja2
Traceback [most recent call last]:
...
ModuleNotFoundError: No module named 'Jinja2'
>>> import jinja2
>>> # No error
7, bạn phải nhập nó với chữ thường >>> import Jinja2
Traceback [most recent call last]:
...
ModuleNotFoundError: No module named 'Jinja2'
>>> import jinja2
>>> # No error
8 bằng Python. Hãy dùng thử bằng cách mở trình thông dịch Python tương tác và chạy các lệnh sau>>>
>>> import Jinja2
Traceback [most recent call last]:
...
ModuleNotFoundError: No module named 'Jinja2'
>>> import jinja2
>>> # No error
Khi bạn cố gắng nhập
>>> import Jinja2
Traceback [most recent call last]:
...
ModuleNotFoundError: No module named 'Jinja2'
>>> import jinja2
>>> # No error
5, với cách đặt tên viết hoa mà bạn đã sử dụng để cài đặt Jinja, thì bạn tăng một số >>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
0. Để nhập gói Jinja vào Python, bạn phải nhập >>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
1 với chữ thường >>> import Jinja2
Traceback [most recent call last]:
...
ModuleNotFoundError: No module named 'Jinja2'
>>> import jinja2
>>> # No error
8Kết xuất mẫu Jinja đầu tiên của bạn
Với Jinja được nhập, bạn có thể tiếp tục tải và hiển thị mẫu đầu tiên của mình
>>>
>>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
Thành phần cốt lõi của Jinja là lớp
>>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
3. Trong ví dụ này, bạn tạo một môi trường Jinja không có bất kỳ đối số nào. Sau đó, bạn sẽ thay đổi các tham số của >>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
4 để tùy chỉnh môi trường của mình. Tại đây, bạn đang tạo một môi trường đơn giản nơi bạn tải chuỗi >>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
5 làm mẫuNhững gì bạn vừa làm có vẻ không ấn tượng hơn nhiều so với việc sử dụng một chuỗi được định dạng bằng Python thuần túy. Tuy nhiên, ví dụ này cho thấy hai bước quan trọng mà bạn thường thực hiện khi sử dụng Jinja
- Tải một mẫu. Tải một nguồn có chứa các biến giữ chỗ. Theo mặc định, chúng được đặt trong một cặp dấu ngoặc nhọn [
6]>>> import jinja2 >>> environment = jinja2.Environment[] >>> template = environment.from_string["Hello, {{ name }}!"] >>> template.render[name="World"] 'Hello, World!'
- Kết xuất mẫu. Điền vào chỗ dành sẵn với nội dung. Bạn có thể cung cấp từ điển hoặc ngữ cảnh. Trong trường hợp này, bạn đã điền vào trình giữ chỗ để bạn nhận được ______18_______7 quen thuộc làm đầu ra
Mẫu nguồn mà bạn đang tải có thể là một chuỗi ký tự. Nhưng mọi thứ trở nên thú vị hơn khi bạn làm việc với tệp và cung cấp tệp văn bản dưới dạng mẫu
Loại bỏ các quảng cáoSử dụng tệp bên ngoài làm mẫu
Nếu bạn muốn làm theo các ví dụ trong hướng dẫn này, thì bạn có thể tiếp tục và tạo một thư mục mới để làm việc trong đó. Bên trong thư mục làm việc của bạn, tạo một thư mục có tên
>>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
8Bạn sẽ lưu trữ bất kỳ mẫu sắp tới nào trong thư mục
>>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
8. Bây giờ hãy tạo một tệp văn bản có tên {# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
0{# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
Hãy tưởng tượng rằng bạn là một giáo viên muốn gửi kết quả cho những học sinh có thành tích tốt. Mẫu
{# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
0 chứa bản thiết kế cho tin nhắn mà bạn có thể sao chép và dán để gửi sau. Giống như trong ví dụ về >>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
7, bạn tìm thấy dấu ngoặc nhọn [>>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
6] trong văn bản mẫu của mìnhTiếp theo, tạo một tệp Python có tên
{# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
4# write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
Khi bạn tạo môi trường Jinja với
{# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
5, bạn có thể chuyển đường dẫn trỏ đến thư mục mẫu của bạn. Thay vì chuyển vào một chuỗi, bây giờ bạn tải {# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
0 làm mẫu của mình. Sau khi mẫu của bạn được tải, bạn có thể sử dụng lại nhiều lần để điền vào nội dung. Trong {# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
4, bạn kết xuất {# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
8 và {# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
9 cho từng học sinh giỏi nhất của mình vào một tệp văn bảnLưu ý rằng các khóa của từ điển
# write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
0, cùng với # write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
1 và # write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
2, khớp với các biến mẫu trong {# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
0. Nếu bạn không cung cấp ngữ cảnh cho các biến trong mẫu, chúng sẽ không báo lỗi. Nhưng chúng hiển thị một chuỗi trống, điều này thường không được mong muốnKhi bạn gọi
# write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
4, bạn trả lại mẫu được hiển thị dưới dạng chuỗi. Như với bất kỳ chuỗi nào khác, bạn có thể sử dụng # write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
5 để ghi nó vào một tệp. Để xem {# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
4 đang hoạt động, hãy chạy tập lệnh[venv] $ python write_messages.py
.. wrote message_sandrine.txt
.. wrote message_gergeley.txt
.. wrote message_frieda.txt
Bạn vừa tạo một tệp cho từng học sinh của mình. Ví dụ, hãy xem
# write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
7Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
Các biến của mẫu
{# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
0 của bạn đã nhận thành công dữ liệu của học sinh. Thật là một cách tuyệt vời để Python và Jinja làm việc cho bạn. Giờ đây, bạn có thể sao chép và dán văn bản, gửi cho học viên của mình và tiết kiệm cho mình một số công việcKiểm soát dòng chảy trong Jinja
Cho đến giờ, bạn đã thêm các biến giữ chỗ vào văn bản mẫu và hiển thị các giá trị vào đó. Trong phần này, bạn sẽ học cách thêm các câu lệnh
# write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
9 và vòng lặp [venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
7 vào một mẫu để hiển thị nội dung theo điều kiện mà không lặp lại chính mìnhSử dụng câu lệnh if
Trong ví dụ ở phần trước, bạn đã lập trình tạo các thông báo tùy chỉnh cho các học sinh giỏi nhất của mình. Bây giờ là lúc để xem xét tất cả các sinh viên của bạn. Thêm hai học sinh có điểm thấp hơn vào
# write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
0 trong {# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
4$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
0Bạn thêm điểm của Fritz và Sirius vào danh sách
# write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
0. Không giống như các sinh viên khác, cả hai đều đạt dưới 80 điểm. Sử dụng dấu [venv] $ python write_messages.py
.. wrote message_sandrine.txt
.. wrote message_gergeley.txt
.. wrote message_frieda.txt
4 để tạo câu điều kiện trong {# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
0$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
1Ngoài các biến mà bạn đã sử dụng trước đây, giờ đây bạn cũng đang sử dụng câu lệnh điều kiện với khối Jinja. Thay vì sử dụng một cặp dấu ngoặc nhọn kép, bạn tạo các khối Jinja bằng một dấu ngoặc nhọn và một dấu phần trăm [
[venv] $ python write_messages.py
.. wrote message_sandrine.txt
.. wrote message_gergeley.txt
.. wrote message_frieda.txt
6] ở mỗi đầuTrong khi bạn có thể coi các biến thông thường là các chuỗi con, các khối Jinja bao bọc một phần lớn hơn trong mẫu của bạn. Đó là lý do tại sao bạn cũng cần nói cho Jinja biết khối của bạn kết thúc ở đâu. Để đóng một khối, bạn sử dụng lại chính các từ khóa đó, với tiền tố
[venv] $ python write_messages.py
.. wrote message_sandrine.txt
.. wrote message_gergeley.txt
.. wrote message_frieda.txt
7Trong ví dụ trên, bạn đang bắt đầu một khối
[venv] $ python write_messages.py
.. wrote message_sandrine.txt
.. wrote message_gergeley.txt
.. wrote message_frieda.txt
8 ở dòng 5, khối này bạn đóng ở dòng 9 với [venv] $ python write_messages.py
.. wrote message_sandrine.txt
.. wrote message_gergeley.txt
.. wrote message_frieda.txt
9. Bản thân câu lệnh # write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
9 hoạt động giống như câu lệnh điều kiện trong Python. Ở dòng 5, bạn đang kiểm tra xem {# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
9 có cao hơn [venv] $ python write_messages.py
.. wrote message_sandrine.txt
.. wrote message_gergeley.txt
.. wrote message_frieda.txt
4 không. Nếu vậy, thì bạn đang hiển thị một thông điệp vui vẻ. Nếu không, bạn hiển thị thông báo xin lỗi ở dòng 8Trong ví dụ trên, các sinh viên đồng ý rằng họ muốn lấy 80 điểm làm điểm chuẩn cho hiệu suất thử thách Python của họ. Vui lòng thay đổi
[venv] $ python write_messages.py
.. wrote message_sandrine.txt
.. wrote message_gergeley.txt
.. wrote message_frieda.txt
4 thành bất kỳ điểm nào khiến bạn và học sinh của bạn cảm thấy thoải mái hơnLoại bỏ các quảng cáoĐòn bẩy cho vòng lặp
Bạn cũng có thể kiểm soát luồng mẫu của mình bằng
[venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
7 vòng lặp. Ví dụ: bạn quyết định tạo một trang HTML cho sinh viên của mình để hiển thị tất cả các kết quả. Lưu ý rằng tất cả học sinh đã đồng ý công khai kết quả của mình trong cuộc thi thân thiện nàyTạo một tệp mới có tên
Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5 trong thư mục >>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
8 của bạn$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
2Tại đây, bạn đang tạo một trang HTML lặp qua từ điển
# write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
0 của bạn và hiển thị hiệu suất của chúng. Cũng giống như với các khối [venv] $ python write_messages.py
.. wrote message_sandrine.txt
.. wrote message_gergeley.txt
.. wrote message_frieda.txt
8, bạn phải đảm bảo rằng bạn đóng khối Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
9 của mình bằng $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
00Bạn có thể kết hợp các câu lệnh
# write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
9 và vòng lặp [venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
7 để kiểm soát luồng mẫu của mình hơn nữa$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
3Tùy thuộc vào điểm số của học sinh, bạn hiển thị biểu tượng cảm xúc mặt cười hoặc mặt buồn. Note that you can put the block expressions on a single line, too
Go on and update
{# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
4$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
4In addition to the
[venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
7 loop where you saved a message for each student, you’re now also writing a single HTML file containing all the results for that student. This time you’re creating a $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
05 dictionary that holds all the variables that you pass into the templateNote. Using
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
05 as a name for the collection that stores the variables for a template is a convention. That said, you can name the dictionary differently if you preferVới một từ điển chứa tất cả ngữ cảnh của mẫu của bạn, bạn có thể gọi
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
07 với $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
05 làm đối số duy nhất. When you run {# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
4, you also create an HTML file>>>
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
5You can view the rendered HTML file in your code editor. However, since you’re working with HTML now, you can also have a look at the file in your browser
Like in Python scripts, you can control the flow of Jinja templates with the
# write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
9 statement and [venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
7 loops. In Jinja, you’re using blocks to wrap content. When you’re using a [venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
7 block, then the content within that block gets rendered in each step of the loopWith templates, you can create building blocks for larger websites without duplicating your front-end code. That’s why web frameworks like Flask leverage the power of Jinja templates. In the next section, you’ll learn how to use Flask to render data from the back end into the web pages of the front end
Use Jinja With Flask
Chances are that you first heard of Jinja when you used a web framework like Flask. Both Jinja and Flask are maintained by Pallets Project, a community-driven organization that takes care of Python libraries that power the Flask web framework
In this section, you’ll continue the examples from before by creating a basic web application for your students
Loại bỏ các quảng cáocài đặt bình
You can continue working in the same directory and virtual environment that you created in the previous section. Khi bạn đang ở trong môi trường ảo đang hoạt động của mình, hãy tiếp tục và cài đặt Flask
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
6With Flask installed, continue to create your first route to verify that Flask works as expected. Create a file named
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
13 in your project’s root directory$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
7When you put an
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
14 decorator on top of a Flask view function, you register it with the given URL . Here, you’re establishing the route $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
15, which returns >>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
7To see your home page in the browser, start your Flask development web server
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
8Now your Flask app is running in
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
17 mode. In $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
17 mode, you’ll get more meaningful error messages if something goes wrong. Also, your server will restart automatically whenever you change something in your codebaseTo see your home page, visit
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
19Awesome, you now have a running Flask app. In the next section, you’ll implement Jinja templates in your Flask app
Add a Base Template
So far, your Flask app returns a string. You could enhance your string by adding HTML code, and Flask would render it for you. But as you learned in the previous section, using templates makes rendering content much more convenient for you
Create a new template named
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20 in your >>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
8 directory$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
9In
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20, you have two occurrences where you use template variables. once in line 7 and then again in line 11. Để hiển thị và phục vụ $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20, hãy tải nó làm trang chủ của bạn trong $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
13[venv] $ python -m pip install Jinja2
0By default, Flask expects your templates in a
>>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
8 directory. Therefore, you don’t need to set the template directory explicitly. When you provide $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20 to $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
27, Flask knows where to look for your templateNote. Usually, you’d improve your website’s look with CSS and add some functionality with JavaScript. Bởi vì bạn đang tập trung vào cấu trúc nội dung trong hướng dẫn này, nên bạn sẽ giữ cho ứng dụng web của mình hầu như không được tạo kiểu
Restart your Flask development server if it hasn’t updated automatically already. Then visit
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
19 and verify that Flask serves and renders your base templateFlask rendered the
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
29 variable in your website’s title and in the welcome message. Next, you’ll create a page to show your students’ resultsLoại bỏ các quảng cáoAdd Another Page
In one of the , you used
Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5 as a template to generate a file named $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
31. Bây giờ bạn đã có một ứng dụng web, bạn có thể sử dụng Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5 để hiển thị mẫu của mình một cách linh hoạt mà không cần lưu nó vào một tệp mới vào lúc nàyĐảm bảo rằng
Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5 được đặt trong >>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
8 và trông như thế này[venv] $ python -m pip install Jinja2
1Các điều chỉnh duy nhất cho phiên bản cũ hơn được đánh dấu ở trên
- Dòng 7 thêm tiêu đề trang động
- Dòng 11 nâng cao tiêu đề đầu tiên
Để truy cập trang kết quả trong ứng dụng web của bạn, bạn phải tạo một lộ trình. Thêm đoạn mã sau vào
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
13[venv] $ python -m pip install Jinja2
2Trong một ứng dụng web chính thức, bạn có thể lưu trữ dữ liệu trong cơ sở dữ liệu bên ngoài. Hiện tại, bạn giữ
# write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
1, # write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
2 và # write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
0 bên cạnh $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
39Flask's
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
27 chỉ chấp nhận một , đó là tên mẫu. Bất kỳ đối số nào khác phải là đối số từ khóa. Vì vậy, bạn phải có hai dấu hoa thị [$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
41] ở phía trước của $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
05. Với các toán tử dấu hoa thị, bạn đang chuyển các mục của $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
05 dưới dạng đối số từ khóa vào $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
27Khi bạn truy cập
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
45 trong trình duyệt của mình, Flask sẽ phục vụ Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5 với ngữ cảnh được hiển thị. Hop over to the browser and take a look at itBây giờ bạn có một trang chủ và một trang hiển thị kết quả của học sinh. That’s a great start for a web app
Trong phần tiếp theo, bạn sẽ học cách tận dụng sức mạnh của các mẫu nhiều hơn nữa bằng cách lồng chúng vào nhau. You’ll also add a navigation menu to your project so that users can conveniently jump from page to page
Nest Your Templates
Khi ứng dụng của bạn phát triển và bạn tiếp tục thêm các mẫu mới, bạn phải đồng bộ hóa mã chung. Cho đến nay, hai mẫu của bạn,
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20 và Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5, trông rất giống nhau. When multiple templates contain the same code, you need to adjust every template if you change any of the common codeTrong phần này, bạn sẽ triển khai cấu trúc mẫu cha và con giúp mã của bạn dễ bảo trì hơn
Điều chỉnh mẫu cơ sở của bạn
Khi bạn sử dụng kế thừa mẫu của Jinja, bạn có thể di chuyển cấu trúc chung của ứng dụng web của mình sang mẫu cơ sở gốc và để các mẫu con kế thừa mã đó
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20 của bạn gần như đã sẵn sàng để dùng làm mẫu cơ sở của bạn. Để làm cho mẫu cơ sở của bạn có thể mở rộng, hãy thêm một số thẻ $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
50 vào cấu trúc[venv] $ python -m pip install Jinja2
3You use the
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
50 tag to define which parts of your base template may be overridden by child templates. Cũng giống như với [venv] $ python write_messages.py
.. wrote message_sandrine.txt
.. wrote message_gergeley.txt
.. wrote message_frieda.txt
8 và Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
9, bạn phải đóng các khối của mình bằng $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
54Lưu ý rằng bạn cũng đặt tên cho các khối của mình. Với đối số
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
29, bạn cho phép một mẫu con thay thế mã giữa $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
56 và $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
57 bằng khối $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
29 của chính nó. Bạn có thể thay mã của $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
59 bằng khối $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
60Ghi chú. Tên của các khối nội dung của bạn phải là duy nhất bên trong mỗi mẫu. Nếu không, Jinja sẽ bối rối không biết chọn khối nào để thay thế
Nội dung mà bạn có ở giữa các thẻ
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
50 trong $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20 là một trình giữ chỗ. Nội dung dự phòng hiển thị bất cứ khi nào một mẫu con không chứa các thẻ $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
50 tương ứngYou can also decide not to add fallback content in between
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
50 tags. Đối với các biến trong mẫu, Jinja sẽ không phàn nàn nếu bạn không cung cấp nội dung cho chúng. Instead, Jinja will render an empty stringTrong
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20, bạn cung cấp nội dung dự phòng cho các khối mẫu của mình. Therefore, you don’t need to change anything in your $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
66 view. Nó sẽ hoạt động giống như trước đâyTrong phần tiếp theo, bạn sẽ chuẩn bị mẫu con của mình để làm việc với
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20Loại bỏ các quảng cáoMở rộng mẫu con
Bạn hiện có một mẫu
Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5 hoạt động độc lập, không có mẫu gốc. That means that you can now adjust the code of Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5 to connect it with $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20[venv] $ python -m pip install Jinja2
4Để kết nối mẫu con với mẫu gốc, bạn phải thêm thẻ
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
71 ở đầu tệpChild templates also contain
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
50 tags. Bằng cách cung cấp tên của khối làm đối số, bạn đang kết nối các khối từ mẫu con với các khối từ mẫu gốcLưu ý rằng
Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5 không chứa khối $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
29. Nhưng trang của bạn sẽ vẫn hiển thị đúng tiêu đề, bởi vì nó sử dụng nội dung dự phòng trong $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20 và chế độ xem cung cấp biến $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
29Bạn không cần điều chỉnh chế độ xem
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
39 của mình. When you visit $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
45, you should notice no changes. The rendered page contains the root code of $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20 and the fill-ins from the block of Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5Keep in mind that any content outside the child template’s blocks won’t appear on your rendered page. Ví dụ: nếu bạn muốn thêm điều hướng vào
Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5 liên kết trở lại trang chủ của mình, thì bạn phải xác định khối giữ chỗ mới trong $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20 hoặc thêm menu điều hướng vào cấu trúc của $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20Bao gồm một Menu điều hướng
The navigation of a website is usually displayed on every page. Với cấu trúc mẫu cơ sở và mẫu con của bạn, tốt nhất bạn nên thêm mã cho menu điều hướng vào
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20Instead of adding the navigation menu code directly into
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20, you can leverage the $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
86 tag. Bằng cách tham chiếu một mẫu khác với $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
86, bạn đang tải toàn bộ mẫu vào vị trí đóCác mẫu được bao gồm là các phần có chứa một phần mã HTML đầy đủ. To indicate that a template is meant to be included, you can prefix its name with an underscore [
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
88]Follow the prefix-based naming scheme and create a new template named
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
89 in your >>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
8 folder[venv] $ python -m pip install Jinja2
5Note that
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
89 contains neither an $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
71 tag nor any $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
50 tags. You can focus solely on how you want to render your navigation menuWhen you use
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
94, Flask creates the full URL to the given view for you. So even when you decide to change the route to one of your pages, the navigation menu will still workInclude
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
89 in $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20 to display your navigation menu on all of your pages[venv] $ python -m pip install Jinja2
6Instead of adding the navigation menu code directly into
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20, you include $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
89 into your website’s header. Vì Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5 kéo dài $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20, bạn có thể truy cập vào $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
45 để xem menu điều hướng mới của mìnhTrang kết quả của bạn kế thừa mã từ mẫu cơ sở của bạn. When you click a link in the navigation menu, the URL in your address bar changes based on your current page
Loại bỏ các quảng cáoÁp dụng các bộ lọc
Hãy dành một chút thời gian và nghĩ về một tình huống trong đó một người khác đảm nhận phần cuối và trách nhiệm của bạn là phần đầu của trang web. Để tránh can thiệp lẫn nhau, bạn không được phép điều chỉnh bất kỳ chế độ xem nào trong
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
13 hoặc thay đổi dữ liệu có trong mẫu của bạnSau cuộc trò chuyện với sinh viên của bạn, bạn đồng ý rằng ứng dụng web của bạn có thể được cải thiện. Bạn nghĩ ra hai tính năng
- Hiển thị các mục menu điều hướng bằng chữ hoa
- Sắp xếp tên học sinh của bạn trong
5 theo thứ tự bảng chữ cáiHello Gergeley! I'm happy to inform you that you did very well on today's Python Challenge. You reached 87 out of 100 points. See you tomorrow! Anke
Bạn sẽ sử dụng chức năng lọc của Jinja để triển khai cả hai tính năng mà không cần chạm vào phần cuối
Điều chỉnh các mục menu của bạn
Jinja cung cấp một loạt. Nếu bạn kiểm tra chúng, thì bạn sẽ nhận thấy rằng chúng trông giống với các hàm dựng sẵn của Python và
Trước khi bạn tiếp tục, hãy xem lại phần
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
89 của bạn[venv] $ python -m pip install Jinja2
5Hiện tại, các mục menu của bạn được hiển thị bằng chữ thường để khớp với tên của chế độ xem. Nếu
[venv] $ python -m pip install Jinja2
05 không phải là chữ thường thì liên kết sẽ không hoạt động. Điều đó có nghĩa là [venv] $ python -m pip install Jinja2
05 trong thuộc tính [venv] $ python -m pip install Jinja2
07 phải giữ nguyênTuy nhiên, bạn có thể điều chỉnh hiển thị của
[venv] $ python -m pip install Jinja2
05 bên trong các thẻ [venv] $ python -m pip install Jinja2
09[venv] $ python -m pip install Jinja2
8Bạn chỉ định biến và sau đó là ký hiệu ống dẫn [_______8_______10], tiếp theo là bộ lọc. Trong một số trường hợp, bạn có thể chỉ định đối số trong ngoặc đơn
Trong menu điều hướng của bạn, bạn bàn giao biến
[venv] $ python -m pip install Jinja2
05 cho bộ lọc [venv] $ python -m pip install Jinja2
12. Giống như phương thức chuỗi [venv] $ python -m pip install Jinja2
13 của Python, bộ lọc [venv] $ python -m pip install Jinja2
12 của Jinja trả về biến bằng chữ in hoaTruy cập
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
19 để xem các thay đổi của bạn trực tiếpHoàn hảo, các mục menu hiện được viết hoa. Bạn đã triển khai tính năng đầu tiên trong danh sách yêu cầu của mình. Đã đến lúc giải quyết yêu cầu tính năng tiếp theo
Sắp xếp danh sách kết quả của bạn
Hiện tại, kết quả của học sinh xuất hiện theo thứ tự như bạn đã định nghĩa chúng trong từ điển ở
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
13. Bạn sẽ sử dụng bộ lọc [venv] $ python -m pip install Jinja2
17 của Jinja để sắp xếp danh sách kết quả theo thứ tự bảng chữ cái của học sinhMở
Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5 và thêm bộ lọc [venv] $ python -m pip install Jinja2
17 vào vòng lặp [venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
7 của bạn[venv] $ python -m pip install Jinja2
9Bộ lọc
[venv] $ python -m pip install Jinja2
17 sử dụng [venv] $ python -m pip install Jinja2
22 của Python dưới mui xeCủa bạn
# write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
0 iterable trong Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5 chứa một từ điển cho mỗi mục. Bằng cách thêm [venv] $ python -m pip install Jinja2
25, bạn yêu cầu Jinja sắp xếp # write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
0 dựa trên các giá trị của {# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
8. Bạn có thể sử dụng bộ lọc [venv] $ python -m pip install Jinja2
17 mà không cần bất kỳ đối số nào khi muốn sắp xếp danh sách các chuỗiChuyển đến
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
45 và xem cách sắp xếp mớiBạn đã sử dụng bộ lọc
[venv] $ python -m pip install Jinja2
17 của Jinja để sắp xếp kết quả của học sinh theo tên của họ. Nếu bạn có những học sinh trùng tên, thì bạn có thể xâu chuỗi các bộ lọc[venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
0Đầu tiên, bạn sắp xếp sinh viên của bạn theo tên. Nếu có hai sinh viên trùng tên, thì bạn sắp xếp họ theo điểm số của họ. Tất nhiên, những sinh viên có cùng tên thực sự cần một số khác biệt ngoài điểm số của họ, nhưng với mục đích của ví dụ này, việc sắp xếp này là đủ
Bạn đảo ngược thứ tự của
{# templates/message.txt #}
Hello {{ name }}!
I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.
See you tomorrow!
Anke
9 để sắp xếp những học sinh này từ điểm cao nhất xuống thấp nhất. Khi dòng của bạn quá dài, Jinja cho phép bạn phân phối câu lệnh của mình trên nhiều dòngGhi chú. Ngược lại với việc sử dụng Booleans trong Python, bạn nên viết Booleans trong Jinja bằng chữ thường
Bạn sử dụng các bộ lọc để chuyển đổi dữ liệu trong các mẫu của mình một cách độc lập với phần cuối của bạn. Khi áp dụng bộ lọc cho một biến, bạn có thể thay đổi giá trị của biến cho người dùng mà không thay đổi bất kỳ cấu trúc dữ liệu cơ bản nào
Kiểm tra để tìm hiểu thêm về các bộ lọc mẫu
Nếu bạn muốn đưa nhiều logic hơn nữa vào các mẫu của mình, thì bạn có thể tận dụng các macro. Trong phần tiếp theo, bạn sẽ khám phá các macro bằng cách triển khai thêm ba tính năng trong ứng dụng Flask của mình
Loại bỏ các quảng cáoBao gồm Macro
Khi bạn bao gồm một phần mẫu như menu điều hướng, mã được bao gồm sẽ được hiển thị trong ngữ cảnh của mẫu gốc mà không cần bất kỳ điều chỉnh nào. Rất thường xuyên, đó chính xác là những gì bạn muốn, nhưng những lúc khác, bạn có thể muốn tùy chỉnh giao diện của các mẫu đi kèm của mình
Các macro của Jinja có thể giúp bạn tạo các phần mẫu chấp nhận các đối số. Giống như khi xác định hàm của riêng bạn trong Python, bạn có thể xác định macro và nhập chúng vào mẫu của mình
Trong phần này, bạn sẽ thêm ba tính năng nữa vào dự án Flask của mình
- Triển khai chế độ tối
- Đánh dấu sinh viên có điểm số tốt nhất
- Đánh dấu trang hiện tại trong menu điều hướng
Giống như trước đây, bạn sẽ không chạm vào bất kỳ mã back-end nào để cải thiện ứng dụng web của mình
Triển khai Chế độ tối
Đối với một số học sinh, cách phối màu sáng tối sẽ hấp dẫn hơn về mặt thị giác. Để phục vụ cho tất cả học viên của mình, bạn sẽ thêm tùy chọn chuyển sang chế độ tối với văn bản màu sáng trên nền tối
Thêm tệp
[venv] $ python -m pip install Jinja2
32 vào thư mục >>> import jinja2
>>> environment = jinja2.Environment[]
>>> template = environment.from_string["Hello, {{ name }}!"]
>>> template.render[name="World"]
'Hello, World!'
8[venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
1Bạn xác định một macro với khối
[venv] $ python -m pip install Jinja2
34 giống với định nghĩa hàm trong Python. Macro của bạn phải có tên và nó có thể chấp nhận đối sốĐối với macro
[venv] $ python -m pip install Jinja2
35, bạn phải cung cấp tên phần tử HTML. Đây sẽ là thành phần mà CSS từ dòng 6 đến dòng 14 chuyển từ nhạt sang đậmĐể tránh phục vụ một trang web có chủ đề tối theo mặc định, bạn muốn cung cấp cho sinh viên của mình tùy chọn để chuyển đổi thiết kế. Khi họ thêm
[venv] $ python -m pip install Jinja2
36 vào bất kỳ tuyến đường nào trong ứng dụng của bạn, bạn sẽ kích hoạt chế độ tốiỞ dòng 4, bạn đang sử dụng đối tượng
[venv] $ python -m pip install Jinja2
37 của Flask để đọc các tham số của yêu cầu. Theo mặc định, đối tượng [venv] $ python -m pip install Jinja2
37 có trong ngữ cảnh mẫu của bạnNếu tham số chế độ tối tồn tại trong yêu cầu GET, thì bạn hiển thị liên kết có tùy chọn chuyển sang chế độ sáng trong dòng 5 và thêm thẻ
[venv] $ python -m pip install Jinja2
39 vào mẫu. Không có bất kỳ tham số chế độ tối nào, bạn hiển thị một liên kết chuyển sang chủ đề tốiĐể sử dụng macro của bạn, bạn phải nhập chúng vào mẫu cơ sở của mình. Cũng giống như câu lệnh
[venv] $ python -m pip install Jinja2
40 của Python, bạn nên đặt khối [venv] $ python -m pip install Jinja2
41 ở đầu mẫu của mình[venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
2Tùy thuộc vào tham số GET của yêu cầu, bạn hiển thị chế độ tối và hiển thị liên kết để chuyển sang chủ đề màu khác
Bằng cách cung cấp
[venv] $ python -m pip install Jinja2
42 làm đối số cho [venv] $ python -m pip install Jinja2
43, học sinh của bạn có thể chuyển bảng màu của toàn bộ trang. Truy cập vào $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
19 và khám phá cách chuyển đổi bảng màu của bạnBạn có thể thử cung cấp
[venv] $ python -m pip install Jinja2
45 thay vì [venv] $ python -m pip install Jinja2
46 cho [venv] $ python -m pip install Jinja2
43 rồi tải lại trang. Bởi vì macro chấp nhận các đối số, nên chúng mang lại cho bạn sự linh hoạt để hiển thị có điều kiện các phần của mẫu của bạnLoại bỏ các quảng cáoLàm nổi bật học sinh giỏi nhất của bạn
Một lý do khác để giới thiệu macro cho dự án Flask của bạn là đặt một số logic vào ngăn riêng của nó. Giống như các mẫu lồng nhau, chức năng gia công phần mềm cho macro có thể dọn dẹp mẫu gốc của bạn
Để làm nổi bật học sinh giỏi nhất của bạn bằng biểu tượng cảm xúc ngôi sao, hãy dọn dẹp
Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5 và tham chiếu macro [venv] $ python -m pip install Jinja2
49[venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
3Lưu ý rằng bạn không nhập
[venv] $ python -m pip install Jinja2
32 ở đầu Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5. Bạn đang mở rộng $ python3 -m venv venv
$ source venv/bin/activate
[venv] $
20, nơi bạn đã nhập tất cả các macro. Vì vậy, không cần phải nhập lại chúng ở đâyThay vì thêm nhiều mã hơn vào một mục danh sách trong
Hello Gergeley!
I'm happy to inform you that you did very well on today's Python Challenge.
You reached 87 out of 100 points.
See you tomorrow!
Anke
5, bạn đang tham khảo [venv] $ python -m pip install Jinja2
54 bên trong vòng lặp [venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
7 của mình. Bạn tận dụng cơ hội để xóa điều kiện # write_messages.py
from jinja2 import Environment, FileSystemLoader
max_score = 100
test_name = "Python Challenge"
students = [
{"name": "Sandrine", "score": 100},
{"name": "Gergeley", "score": 87},
{"name": "Frieda", "score": 92},
]
environment = Environment[loader=FileSystemLoader["templates/"]]
template = environment.get_template["message.txt"]
for student in students:
filename = f"message_{student['name'].lower[]}.txt"
content = template.render[
student,
max_score=max_score,
test_name=test_name
]
with open[filename, mode="w", encoding="utf-8"] as message:
message.write[content]
print[f".. wrote {filename}"]
9 … [venv] $ python -m pip install Jinja2
57 hiển thị biểu tượng cảm xúc khuôn mặt vui hoặc buồn. Mã này sẽ hoàn toàn phù hợp với mục đích thêm huy hiệu cho tất cả học sinh của macroMacro
[venv] $ python -m pip install Jinja2
49 sẽ mong đợi hai đối số- Từ điển
59 hiện tại[venv] $ python -m pip install Jinja2
- Danh sách
0 đầy đủ# write_messages.py from jinja2 import Environment, FileSystemLoader max_score = 100 test_name = "Python Challenge" students = [ {"name": "Sandrine", "score": 100}, {"name": "Gergeley", "score": 87}, {"name": "Frieda", "score": 92}, ] environment = Environment[loader=FileSystemLoader["templates/"]] template = environment.get_template["message.txt"] for student in students: filename = f"message_{student['name'].lower[]}.txt" content = template.render[ student, max_score=max_score, test_name=test_name ] with open[filename, mode="w", encoding="utf-8"] as message: message.write[content] print[f".. wrote {filename}"]
Nếu bạn đã truy cập trang của mình bây giờ, bạn sẽ gặp lỗi vì Flask không thể tìm thấy macro mà bạn đang tham chiếu. Tiếp tục và thêm macro mới của bạn vào
[venv] $ python -m pip install Jinja2
32[venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
4Jinja cho phép bạn xác định các biến của riêng mình bên trong một mẫu với khối
[venv] $ python -m pip install Jinja2
62. Khi bạn xác định biến, bạn cũng có thể thêm bộ lọc vào giá trị của chúng và thậm chí xâu chuỗi chúngTrong
[venv] $ python -m pip install Jinja2
63, bạn đang xác định [venv] $ python -m pip install Jinja2
64 bằng cách trước tiên tạo danh sách tất cả các điểm bằng bộ lọc [venv] $ python -m pip install Jinja2
65 và sau đó chọn điểm cao nhất với [venv] $ python -m pip install Jinja2
66. Cả hai bộ lọc đều hoạt động tương tự như hàm [venv] $ python -m pip install Jinja2
65 hoặc [venv] $ python -m pip install Jinja2
66 của PythonKhi bạn biết điểm cao nhất trong số các học sinh của mình, sau đó bạn kiểm tra điểm của học sinh hiện tại của bạn so với điểm đó từ dòng 8 đến 14
Truy cập
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
45 và xem hoạt động của macro mới của bạnNgoài biểu tượng cảm xúc mặt cười hoặc buồn mà bạn đã hiển thị trước đây, giờ đây, bạn hiển thị biểu tượng cảm xúc ngôi sao cho học sinh có thành tích tốt nhất của mình
Đánh dấu trang hiện tại
Tính năng cuối cùng mà bạn triển khai sẽ cải thiện menu điều hướng của bạn. Hiện tại, menu điều hướng vẫn giữ nguyên trên cả hai trang. Trong phần này, bạn sẽ tạo một macro đánh dấu mục menu của trang hiện tại bằng một mũi tên
Thêm một macro khác vào
[venv] $ python -m pip install Jinja2
32[venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
5Macro
[venv] $ python -m pip install Jinja2
71 của bạn lấy mục menu làm đối số. Nếu [venv] $ python -m pip install Jinja2
05 khớp với điểm cuối hiện tại, thì macro của bạn sẽ hiển thị một mũi tên ngoài liên kết mục menuNgoài ra, bạn kiểm tra chế độ màu. Nếu
[venv] $ python -m pip install Jinja2
73 là một phần của yêu cầu GET, thì [venv] $ python -m pip install Jinja2
74 sẽ được thêm vào liên kết menu. Nếu không kiểm tra và thêm chế độ, bạn sẽ chuyển sang chủ đề sáng mỗi khi nhấp vào liên kết, vì [venv] $ python -m pip install Jinja2
74 sẽ không phải là một phần của liên kếtThay thế liên kết mục menu trong
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
89 bằng macro mới của bạn[venv] $ python -m pip list
Package Version
---------- -------
Jinja2 3.x
...
6Bằng cách thêm macro
[venv] $ python -m pip install Jinja2
71 vào menu điều hướng của bạn, bạn giữ cho mẫu điều hướng của mình sạch sẽ. Bạn đưa ra bất kỳ logic có điều kiện nào cho [venv] $ python -m pip install Jinja2
71Truy cập
$ python3 -m venv venv
$ source venv/bin/activate
[venv] $
19 và xem tất cả các tính năng mà bạn đã triển khaiMacro là một tính năng mạnh mẽ của Jinja. Tuy nhiên, bạn không nên lạm dụng chúng. Trong một số trường hợp, có thể tốt hơn nếu đưa logic vào phần cuối thay vì để các mẫu của bạn thực hiện công việc
Sẽ luôn có những trường hợp khó khăn mà bạn phải quyết định xem bạn có thêm mã trực tiếp vào mẫu của mình hay không, chuyển nó sang một mẫu được bao gồm hay tạo macro thay thế. Nếu mã mẫu của bạn vật lộn quá nhiều với cấu trúc dữ liệu, thì đó thậm chí có thể là dấu hiệu cho thấy logic mã của bạn thuộc về mặt sau của ứng dụng
Loại bỏ các quảng cáoPhần kết luận
Jinja là một công cụ tạo khuôn mẫu giàu tính năng được đóng gói với khung web Flask. Nhưng bạn cũng có thể sử dụng Jinja độc lập với Flask để tạo các mẫu mà bạn có thể điền nội dung theo chương trình
Trong hướng dẫn này, bạn đã học cách
- Cài đặt công cụ mẫu Jinja
- Tạo mẫu Jinja đầu tiên của bạn
- Kết xuất mẫu Jinja trong Flask
- Sử dụng
7 vòng lặp và câu điều kiện với Jinja[venv] $ python -m pip list Package Version ---------- ------- Jinja2 3.x ...
- Mẫu Nest Jinja
- Sửa đổi các biến trong Jinja bằng các bộ lọc
- Sử dụng macro để thêm chức năng vào giao diện người dùng của bạn
Nếu bạn muốn áp dụng kiến thức mới thu được của mình về Jinja, thì bạn có thể xây dựng giao diện người dùng cho công cụ rút ngắn URL FastAPI hoặc triển khai ứng dụng Flask của bạn cho Heroku
Bạn cũng có thể cân nhắc sử dụng. Để tìm hiểu thêm về sự khác biệt giữa công cụ tạo khuôn mẫu của Jinja và Django, hãy truy cập tài liệu của Jinja về việc chuyển đổi từ các công cụ tạo khuôn mẫu khác sang Jinja
Bạn có các trường hợp sử dụng khác mà bạn tận dụng các khả năng mà Jinja cung cấp không?
Đánh dấu là đã hoàn thành
🐍 Thủ thuật Python 💌
Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python
Gửi cho tôi thủ thuật Python »
Giới thiệu về Philipp Accany
Philipp là một kỹ sư phần mềm có trụ sở tại Berlin với nền tảng thiết kế đồ họa và niềm đam mê phát triển web toàn diện
» Thông tin thêm về PhilippMỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là
Aldren
Geir Arne
Joanna
kate
Michael
Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực
Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonista chuyên gia
Nâng cao kỹ năng Python của bạn »
Chuyên gia Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực
Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng Pythonistas chuyên gia
Nâng cao kỹ năng Python của bạn »
Bạn nghĩ sao?
Đánh giá bài viết này
Tweet Chia sẻ Chia sẻ EmailBài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?
Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi