当我尝试使用默认的 django 登录页面时收到此错误:
模板不存在于 /login/ 注册/登录.html
这是我的网址文件:
from django.urls import path
from django.urls import path, include
from . import views
urlpatterns = [
path("", views.home, name="home"),
path("main-page/", views.main_page, name="main-page"),
path("kick-page/", views.kick_page, name="kick-page"),
path('', include("django.contrib.auth.urls")), # add django auth pages for login
]
这是我的视图文件:
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth import login, authenticate
from .forms import * #import all the forms from forms.py
def home (response):
form = SignIn()
return render(response, "main/home.html", {"form": form})
#need a page to select site/microtik, and add/remove microtiks
def main_page (response):
return render(response, "main/main_page.html", {})
#Need a page that shows the selected mictotik and a form to enter MAC of device to kick
def kick_page (response):
return render(response, "main/kick_page.html", {})
这是我的目录结构:
C:.
└───microsite
│ db.sqlite3
│ manage.py
│
├───main
│ │ admin.py
│ │ apps.py
│ │ forms.py
│ │ models.py
│ │ tests.py
│ │ urls.py
│ │ views.py
│ │ __init__.py
│ │
│ ├───migrations
│ │ │ __init__.py
│ │ │
│ │ └───__pycache__
│ │ __init__.cpython-311.pyc
│ │
│ ├───templates
│ │ └───main
│ │ │ base.html
│ │ │ home.html
│ │ │ kick_page.html
│ │ │ main_page.html
│ │ │
│ │ └───registration
│ │ login.html
│ │
│ └───__pycache__
│ admin.cpython-311.pyc
│ apps.cpython-311.pyc
│ forms.cpython-311.pyc
│ models.cpython-311.pyc
│ urls.cpython-311.pyc
│ views.cpython-311.pyc
│ __init__.cpython-311.pyc
│
└───microsite
│ asgi.py
│ settings.py
│ urls.py
│ wsgi.py
│ __init__.py
│
└───__pycache__
settings.cpython-311.pyc
urls.cpython-311.pyc
wsgi.cpython-311.pyc
__init__.cpython-311.pyc
我正在按照 Tech with Tim 的指南尝试向我的网站添加访问控制,以便用户必须先登录才能访问网站上的其他视图。
我将错误连同文件信息一起放入聊天 GPT 中,它不断告诉我,该错误是由于文件“login.html”未存储在模板文件夹内名为“registration”的文件夹中而引起的。
我已经进行了三次拼写检查并验证了文件位置,但我仍然无法加载登录页面。我的所有其他页面及其模板都可以正常加载。
如果有人能帮助我弄清楚发生了什么事,我将非常感激。
您遇到
TemplateDoesNotExist
错误的原因是您的 templates/registration
目录的位置。请确保此目录位于项目的根目录中,特别是位于 microsite/templates/registration/login.html
。自定义登录模板的正确路径应该相对于项目的根目录,以便 Django 识别和访问。