Posts Hello world trong Django
Post
Cancel

Hello world trong Django

Để tạo 1 app tên là home trong django ta chạy như sau: python3.8 manage.py startapp home Cấu trúc của app vừa tạo sẽ như thế này:

1
2
3
4
5
6
7
8
home/
├── admin.py
├── __init__.py
├── migrations
│   └── __init__.py
├── models.py
├── tests.py
└── views.py

Trong cấu trúc trên ta chỉ quan tâm nhiều nhất là models.py và views.py. Ngoài ra chúng ta cần tạo thêm 2 đối tượng quan trọng nữa là url config cho app và template. Url config cho app sẽ nằm trong file urls.py:

1
touch urls.py

Template sẽ nằm trong folder template

1
mkdir templates

Tạo 1 trang “hello world” trong Django

Để tạo 1 trang hello world trong django chúng ta cần các bước sau:

  1. Template html chứa nội dung “hello world”.
  2. View dùng để render template
  3. Url cho view

Bước 1: Tạo template:

Vào thư mục gốc của dự án, sau đó:

1
vim home/templates/home/index.html

Với nội dung:

1
Hello world

Sau đó khai báo đường dẫn folder chứa template vừa mới khai báo bằng cách cập nhật file settings.py. Tìm đến phần TEMPLATES:

Sửa

1
2
3
...
 'DIRS': [],
...

Thành

1
2
3
4
5
...
'DIRS': [
           os.path.join(BASE_DIR, 'home', 'templates'),
       ],
...

Bước 2: Tạo view để render template

1
vim /home/views.py

với nội dung

1
2
3
4
5
6
7
8
from django.shortcuts import render


def index(request):
   return render(
       request,
       'home/index.html'
   )

Bước 3: Config url cho view mới:

Cập nhật file urls.py trong home:

1
2
3
4
5
6
7
8
9
rom django.conf.urls import patterns, url

urlpatterns = patterns(
   '',
   url(
       r'^$',
       'home.views.index',
       name='index'),
)

Khai báo file urls.py của app home vào home/urls.py:

1
2
3
4
5
6
7
8
9
from django.conf.urls import patterns, include, url


urlpatterns = patterns(
   '',
   url(
       r'^',
       include('home.urls', namespace='home')),
)
This post is licensed under CC BY 4.0 by the author.