Django學習筆記-建立簡易首頁

前置條件

已完成從建立虛擬環境到啓用服務,啓動服務後能正常顯示hello world!html

新建網頁

一、在項目根目錄下創建存放網頁的文件夾,如templates,目錄結構爲:django

templates
      ├─ base.html
      └─ pages
              ├─ index.html
              └─ services.html

二、編寫簡易網站首頁 在index.html文件中編寫簡易網站首頁,templates文件夾下的其餘文件不予考慮。網站

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ title }}</title>
</head>
<body>
<h1>{{ welcome }}</h1>
</body>
</html>

三、設置pages\views.pydebug

from django.shortcuts import render

def index(request):
    return render(request, 'pages/index.html', context={
                      'title': 'FWA',
                      'welcome': '歡迎訪問FWA公司首頁',
                  })

四、設置settings.pycode

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],	# 新添加的內容,讓django找到網頁存放的位置
        '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',
            ],
        },
    },
]

啓動項目

能夠顯示:歡迎訪問FWA公司首頁htm

相關文章
相關標籤/搜索