Hello. Suddenly without me changing nothing related to the admin I got this while trying to login into my admin account. I could not find any solution by now.
Welcome @Erol-Mehmed !
First, a couple of side notes: Please don’t post links to error messages or tracebacks on another site. Copy/paste the error with the traceback here, with the entire block marked as “preformatted text”. (You can do this by surrounding the text between lines of ```. This means you’ll have a line of ```, then the text, then another line of ```.) Also, please don’t post screen images of the browser error message. It’s more helpful to post the complete error message with the traceback from the server console.
Now, we’re going to need to see more details about your project. You say that you haven’t changed anything related to the admin. What changes have you made? In particular, have you made any changes to your models? What about your settings?
Hello Ken. Thanks for the advices.
I did not make any changes to the settings.py file and I did not make any changes to any of the models when this problem occurred. I did make changes only to my urls’s files and to one of the views, but after removing those changes the error still persists.
@KenWhitesell I still did not find solution.
If it was working “then”, but not working “now”, something has changed - either in your code or in your working environment. Did you add any third-party packages to your project? Did you upgrade any packages?
We could look at your settings.py
, admin.py
, and models.py
files, and the output of a pip list
command, to see if there’s anything obvious there - but that would just be a starting point.
settings.py
"""
Django settings for backend project.
Generated by 'django-admin startproject' using Django 5.1.1.
For more information on this file, see
https://docs.djangoproject.com/en/5.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.1/ref/settings/
"""
from pathlib import Path
import os
from settings_module.config_env import ALLOWED_HOSTS, SECRET_KEY, CORS_ALLOWED_ORIGINS
from settings_module.rest_framework_settings import REST_FRAMEWORK
from settings_module.database_settings import DATABASES
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = SECRET_KEY
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ALLOWED_HOSTS
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'corsheaders',
'product_catalog',
'core',
]
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',
'corsheaders.middleware.CorsMiddleware',
]
# CORS settings
CORS_ALLOWED_ORIGINS = CORS_ALLOWED_ORIGINS
# REST FRAMEWORK SETTINGS
REST_FRAMEWORK = REST_FRAMEWORK
ROOT_URLCONF = 'backend.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 = 'backend.wsgi.application'
# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
DATABASES = DATABASES
# Password validation
# https://docs.djangoproject.com/en/5.1/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/5.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/
STATIC_URL = 'static/'
# Media files (Uploaded files)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
admin.py
from django.contrib import admin
from .models import Product, ProductImage, ProductMaterial, Discount
@admin.register(Discount)
class DiscountAdmin(admin.ModelAdmin):
list_display = ('name', 'percent', 'start_date', 'end_date')
search_fields = ('name',)
list_filter = ('start_date', 'end_date')
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
list_display = ('name', 'category', 'gender', 'price', 'condition', 'size', 'color', 'condition', 'description', 'discount')
search_fields = ('name', 'category')
list_filter = ('category', 'condition', 'color')
@admin.register(ProductImage)
class ProductImageAdmin(admin.ModelAdmin):
list_display = ('product', 'image')
search_fields = ('product__name',)
@admin.register(ProductMaterial)
class ProductMaterialAdmin(admin.ModelAdmin):
list_display = ('name',)
search_fields = ('name',)
models.py
from django.db import models
import uuid
from django.utils.text import slugify
class BaseModel(models.Model):
name = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class Category(BaseModel):
def __str__(self):
return self.name
class Meta:
verbose_name_plural = 'Categories'
class Discount(BaseModel):
percent = models.DecimalField(max_digits=5, decimal_places=2)
start_date = models.DateTimeField()
end_date = models.DateTimeField()
def __str__(self):
return self.name
class Product(BaseModel):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
product_number = models.CharField(unique=True, editable=False)
slug = models.SlugField(max_length=255, unique=True)
gender = models.CharField(max_length=255)
price = models.DecimalField(max_digits=10, decimal_places=2)
size = models.CharField(max_length=255)
condition = models.CharField(max_length=255)
color = models.CharField(max_length=255)
description = models.TextField()
materials = models.ManyToManyField('ProductMaterial', related_name='products')
discount = models.ForeignKey(Discount, null=True, blank=True, on_delete=models.SET_NULL)
def __str__(self):
return self.name
def save(self, *args, **kwargs):
if not self.product_number:
self.product_number = str(uuid.uuid4().int)[:8]
if not self.slug:
self.slug = f"{slugify(self.name)}-pr{self.product_number}"
super().save(*args, **kwargs)
class ProductImage(BaseModel):
name = None
product = models.ForeignKey(Product, related_name='images', on_delete=models.CASCADE)
image = models.ImageField(upload_to='products/')
def __str__(self):
return f"Image for {self.product.name}"
class ProductMaterial(BaseModel):
def __str__(self):
return self.name
pip list
Package Version
------------------- -------
asgiref 3.8.1
Django 5.1.1
django-cors-headers 4.4.0
djangorestframework 3.15.2
pillow 10.4.0
pip 24.2
psycopg 3.2.1
psycopg-binary 3.2.1
sqlparse 0.5.1
typing_extensions 4.12.2
tzdata 2024.1
Quoting directly from the docs at Home - Django REST framework
REST framework requires the following:
- Django (4.2, 5.0)
- Python (3.8, 3.9, 3.10, 3.11, 3.12)
The DRF docs do not show that DRF is compatible with Django 5.1.
Side note: You hadn’t mentioned that you were using DRF. This is not an official DRF support site. They have their own support channels.
Hm. Lest hope this is the problem. Thanks a lot and I will try and see if using earlier version solves it.
This solved the problem. Thanks again.