簡體   English   中英

django 1.9和registration / login.html

[英]django 1.9 and registration/login.html

我正在研究django 1.9項目。

使用Django 1.7.7,登錄功能正常,但現在我一直有: registration/login.html : Template Does Not Exist

模板login.html,logout.html出現在'webgui / template / registration /'中,我沒有修改它們。

這里有一些我的settings.py:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'webgui',
]

MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'project.urls'

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

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'NCIS.db'),
}
}

STATIC_URL = '/static/'
LOGIN_REDIRECT_URL = '/login/'

LOGOUT_URL = '/logout/'


DIRS = (
    join(BASE_DIR, 'webgui/template/registration'),
    join(BASE_DIR, 'webgui/template/')
)

我的urls.py

from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth.views import login, logout
import webgui.views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', login, name='login.html'),
    url(r'^login/$', login, name='login.html'),
    url(r'^logout/$', logout, name='logout.html'),

    url(r'^homepage/$', webgui.views.homepage),
    url(r'^addproject/$', webgui.views.addproject)
]

怎么了? 我檢查了Django文檔,但這是默認行為。

此問題首先出現在搜索引擎中,搜索registration/login.html : Template Does Not Exist

因此,對於那些通過搜索引擎發送的人,請注意,Django默認不提供此模板。 如果您還沒有創建它,那就是您收到此錯誤的原因

以下是如何創建一個簡單的https://simpleisbetterthancomplex.com/tutorial/2016/06/27/how-to-use-djangos-built-in-login-system.html

將我的django升級到1.9.1之后,同樣的事情發生在我身上。 顯然,模板目錄有更新。 這是我修復它的方法。

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    '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',
        ],
    },
},
]

當然你應該定義BASE_DIR

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

之后,我刪除了templates文件夾,並為每個應用程序創建了一個模板文件夾。 因此,在每個應用程序內部,只需創建模板並將html文件放入其中。

同樣在視圖中,將它連接到這樣的html文件。

def index(request):
    context_dict = {}
    return render(request, 'index.html', context_dict)

這對我有用。

嘗試將模板dirs路徑放在TEMPLATES設置中的DIRS列表中。 (無論如何,您的模板文件夾名稱應該是templates而不是template 。)

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [join(BASE_DIR, 'webgui/template/registration'),join(BASE_DIR, 'webgui/template/')],
    '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',
        ],
    },
},
]

您已設置'APP_DIRS': True,因此Django將在INSTALLED_APPS搜索每個應用內的templates目錄,包括您的webgui應用。

問題是您已將目錄命名為webgui/template/而不是webgui/templates/ ,因此應用程序加載器將找不到它。

最簡單的解決方法是重命名目錄。 如果您不想這樣做,則必須將webgui/template目錄添加到DIRS選項中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM