访问 django 管理页面时出错,如下所示 - TypeError at /admin/login/ _getfullpathname: 路径参数的非法类型
Posted
技术标签:
【中文标题】访问 django 管理页面时出错,如下所示 - TypeError at /admin/login/ _getfullpathname: 路径参数的非法类型【英文标题】:Error while accessing the django admin page as follows - TypeError at /admin/login/ _getfullpathname: illegal type for path parameter 【发布时间】:2016-05-19 17:56:22 【问题描述】:在处理该项目时,上面提到的一个错误出现了,我无法再访问 Django 管理页面并且还没有找到解决它的方法。我们将不胜感激。
apps 目录下的 admin.py -
from django.contrib import admin
from .models import Page , Category , UserProfile
# Register your models here.
admin.site.register(Page)
admin.site.register(Category)
admin.site.register(UserProfile)
settings.py -
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 = '&=k(67-(72)h_w%f3b#4&fuop1+-_uyv3$lwx5(qz1*1il0^2x'
# 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',
'rango'
]
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 = 'tango_with_django_project.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 = 'tango_with_django_project.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/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR , 'static')
]
MEDIA_ROOT = [
os.path.join(BASE_DIR , 'media')
]
urls.py
from django.conf.urls import url , include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^rango/', include('rango.urls'))
]
应用内的 urls.py -
from django.conf.urls import patterns , url
from .import views
urlpatterns = [
url(r'^$' , views.index , name = 'index' ) ,
url(r'^about/' , views.about , name = 'about' ) ,
url(r'^category/(?P<category_name_url>\w+)/$' , views.category , name = 'category'),
url(r'^add_category/', views.add_category, name = 'add_category'),
url(r'^category/(?P<category_name>\w+)/add_page/$' , views.add_page , name = 'add_page'),
url(r'^register/$' , views.register , name = 'register'),
]
Traceback 错误如下 -
Template error:
In template C:\Python35-32\lib\site-packages\django\contrib\admin\templates\admin\login.html, error at line 0
_getfullpathname: illegal type for path parameter 1 : % extends "admin/base_site.html" %
2 : % load i18n admin_static %
3 :
4 : % block extrastyle % block.super <link rel="stylesheet" type="text/css" href="% static "admin/css/login.css" %" />
5 : form.media
6 : % endblock %
7 :
8 : % block bodyclass % block.super login% endblock %
9 :
10 : % block usertools %% endblock %
Traceback:
File "C:\Python35-32\lib\site-packages\django\core\handlers\base.py" in get_response
174. response = self.process_exception_by_middleware(e, request)
File "C:\Python35-32\lib\site-packages\django\core\handlers\base.py" in get_response
172. response = response.render()
File "C:\Python35-32\lib\site-packages\django\template\response.py" in render
160. self.content = self.rendered_content
File "C:\Python35-32\lib\site-packages\django\template\response.py" in rendered_content
137. content = template.render(context, self._request)
File "C:\Python35-32\lib\site-packages\django\template\backends\django.py" in render
95. return self.template.render(context)
File "C:\Python35-32\lib\site-packages\django\template\base.py" in render
206. return self._render(context)
File "C:\Python35-32\lib\site-packages\django\template\base.py" in _render
197. return self.nodelist.render(context)
File "C:\Python35-32\lib\site-packages\django\template\base.py" in render
992. bit = node.render_annotated(context)
File "C:\Python35-32\lib\site-packages\django\template\base.py" in render_annotated
959. return self.render(context)
File "C:\Python35-32\lib\site-packages\django\template\loader_tags.py" in render
173. return compiled_parent._render(context)
File "C:\Python35-32\lib\site-packages\django\template\base.py" in _render
197. return self.nodelist.render(context)
File "C:\Python35-32\lib\site-packages\django\template\base.py" in render
992. bit = node.render_annotated(context)
File "C:\Python35-32\lib\site-packages\django\template\base.py" in render_annotated
959. return self.render(context)
File "C:\Python35-32\lib\site-packages\django\template\loader_tags.py" in render
173. return compiled_parent._render(context)
File "C:\Python35-32\lib\site-packages\django\template\base.py" in _render
197. return self.nodelist.render(context)
File "C:\Python35-32\lib\site-packages\django\template\base.py" in render
992. bit = node.render_annotated(context)
File "C:\Python35-32\lib\site-packages\django\template\base.py" in render_annotated
959. return self.render(context)
File "C:\Python35-32\lib\site-packages\django\template\loader_tags.py" in render
69. result = block.nodelist.render(context)
File "C:\Python35-32\lib\site-packages\django\template\base.py" in render
992. bit = node.render_annotated(context)
File "C:\Python35-32\lib\site-packages\django\template\base.py" in render_annotated
959. return self.render(context)
File "C:\Python35-32\lib\site-packages\django\template\library.py" in render
201. output = self.func(*resolved_args, **resolved_kwargs)
File "C:\Python35-32\lib\site-packages\django\contrib\admin\templatetags\admin_static.py" in static
17. return _static(path)
File "C:\Python35-32\lib\site-packages\django\contrib\staticfiles\templatetags\staticfiles.py" in static
9. return staticfiles_storage.url(path)
File "C:\Python35-32\lib\site-packages\django\utils\functional.py" in inner
204. self._setup()
File "C:\Python35-32\lib\site-packages\django\contrib\staticfiles\storage.py" in _setup
394. self._wrapped = get_storage_class(settings.STATICFILES_STORAGE)()
File "C:\Python35-32\lib\site-packages\django\contrib\staticfiles\storage.py" in __init__
39. *args, **kwargs)
File "C:\Python35-32\lib\site-packages\django\core\files\storage.py" in __init__
185. self.location = abspathu(self.base_location)
File "C:\Python35-32\lib\ntpath.py" in abspath
535. path = _getfullpathname(path)
Exception Type: TypeError at /admin/login/
Exception Value: _getfullpathname: illegal type for path parameter
【问题讨论】:
MEDIA_ROOT
in settings.py 应该是字符串,而不是列表 docs.djangoproject.com/en/1.9/ref/settings/#media-root
【参考方案1】:
你的 MEDIA_ROOT 有问题:
MEDIA_ROOT = [
os.path.join(BASE_DIR , 'media')
]
MEDIA_ROOT 与 STATICFILES_DIRS 不同,STATICFILES_DIRS 是一个可以包含多个目录的列表或元组。
尝试修改MEDIA_ROOT如下:
MEDIA_ROOT = os.path.join(BASE_DIR , 'media')
或
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), '../media/').replace('\\','/')
【讨论】:
以上是关于访问 django 管理页面时出错,如下所示 - TypeError at /admin/login/ _getfullpathname: 路径参数的非法类型的主要内容,如果未能解决你的问题,请参考以下文章