django-admin.py startproject website在Django還有很重要的就是APP,一個專案可由很多APP組成,APP可多個專案互相使用,所以寫出一個好的APP給專案通用是相當重要的
終端機下輸入
python manage.py startapp staff產生出Staff的APP
接著在Staff下的views.py新增一個function
def staffhello(request,name):
return render(request, 'staff_hello.html', {"hello_str": "你好啊{name},這是來自Staff APP!!".format(name=name)})
staff下新增一個templates資料夾,然後再新增一個staff_hello.html
<!DOCTYPE html>
<html>
<head>
<title>staff_hello</title>
</head>
<body>
<h1>{{ hello_str }}</h1>
</body>
</html>
修改website底下的urls.py
from staff.views import staffhello
urlpatterns = [
path("staff/<str:name>",staffhello),
]
重點來了上述做完後如果網站跑起來會有錯誤必須註冊APP
在setting.py中修改
#原本
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
#修改後
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'staff',
]

