python – Django_Hosts导入错误

我想要做的是让网站重定向到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',]

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...