简体   繁体   中英

A server error occurred. Please contact the administrator

I am trying to create a user sign up profile.Here is the code. But i run my server. try to open my admin panel it shows (A server error occurred. Please contact the administrator.). Help me out This is my model

from __future__ import unicode_literals
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.encoding import python_2_unicode_compatible


@python_2_unicode_compatible
class User(AbstractUser):
    """
        First Name and Last Name do not cover name patterns around the globe.
    """
    name = models.CharField(_("name"), max_length=20,
                                unique=True)
    adress = models.CharField(_("Address"), max_length=20,
                                     blank=True)
    city = models.CharField(_("City"), max_length=15, blank=True)
    country = models.CharField(_("Country"), max_length=15, blank=True)

    def __str__(self):
        return self.name

    #def get_absolute_url(self):
     #   return reverse('users:detail', kwargs={'username': self.username})

    class Meta(AbstractUser.Meta):
        swappable = 'AUTH_USER_MODEL'

    USERNAME_FIELD = 'username'

And my setting.py is

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.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '6b3k$&lk-mt08%di2#%oj+ggr=7r_)_7#=uomzsj)jk&yy@tg^'

# 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',
    'debug_toolbar',
    'login',
]

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',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
]

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


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'myproject',
        'USER': 'myprojectuser',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',
    }
}
# Password validation
# https://docs.djangoproject.com/en/1.10/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.10/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.10/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

import django.contrib.auth
django.contrib.auth.LOGIN_URL = '/'

INTERNAL_IPS = '127.0.0.1'

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


AUTH_USER_MODEL = 'login.User'

I guess that you want to get a category from a given id.

def test_carete_sub_category(self):
    category_id = 1
    category = Category.objects.get(id=category_id)
    ...

When testing, you have to create some objects in order to check the code works. In this example Category object. If not, this will raise an ObjectDoesNotExist exception

There are several ways to create these objects in your tests. Here is 2 possible ways:

1) You can dump some models of your database in a json file.

Run python manage.py dumpdata myapp.category 1> categories.json , copy it to the fixtures sub-directory of your app and load it as a fixture in your tests

class ProjectTests(APITestCase):
    # This create the objects from dump for each test
    fixtures = ['categories.json']  

    def test_carete_sub_category(self):
         # Get the category object with id 1 defined in categories.json
         category = Category.objects.get(id=1)  
         ...

2) You can create the objects in the setUp method of your TestCase

class ProjectTests(APITestCase):
    # This is called before each test
    def setUp(self):
         the_args_for_creation = {'name': 'bal-bla'}
         self._cat_id = Category.objects.create(** the_args_for_creation).id   

    def test_carete_sub_category(self):
         # Get the category object
         category = Category.objects.get(id=self._cat_id)  
         ...

You may be interested by django-model-mommy or django-factory-boy to make object creation easier in your tests.

Of course, the object creation can be done in the test itself rather than in the setUp . If done in setUp , the objects will be created at the beginning of each test.

I hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM