Linked Questions

Popular Questions

Django ValueError at /admin/, even after flush

Asked by At

I'm setting up an Django application with react, redux and the django-rest-framework. After adding a "Customer" model within Django, I got an ValueError when accessing the admin interface.

The error keeps returning even after removing all the models from the admin interface by removing the line: admin.site.register(model).

Even when I flush the database, the error keeps returning.

I used the django User model with a knox token to log into the django application.

This is an local django installation within a pipenv.

error:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/

Django Version: 2.1.5
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'leads',
 'rest_framework',
 'frontend',
 'knox',
 'accounts',
 'customer']
Installed 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']



Traceback:

File "/home/[NAME]/.local/share/virtualenvs/lead_manager-2CdCcx2v/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/[NAME]/.local/share/virtualenvs/lead_manager-2CdCcx2v/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "/home/[NAME]/.local/share/virtualenvs/lead_manager-2CdCcx2v/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/[NAME]/.local/share/virtualenvs/lead_manager-2CdCcx2v/lib/python3.5/site-packages/django/contrib/admin/sites.py" in wrapper
  241.                 return self.admin_view(view, cacheable)(*args, **kwargs)

File "/home/[NAME]/.local/share/virtualenvs/lead_manager-2CdCcx2v/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/home/[NAME]/.local/share/virtualenvs/lead_manager-2CdCcx2v/lib/python3.5/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)

File "/home/[NAME]/.local/share/virtualenvs/lead_manager-2CdCcx2v/lib/python3.5/site-packages/django/contrib/admin/sites.py" in inner
  213.                 if request.path == reverse('admin:logout', current_app=self.name):

File "/home/[NAME]/.local/share/virtualenvs/lead_manager-2CdCcx2v/lib/python3.5/site-packages/django/urls/base.py" in reverse
  58.                 app_list = resolver.app_dict[ns]

File "/home/[NAME]/.local/share/virtualenvs/lead_manager-2CdCcx2v/lib/python3.5/site-packages/django/urls/resolvers.py" in app_dict
  477.             self._populate()

File "/home/[NAME]/.local/share/virtualenvs/lead_manager-2CdCcx2v/lib/python3.5/site-packages/django/urls/resolvers.py" in _populate
  430.                     url_pattern._populate()

File "/home/[NAME]/.local/share/virtualenvs/lead_manager-2CdCcx2v/lib/python3.5/site-packages/django/urls/resolvers.py" in _populate
  419.                     bits = normalize(url_pattern.pattern.regex.pattern)

File "/home/[NAME]/.local/share/virtualenvs/lead_manager-2CdCcx2v/lib/python3.5/site-packages/django/utils/regex_helper.py" in normalize
  153.                 count, ch = get_quantifier(ch, pattern_iter)

File "/home/[NAME]/.local/share/virtualenvs/lead_manager-2CdCcx2v/lib/python3.5/site-packages/django/utils/regex_helper.py" in get_quantifier
  265.     return int(values[0]), ch

Exception Type: ValueError at /admin/
Exception Value: invalid literal for int() with base 10: 'id'

Settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'leads',
    'rest_framework',
    'frontend',
    'knox',
    'accounts',
    'customer',
]

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': ('knox.auth.TokenAuthentication',)
}

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 = 'leadmanager.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',
            ],
        },
    },
]

Model:

phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")

class CustomerManager(models.Manager):

    def create_customer(self, username, password, first_name, last_name, email, phone, country, city, zip, address):
        customer = self.create(username=username, password=password, first_name=first_name, last_name=last_name, email=email, phone=phone, country=country, city=city, zip=zip, address=address)
        return customer

# Create your models here.
class Customer(models.Model):
    username = models.CharField(max_length=50)
    password = models.CharField(max_length=256)
    first_name = models.CharField(max_length=75)
    last_name = models.CharField(max_length=75)
    email = models.EmailField()
    phone = models.CharField(validators=[phone_regex], max_length=17, blank=True)
    country = CountryField()
    city = models.CharField(max_length=50, default='City')
    zip = models.CharField(max_length=10)
    address = models.CharField(max_length=100)
    status = models.SmallIntegerField(choices=STATUS_CHOICES, default=1)
    objects = CustomerManager()

    def verify_password(self, raw_password):
        return pbkdf2_sha256.verify(raw_password, self.password)

    def __str__(self):
        return self.first_name + " " + self.last_name

My expected results are to get access to the Django Admin interface without errors. I hope that I have given all the information needed to resolve this problem.

Edit: urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('frontend.urls')),
    path('', include('leads.urls')),
    path('', include('accounts.urls')),
    path('', include('customer.urls')),
]

Related Questions