我想要做的是让网站重定向到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’不是包’
我究竟做错了什么?!