异常类型:ValidationError at / home异常值:[u“'1a3288b3-7588-483f-8e85-1affa952dbbf'值必须是整数。”]

时间:2017-02-06 03:56:17

标签: django django-models

我已经创建了注册页面,模型,当用户注册后,他被重定向到主页。当我点击注册用户正在插入数据库,但当它重定向到主页时,我收到此错误:

回溯:

内部的文件“C:\ Python27 \ lib \ site-packages \ django \ core \ handlers \ exception.py”   39. response = get_response(request)

_get_response中的文件“C:\ Python27 \ lib \ site-packages \ django \ core \ handlers \ base.py”   187. response = self.process_exception_by_middleware(e,request)

_get_response中的文件“C:\ Python27 \ lib \ site-packages \ django \ core \ handlers \ base.py”   185. response = wrapped_callback(request,* callback_args,** callback_kwargs)

主页中的文件“C:\ Users \ KR \ Desktop \ projects \ project2 \ welcome \ homeview.py”   14.如果request.user.is_authenticated():

内部文件“C:\ Python27 \ lib \ site-packages \ django \ utils \ functional.py”   234. self._setup()

_setup中的

文件“C:\ Python27 \ lib \ site-packages \ django \ utils \ functional.py”   380. self._wrapped = self._setupfunc()

文件“C:\ Python27 \ lib \ site-packages \ django \ contrib \ auth \ middleware.py”in   24. request.user = SimpleLazyObject(lambda:get_user(request))

get_user中的文件“C:\ Python27 \ lib \ site-packages \ django \ contrib \ auth \ middleware.py”   12. request._cached_user = auth.get_user(request)

get_user中的文件“C:\ Python27 \ lib \ site-packages \ django \ contrib \ auth__init __。py”   180. user_id = _get_user_session_key(request)

_get_user_session_key中的文件“C:\ Python27 \ lib \ site-packages \ django \ contrib \ auth__init __。py”   59. return get_user_model()._ meta.pk.to_python(request.session [SESSION_KEY])

to_python中的文件“C:\ Python27 \ lib \ site-packages \ django \ db \ models \ fields__init __。py”   927. params = {'value':value},

异常类型:/ home处的ValidationError 例外值:[u“'1a3288b3-7588-483f-8e85-1affa952dbbf'值必须是整数。”]

这是模型:

class user_model(AbstractBaseUser):
    user_id = models.CharField(max_length=50, primary_key=True)
    username = models.CharField(max_length=50, unique=True)
    first_name = models.CharField(max_length=50, default="None")
    last_name = models.CharField(max_length=50, default="None")
    email = models.EmailField(default="None")
    password = models.CharField(max_length=150, default="abc123")

    '''Custom user information fields. '''
    myself = models.CharField(max_length=300, default="None")
    address = models.CharField(max_length=300, default="None")
    mobilePh = models.CharField(max_length=20, default="None")
    workPh = models.CharField(max_length=20, default="None")
    workEmail = models.EmailField(default="None")
    last_login = models.CharField(max_length=50, default="None")
    profile_pic = models.ImageField(width_field=None, height_field=None,
                                    blank=True, default="None",
                                    upload_to=upload_profile_pic)
    banner_pic = models.ImageField(width_field=None, height_field=None,
                                   blank=True, default="None",
                                   upload_to=upload_banner_pic)

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['user_id']

    class Meta:
        db_table = 'user_model'
        app_label = 'welcome'

这是注册视图

def register(request):
    if request.method == 'POST':
    if validate_registration_attributes(request.POST):
        user_uuid = uuid.uuid4()
        try:
        user = User.objects.create(user_id=str(user_uuid),                                                                     last_login=str(datetime.datetime.now()),
                       username=request.POST['username'],
                       first_name=request.POST['first_name'],
                       last_name=request.POST['last_name'],
                       email=request.POST['email'],
                       profile_pic="blank_image.png",
                       banner_pic="blank_image.png",
                       password='',
                       )
        user.set_password(request.POST['passwd_1'])
        user.save()

        login(request, user)
        return HttpResponseRedirect('/home')
        except Exception as err:
        user.delete()
        print "Something happened"
        raise Exception("""EXCEPTION at register-user.objects.create in
                        userObject:%s""" % (err))
        global exceptmsg
        return HttpResponseRedirect('/welcome')
    else:
        print "New account registration form validation failed"
        context = {'RegisterResponse': exceptmsg}
        return render(request, 'welcome.html', context)

这是主页视图:

def home(request):
    if request.user.is_authenticated():
    return HttpResponseRedirect('/home')
    else:
    return HttpResponseRedirect('/welcome')

是模型或其他任何问题。请帮忙。

1 个答案:

答案 0 :(得分:0)

我忘了覆盖settings.py中的默认用户模型。我将AUTH_USER_MODEL的值更改为 myappname.user_model 。现在它工作正常。