Hướng dẫn Django - Python - Day 4: Django Template
Hướng dẫn Django - Python - Day 4: Django Template
- Tạo folder templates bên trong folder first_project.
- Trong file first_project/setting.py ta sửa như sau:
Trong đó os là ứng dụng để lấy ra đường dẫn của thư mục. Ta dùng phương thức os.path.join() để đảm bảo đường dẫn đến template là chính xác khi developer khác sử dụng dụng app trên các máy khác nhau.
- Vẫn trong file setting, kéo xuống bên dưới phần đăng kí templates ta chèn TEMPLATE_DIR vào mảng DIRS
- Trong folder templates vừa tạo, tạo file index.html như sau:
- Tại file first_app/views ta sửa lại:
Sử dụng hàm render để đưa ra file index.html trong templates, và đưa thêm nội dung của dictionary my_dict bằng context.
Tóm lại: Follow như sau - Tại setting ta setup đường dẫn về template, tại template ta tạo ra các html ở đây là file index.html, tại views ta yêu cầu render() file index.html của template. Ta có thể đẩy trực tiệp nội dung từ views sang index.html ở template bằng cấu trúc {{....}} ở đây sử dụng biến {{insert_me}}.
------------Cấu trúc project đến lúc này như sau --------------
Lưu ý: ở đây đã chuyển index.html vào thư mục templates/first_app và thay đổi đường dẫn trong views.py
- Tạo folder templates bên trong folder first_project.
- Trong file first_project/setting.py ta sửa như sau:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
#Đây là phương thức để sử dụng template của mình cho người dùng khác
TEMPLATE_DIR = os.path.join(BASE_DIR, "template")
Trong đó os là ứng dụng để lấy ra đường dẫn của thư mục. Ta dùng phương thức os.path.join() để đảm bảo đường dẫn đến template là chính xác khi developer khác sử dụng dụng app trên các máy khác nhau.
- Vẫn trong file setting, kéo xuống bên dưới phần đăng kí templates ta chèn TEMPLATE_DIR vào mảng DIRS
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR,],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
- Trong folder templates vừa tạo, tạo file index.html như sau:
<html>
<head>
<meta charset="utf-8">
<title>This is the 1st App</title>
</head>
<body>
<h1>Hello, this is index.html</h1>
{{ insert_me }} #để gọi trong file views.
</body>
</html>
- Tại file first_app/views ta sửa lại:
def index(request): #tạo hàm lấy ra dữ liệu từ yêu cầu
my_dict = {'insert_me': "Hello, I am from views.py"}
return render(request, 'index.html', context = my_dict)
Sử dụng hàm render để đưa ra file index.html trong templates, và đưa thêm nội dung của dictionary my_dict bằng context.
Tóm lại: Follow như sau - Tại setting ta setup đường dẫn về template, tại template ta tạo ra các html ở đây là file index.html, tại views ta yêu cầu render() file index.html của template. Ta có thể đẩy trực tiệp nội dung từ views sang index.html ở template bằng cấu trúc {{....}} ở đây sử dụng biến {{insert_me}}.
------------Cấu trúc project đến lúc này như sau --------------
Lưu ý: ở đây đã chuyển index.html vào thư mục templates/first_app và thay đổi đường dẫn trong views.py
Nhận xét
Đăng nhận xét