-->

django 1.9 and registration/login.html

2020-08-10 08:15发布

问题:

I'm working on a django 1.9 project.

With Django 1.7.7, login functionnalities was working, but now all the time I have : registration/login.html : Template Does Not Exist

The templates login.html, logout.html are present in 'webgui/template/registration/' and I didn't modified them.

Here some of my 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/')
)

And my 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)
]

What's wrong? I checked the Django docs, but that's the default behaviour.

回答1:

Try to put your template dirs paths in DIRS list inside TEMPLATES setting. (Anyway, your template folder name should be templates not 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',
        ],
    },
},
]


回答2:

This question appears first in search engine with searching for registration/login.html : Template Does Not Exist

So for those coming through search engine, please note that this template doesn't come by default with Django. If you haven't created it, that's why you are getting this error

Here is how you can create a simple one https://simpleisbetterthancomplex.com/tutorial/2016/06/27/how-to-use-djangos-built-in-login-system.html



回答3:

After upgrading my django to 1.9.1, same thing happened to me. Apparently, there are updates on templates directory. Here is how I fixed it.

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

Of course you should have BASE_DIR defined

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

After that,I've deleted the templates folder and created a templates folder for each app. So, inside the each app, just create templates and put the html files inside.

Also in views, connect it to the html file like this.

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

This worked for me.



回答4:

You have set 'APP_DIRS': True,, so Django will search for templates directories inside each app in INSTALLED_APPS, including your webgui app.

The problem is that you have named your directory webgui/template/ instead of webgui/templates/, so the app loader won't find it.

The easiest fix is to rename your directory. If you don't want to do this, you'll have to add the webgui/template directory to your DIRS option.