所以我正在阅读PluralSight的'Django Fundamentals',它基于旧版本的Django。正如我现在写的那样,我正在使用Django 1.10,我相信它是最新版本。当我关注视频时,我正在为一所虚构的学校创建一个数据库。这是我基于Pluralsight教程的网址模式:
from django.conf.urls import url
from django.contrib import admin
from school.views import home
urlpatterns += patterns(
'django.contrib.auth.views',
url(r'^login/$', 'login',
{'template_name': 'login.html'},
name = 'school_login'),
url(r'^logout/$', 'logout',
{'next_page': 'school_home'},
name = 'school_logout'),
)
更新:
我现在根据@karthir的建议更改了它,并且包含了所有网址:
from django.conf.urls import url
from django.contrib import admin
from school.views import home
urlpatterns = [
url(r'^admin/', admin.site.urls),
#url(r'^$', home, name = 'home') #school.views.home')
#url(r'^$', 'main.views.home', name = 'school_home')
url(r'^$', home, name = 'home')
]
urlpatterns = [
#'django.contrib.auth.views',
url(r'^login/$', 'login', {'template_name': 'login.html'}, name = 'school_login'),
url(r'^logout/$', 'logout', {'next_page': 'school_home'}, name = 'school_logout'),
]
这是新错误:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper
at 0x03CD92B8>
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 226, in
wrapper
fn(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\core\management\commands\runserver.
py", line 121, in inner_run
self.check(display_num_errors=True)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 374,
in check
include_deployment_checks=include_deployment_checks,
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 361,
in _run_checks
return checks.run_checks(**kwargs)
File "C:\Python34\lib\site-packages\django\core\checks\registry.py", line 81,
in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Python34\lib\site-packages\django\core\checks\urls.py", line 14, in c
heck_url_config
return check_resolver(resolver)
File "C:\Python34\lib\site-packages\django\core\checks\urls.py", line 24, in c
heck_resolver
for pattern in resolver.url_patterns:
File "C:\Python34\lib\site-packages\django\utils\functional.py", line 35, in _
_get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python34\lib\site-packages\django\urls\resolvers.py", line 313, in ur
l_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python34\lib\site-packages\django\utils\functional.py", line 35, in _
_get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python34\lib\site-packages\django\urls\resolvers.py", line 306, in ur
lconf_module
return import_module(self.urlconf_name)
File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "C:\Python34\Scripts\schoolDatabase\schoolDatabase\urls.py", line 23, in
<module>
url(r'^$', 'main.views.home', name = 'school_home')
File "C:\Python34\lib\site-packages\django\conf\urls\__init__.py", line 85, in
url
raise TypeError('view must be a callable or a list/tuple in the case of incl
ude().')
TypeError: view must be a callable or a list/tuple in the case of include().
现在我如何更新它以便它可以与Django 1.10一起使用?我认为这与我不知道如何更新settings.py以添加TEMPLATE_DIRS这一事实有关。
这是我的settings.py:
"""
Django settings for schoolDatabase project.
Generated by 'django-admin startproject' using Django 1.9.1.
For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '!^ht#05ke0msws0%qt2ctve@cwn*-!=-&!+o^bkhc!16^&m^!q'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'school'
]
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 = 'schoolDatabase.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',
],
},
},
]
WSGI_APPLICATION = 'schoolDatabase.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#'ENGINE': 'django.db.backends.mysql',
#'NAME': 'wadzanai_school',
#'USER': 'wadzanai_school',
#'PASSWORD': 'OberynViper01',
#'HOST': '10.169.0.121', # Or an IP Address that your DB is hosted on
#'PORT': '3306',
}
}
# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
TEMPLATE_DIRS = (os.path.join(BASE_DIR, "static"),)