Chuyển đến nội dung chính

Templates Trong Django

Templates

Trong trang Django Intro, chúng tôi đã biết rằng kết quả phải ở dạng HTML và nó phải được tạo trong một mẫu, vì vậy hãy làm điều đó.

Tạo một thư mục templates bên trong thư mục members và tạo một tệp HTML có tên myfirst.html.

Cấu trúc tệp phải giống như sau:

myworld
    manage.py
    myworld/
    members/
        templates/
            myfirst.html

Mở tệp HTML và chèn những thứ sau:

members/templates/myfirst.html:

<!DOCTYPE html>
<html>
<body>

<h1>Hello World!</h1>
<p>Welcome to my first Django project!</p>

</body>
</html>

Sửa View Trong Django

Mở tệp views.py và thay thế chế độ xem chỉ mục bằng:

members/views.py:

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

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

Thay đổi cài đặt

Để có thể làm việc với những thứ phức tạp hơn “Hello World!”, Chúng tôi phải nói với Django rằng một ứng dụng mới đã được tạo.

Điều này được thực hiện trong tệp settings.py trong thư mục myworld.

Tra cứu danh sách INSTALLED_APPS [] và thêm ứng dụng members như sau:

myworld / settings.py:

INSTALLED_APPS = [
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'member.apps.MembersConfig'
]

Sau đó chạy lệnh này:

py manage.py migrate

Cái nào sẽ tạo ra đầu ra này:

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

(myproject)C:\Users\Your Name\myproject\myworld>

Khởi động máy chủ bằng cách điều hướng đến thư mục / myworld và thực hiện lệnh này:

py management.py runningerver

Trong cửa sổ trình duyệt, nhập 127.0.0.1:8000/members/ vào thanh địa chỉ.

Kết quả sẽ như thế này:

Django Templates
Django Templates

The post Templates Trong Django first appeared on Techacademy.



source https://techacademy.edu.vn/templates-trong-django/

Nhận xét

Bài đăng phổ biến từ blog này

Đề Thi HSG Tin Học Lớp 12 Tỉnh Lào Cai Năm 2025

Rate this post Dưới đây là Đề Thi HSG Tin Học Lớp 12 Tỉnh Lào Cai Năm 2025 Bạn đọc có thể share lời giải bên dưới comment bài viết. Tham khảo: TỔNG HỢP ĐỀ THI CHUYÊN TIN TẤT CẢ CÁC TRƯỜNG TRÊN TOÀN QUỐC TỔNG HỢP ĐỀ THI HỌC SINH GIỎI TIN HỌC LỚP 9 TOÀN QUỐC TỔNG HỢP ĐỀ THI HỌC SINH GIỎI TIN HỌC LỚP 12 TOÀN QUỐC KHÓA HỌC LẬP TRÌNH C++ KHÓA HỌC LẬP TRÌNH PYTHON  The post Đề Thi HSG Tin Học Lớp 12 Tỉnh Lào Cai Năm 2025 first appeared on Techacademy .

Cách Vẽ Hình Trong Scratch

Hãy cùng Techacademy tìm hiểu cách vẽ hình trong lập trình Scratch nhé! Tại đây bạn sẽ biết thêm nhiều điều thú vị và hấp dẫn về cách vẽ các loại hình trong Scratch. I. Cách Vẽ Hình Vuông Trong Scratch Trong bài viết này mình sẽ hướng dẫn các bạn cách vẽ hình vuông trong Scratch đồng thời sử dụng kĩ thuật quay hợp lý để nhân bản tạo thành những hình vẽ đẹp đã ra trong các kì thi tin học trẻ phần vẽ hình bằng Scratch. Hãy tham khảo với onthihsg ngay nhé. + Thủ tục con vẽ hình vuông trong Scratch Trước hết ta cùng xây dựng một mảnh ghép để vẽ hình vuông với tham số là cạnh của hình vuông như sau: Cách Vẽ Hình Vuông Trong Scratch Chỉ cần một vòng lặp lại 4 lần việc vẽ một cạnh và xoay 90 độ là xong, quá đơn giản phải không nào + Vẽ các hình phức tạp hơn từ hình vuông Bây giờ ta hãy phát triển để vẽ hai hình trong đề thi tin học trẻ Đông Triều năm 2019 nào Cách Vẽ Hình Vuông Trong Scratch Nhìn hình ta thấy hình tạo thành từ 5 hình vuông vì vậy ta sẽ gọi 5 lần thủ tục vẽ h...

How to Choose Term Paper Writing Service

When looking to employ term paper writers to you contador de palabras ingles r writing job on the internet there are certainly some important variables to check before employing them. As many have seen around the planet, an contador de palabras twitter effective author ought to have the ability to combine technical knowledge and outstanding writing ability with the right methodology and application. It is no good, using a writer who can only produce a well researched and concisely written composition. There are several points which should always be kept in mind before deciding on any writer. An individual must bear in mind that a writer is just as good as their past performance. Thus, assess their academic writing documents and also take a look at their freelance job portfolio. Assess how many times they’ve been cited or acknowledged for their excellent academic writing aid. Writers for academic and term papers all specialize in a specific area of academic essay writing solutions. ...