无法再访问 django 管理员

Posted

技术标签:

【中文标题】无法再访问 django 管理员【英文标题】:Can't access django admin anymore 【发布时间】:2021-10-31 11:50:30 【问题描述】:

上次迁移后,我无法再访问我的管理页面。我所做的只是添加一个连接两个模型(列表和用户)的外部字段。我收到消息:

“C:... Django\commerce\media\admin”不存在

我做了很多搜索,但我想出的只是删除“django.contrib.sites”,或者在设置 SITE_ID 等于 1 时添加它。还有建议:

from django.contrib.sites.models import Site
Site.objects.create(pk=1, domain='localhost', name='localhost')

进入python shell。

这些东西都不适合我。出于某种原因,django 似乎正在我的媒体文件夹中搜索,但我不知道它为什么会这样做。

我的设置:

"""
Django settings for commerce project.

Generated by 'django-admin startproject' using Django 3.0.2.

For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/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/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '6ps8j!crjgrxt34cqbqn7x&b3y%(fny8k8nh21+qa)%ws3fh!q'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'auctions',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'commerce.urls'

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 = 'commerce.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    


AUTH_USER_MODEL = 'auctions.User'

# Password validation
# https://docs.djangoproject.com/en/3.0/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/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'US/Pacific'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, javascript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'

MEDIA_ROOT = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

SITE_ID = 1

相关型号:

class User(AbstractUser):
    pass

class Listing(models.Model):
    lister = models.ForeignKey("User", on_delete=models.CASCADE)
    # the first the value the computer sees, the second for humans
    categories = [
            ("clothes", "Clothes"),
            ("electronics", "Electronics"),
            ("toys", "Toys"),
            ("home", "Home"),
            ("other", "Other")
        ]
    category = models.CharField(
            max_length=20,
            choices = categories
        )
    name = models.CharField(max_length=50)
    pic = models.ImageField(upload_to="images/", null=True, blank=True)
    description = models.TextField()
    bid = models.DecimalField(max_digits=8, decimal_places=2)
    date = models.DateTimeField(null=True, blank=True)

    def place(self):
        self.date = timezone.now()
        self.save()
        

    def __str__(self):
        return f"self.name going for self.bid since self.date"

我对 Django 毫无经验;它被证明是相当令人头疼的,但它也是 Web 构建的最佳框架之一。任何帮助表示赞赏!

【问题讨论】:

【参考方案1】:

在您的settings.py 文件中,您定义了 2 次MEDIA_ROOT。 应该是:

MEDIA_URL = '/media/'MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

【讨论】:

不错的收获!我不知道这是怎么发生的;我没有更改它,但感谢您找到它!

以上是关于无法再访问 django 管理员的主要内容,如果未能解决你的问题,请参考以下文章

Django Admin后台管理

无法使用 PostgreSQL 数据库访问管理页面

Django 清除所有管理员列表过滤器

Django - 更新用户/帐户管理

Django:从内联模型管理员访问父实例

在 Django 中,只允许管理员用户访问视图