Django 将用户发送到错误的模板位置?

Posted

技术标签:

【中文标题】Django 将用户发送到错误的模板位置?【英文标题】:Django sending users to the wrong template location? 【发布时间】:2019-05-26 22:35:44 【问题描述】:

我正在编写 Python 速成课程第 19 章,我们将在其中设置“学习日志”应用程序。我遇到了模板引用错误(在第 440 页)。

除了获取此处列出的所有更新之外,我还重新编写了本书的这一部分:https://ehmatthes.github.io/pcc/chapter_19/README.html

但似乎不断收到看起来是模板引用的错误。我注意到模板在查找 login.html 时引用了“learning_logs”目录,这是不正确的——它应该在“learning_log”目录中查找。

learning_log\users\urls.py

from django.contrib.auth import views as auth_views

from . import views

app_name = 'users'
urlpatterns = [
    # Login page.
    path('login/',
        auth_views.LoginView.as_view(template_name='users/login.html'),
        name='login'),
]

learning_logs\urls.py

"""learning_logs URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""

"""Defines URL patterns for learning_logs."""

from django.urls import path

from . import views

app_name = 'learning_logs'
urlpatterns = [
    # Home page.
    path('', views.index, name='index'),

    #Show all topics.
    path('topics/', views.topics, name='topics'),

    #Detail page for a single topic.
    path('topics/<int:topic_id>/', views.topic, name='topic'),

    #Page for adding a new topic.
    path('new_topic/', views.new_topic, name='new_topic'),

    #Page for adding a new entry.
    path('new_entry/<int:topic_id>/', views.new_entry, name='new_entry'),

    #Page for editing an entry.
    path('edit_entry/<int:entry_id>/', views.edit_entry, name='edit_entry'),
]

learning_log\urls.py

"""learning_log URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.urls import path, include
from django.contrib import admin

urlpatterns = [
    path('admin/', admin.site.urls),
    path('users/', include('users.urls')),
    path('', include('learning_logs.urls')),
]

这是浏览器错误:

TemplateDoesNotExist at /users/login/
users/login.html
Request Method:
GET
Request URL:
http://localhost:8000/users/login/
Django Version:
2.2.1
Exception Type:
TemplateDoesNotExist
Exception Value:
users/login.html
Exception Location:
C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\template\loader.py in select_template, line 47
Python Executable:
C:\Users\kevin\Documents\webapps\11_env\Scripts\python.exe
Python Version:
3.7.3
Python Path:
['C:\\Users\\kevin\\Documents\\webapps',
 'C:\\Users\\kevin\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip',
 'C:\\Users\\kevin\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
 'C:\\Users\\kevin\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
 'C:\\Users\\kevin\\AppData\\Local\\Programs\\Python\\Python37-32',
 'C:\\Users\\kevin\\Documents\\webapps\\11_env',
 'C:\\Users\\kevin\\Documents\\webapps\\11_env\\lib\\site-packages']
Server time:
Sun, 26 May 2019 22:14:26 +0000
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\contrib\admin\templates\users\login.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\contrib\auth\templates\users\login.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\kevin\Documents\webapps\learning_logs\templates\users\login.html (Source does not exist)

这是服务器错误:


    Traceback (most recent call last):
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
        response = get_response(request)
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\core\handlers\base.py", line 145, in _get_response
        response = self.process_exception_by_middleware(e, request)
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\core\handlers\base.py", line 143, in _get_response
        response = response.render()
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\template\response.py", line 106, in render
        self.content = self.rendered_content
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\template\response.py", line 81, in rendered_content
        template = self.resolve_template(self.template_name)
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\template\response.py", line 63, in resolve_template
        return select_template(template, using=self.using)
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\template\loader.py", line 47, in select_template
        raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
    django.template.exceptions.TemplateDoesNotExist: users/login.html
    [26/May/2019 15:12:27] "GET /users/login/ HTTP/1.1" 500 79384
    Not Found: /users/
    [26/May/2019 15:12:39] "GET /users/ HTTP/1.1" 404 3224
    Internal Server Error: /users/login/
    Traceback (most recent call last):
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
        response = get_response(request)
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\core\handlers\base.py", line 145, in _get_response
        response = self.process_exception_by_middleware(e, request)
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\core\handlers\base.py", line 143, in _get_response
        response = response.render()
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\template\response.py", line 106, in render
        self.content = self.rendered_content
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\template\response.py", line 81, in rendered_content
        template = self.resolve_template(self.template_name)
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\template\response.py", line 63, in resolve_template
        return select_template(template, using=self.using)
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\template\loader.py", line 47, in select_template
        raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
    django.template.exceptions.TemplateDoesNotExist: users/login.html
    [26/May/2019 15:12:43] "GET /users/login/ HTTP/1.1" 500 79384
    Internal Server Error: /users/login/
    Traceback (most recent call last):
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
        response = get_response(request)
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\core\handlers\base.py", line 145, in _get_response
        response = self.process_exception_by_middleware(e, request)
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\core\handlers\base.py", line 143, in _get_response
        response = response.render()
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\template\response.py", line 106, in render
        self.content = self.rendered_content
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\template\response.py", line 81, in rendered_content
        template = self.resolve_template(self.template_name)
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\template\response.py", line 63, in resolve_template
        return select_template(template, using=self.using)
      File "C:\Users\kevin\Documents\webapps\11_env\lib\site-packages\django\template\loader.py", line 47, in select_template
        raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
    django.template.exceptions.TemplateDoesNotExist: users/login.html

【问题讨论】:

【参考方案1】:

在设置中仔细检查您的模板目录并确保斜杠是正斜杠/不是 \

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',
            ],
        ,
    ,

【讨论】:

【参考方案2】:

你确定你没有打错任何东西吗?我做的很多,它总是把我的程序弄乱了。尝试检查您输入 learning_logs 或 learning_log 的位置,看看是否需要添加或删除 s。

【讨论】:

【参考方案3】:

看来这本书建议您将 login.html 文件保存到 learning_log/users/templates 文件夹,但它应该在 learning_logs 文件夹中。一旦我移动了模板,就解决了我的问题。感谢 Nurhun 和 Lumiobyte!

【讨论】:

以上是关于Django 将用户发送到错误的模板位置?的主要内容,如果未能解决你的问题,请参考以下文章

如何将变量发送到由通用视图调用的模板

将表格数据从模板发送到 django 中的 view.py

Django - 如何正确地将列表从模板发送到 AJAX 以查看?

如何将数据发送到 Django 中的基本模板?

如何将带有上下文变量的 Django 模板发送到 Ajax 调用?

如何仅将对象的一部分发送到django中的模板