python - 访问 django 管理页面时出错,如下 - TypeError at/admin/login/_getfullpathname : illegal type for path parameter

标签 python django django-templates django-urls django-settings

在处理该项目时,出现了上述错误,我无法再访问 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)

设置.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'),
]

回溯错误如下 -

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 有问题:

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('\\','/')

关于python - 访问 django 管理页面时出错,如下 - TypeError at/admin/login/_getfullpathname : illegal type for path parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35297289/

相关文章:

python - Django 管理操作的权限

python - Django : How to manage database specific code?

python - 如何删除groupby的最后一行

python - 使用 Pygame 制作多个 'game screens'

python - 我如何使用 Python 和 Selenium 遍历 webelements 列表?

javascript - Django - 倒数计时器

Django模板: include template passing translated variable

迭代数组并删除所有 None 的 Pythonic 方式

python - 在法国的 Windows 服务器上使用 django 的换行符问题

python - 即时/动态创建模板