无法再访问 django 管理

Can't access django admin anymore

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

“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')

进入pythonshell.

None 这些东西对我有用。出于某种原因,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 构建框架之一。感谢您的帮助!

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

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