本文主要是介绍四、记一次问题处理过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
截止到三、设计留言板功能,出现了一个问题,现在困扰了大概一天的时间终于解决,结果发现是一个很简单的问题,但网上把我误导了,现在记录如下:
问题描述
问题主要是出在运行项目的时候,未能显示出message_form.html的内容,报错为:
DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0xbb in position 9: invalid start byte. You passed in 'E:\\python\xbb\xb7\xbe\xb3\\lib\\site-packages\\django\\core\\handlers\\exception.py' (<type 'str'>)
分析
乍一看报错信息,有一部分是乱码的E:\\python\xbb\xb7\xbe\xb3
,经过转码,发现乱码部分是对应汉字“环境”,这个目录是我的python环境的目录。于是就认为是目录有中文导致的。但是这就很奇怪了,以前弄项目也是有这个目录的,都没有出现问题。
在网上查阅了许多方法,有的说是需要自己重新转码,有的说是其他问题,但经过试验没有生效,最后仔细检查了一下配置文件setting.py发现了一个极为简单且弱智的错误:没加中括号。
解决方法
更改了setting.py文件这个字段为如下:
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',],},},
]
注意,这里是给‘DIRS’字段的值添加了中括号变成:
'DIRS': [os.path.join(BASE_DIR, 'templates')],
这样就好了
附上现在的setting.py的完整代码:
# -*-coding: utf-8 -*-
"""
Django settings for DjangoTestProject project.Generated by 'django-admin startproject' using Django 1.11.10.For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""import os
import sys# 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.11/howto/deployment/checklist/# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'o16id4z4s3amec4p8_)ix&)p=dep+)p-sn7bqhvsa*4z9p@)ea'# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = TrueALLOWED_HOSTS = ['*']# Application definitionINSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',
]MIDDLEWARE = ['django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.locale.LocaleMiddleware','django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware',
]ROOT_URLCONF = 'DjangoTestProject.urls'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',],},},
]WSGI_APPLICATION = 'DjangoTestProject.wsgi.application'# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databasesDATABASES = {'default': {'ENGINE': 'django.db.backends.mysql','NAME': 'testdjangoz','USER': 'root','PASSWORD': 'redhat','HOST': '127.0.0.1'}
}# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validatorsAUTH_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.11/topics/i18n/LANGUAGE_CODE = 'en-us'TIME_ZONE = 'UTC'USE_I18N = TrueUSE_L10N = TrueUSE_TZ = True# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'DjangoTestProject/static')
]
这篇关于四、记一次问题处理过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!