環境
Django 2.0 + Win 10 + Pycharm + 360瀏覽器
報錯
項目結構(報異常)
解決方法
看了好多大佬的解決方法,基本上都是配置settings.py文件,配來配去搞了好幾個小時,依然沒有解決問題。
後來發現,提示說的是templates路徑下找不到文件,那麼我們就在項目裏面自己建個文件夾,命名爲templates。
運行發現,還是有問題,還是找不到html,修改路由啥的搞了一通,還是沒用。
經過多次實驗,發現了正解:在templates下新建個文件夾,與應用名稱相同(我的是indexpage),再把html放到路徑下,終於可以訪問了!
項目結構(已解決)
代碼(已解決)
indexpage/views.py
... ...
def get_login_page(request):
return render(request, "indexpage/login.html")
... ...
indexpage/urls.py
from django.urls import path, include
import indexpage.views
urlpatterns = [
path('hello',indexpage.views.helloworld),
path('login', indexpage.views.get_login_page),
]
settings.py(保持默認)
... ...
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# print(BASE_DIR)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
... ...
有緣看到的小夥伴,給人家點個讚唄~麼麼噠❤