gpt4 book ai didi

python - Django_Hosts 导入错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:11:34 25 4
gpt4 key购买 nike

我想做的是让网站重定向到 www(例如“example.com”到“www.example.com”)

我使用pip安装了django_hosts,然后按照网站上的文档

这是我的settings.py 文件:

"""
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 1.9.

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 = 'lsjz5tm#+0(99cv@mg=himl8=4w-vd^qq07jpd3d5278!hv06x'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = [
# 'example.com',
# 'www.example.com',
'*',
]


# Application definition

INSTALLED_APPS = [
'Reviews',
'Contact',
'Lessons',
'News',
'QNA',
'Home',
'About',
'Pupils',
'Website',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#third party
'django_hosts'
]

MIDDLEWARE_CLASSES = [
'django_hosts.middleware.HostsRequestMiddleware'
'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',
'django_hosts.middleware.HostsResponseMiddleware'
]

ROOT_URLCONF = 'mysite.urls'
ROOT_HOSTCONF = 'mysite.hosts'
DEFAULT_HOST = 'www'

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 = 'mysite.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'),
}
}


# 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/'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
STATICFILES_DIRS = (
os.path.join('static'),
)
STATIC_ROOT = ''

这是我的hosts.py 文件:

from django.conf import settings
from django_hosts import patterns, host

host_patterns = patterns('',
host(r'www', settings.ROOT_URLCONF, name='www'),
#host(r'(\w+)', 'path.to.custom_urls', name='wildcard'),
)

当我运行 python manage.py runserver 时,我得到以下错误:

django.core.exceptions.ImproperlyConfigured:无法加载 WSGI 应用程序“mysite.wsgi.application”;导入模块时出错:'没有名为'django_hosts.middleware.HostsRequestMiddlewaredjango'的模块; 'django_hosts.middleware' 不是一个包'

我做错了什么?!

最佳答案

您在第一行末尾漏掉了一个逗号:

MIDDLEWARE_CLASSES = [
'django_hosts.middleware.HostsRequestMiddleware', # <-----
'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',
]

关于python - Django_Hosts 导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42471097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com