如何修复TypeError:期望字符串或类字节对象?

时间:2017-07-02 17:34:48

标签: django django-views

当我提交时,这个错误就会爆炸,谁可以提供帮助,欢迎,兄弟们!)

堆栈问我添加更多文字,但要添加什么?如果一切都被告知了?

但要添加什么?如果一切都被告知了?

错误与日期有关

  

fulltraceback

> Internal Server Error: /accounts/reset_activation_key/ Traceback (most
> recent call last):   File
> "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\core\handlers\exception.py",
> line 41, in inner
>     response = get_response(request)   File "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\core\handlers\base.py",
> line 187, in _get_response
>     response = self.process_exception_by_middleware(e, request)   File "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\core\handlers\base.py",
> line 185, in _get_response
>     response = wrapped_callback(request, *callback_args, **callback_kwargs)   File "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\newstudio\accounts\views.py",
> line 140, in reset_activation_key
>     profile.save()   File "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\db\models\base.py",
> line 806, in save
>     force_update=force_update, update_fields=update_fields)   File "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\db\models\base.py",
> line 836, in save_base
>     updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)   File
> "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\db\models\base.py",
> line 903, in _save_table
>     forced_update)   File "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\db\models\base.py",
> line 953, in _do_update
>     return filtered._update(values) > 0   File "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\db\models\query.py",
> line 664, in _update
>     return query.get_compiler(self.db).execute_sql(CURSOR)   File "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\db\models\sql\compiler.py",
> line 1191, in execute_sql
>     cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)   File
> "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\db\models\sql\compiler.py",
> line 863, in execute_sql
>     sql, params = self.as_sql()   File "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\db\models\sql\compiler.py",
> line 1157, in as_sql
>     val = field.get_db_prep_save(val, connection=self.connection)   File
> "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\db\models\fields\__init__.py",
> line 770, in get_db_prep_save
>     prepared=False)   File "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\db\models\fields\__init__.py",
> line 1459, in get_db_prep_value
>     value = self.get_prep_value(value)   File "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\db\models\fields\__init__.py",
> line 1438, in get_prep_value
>     value = super(DateTimeField, self).get_prep_value(value)   File "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\db\models\fields\__init__.py",
> line 1296, in get_prep_value
>     return self.to_python(value)   File "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\db\models\fields\__init__.py",
> line 1399, in to_python
>     parsed = parse_datetime(value)   File "C:\Users\P.A.N.D.E.M.I.C\Desktop\td11\lib\site-packages\django\utils\dateparse.py",
> line 94, in parse_datetime
>     match = datetime_re.match(value) TypeError: expected string or bytes-like object

views.py

def reset_activation_key(request):
    if request.user.is_authenticated():
        return redirect('/accounts/logout')
    if request.method   == "POST":
        form                    = ResetActivatioKey(request.POST or None)
        if form.is_valid():
            email               = form.cleaned_data['email']
            user                = User.objects.get(email=email)
            profile             = UserProfile.objects.get(user=user)
            if profile.is_active:
                return redirect('/accounts/login')
            if profile is not None and profile.is_active == False :
                username        = user.username
                get_secret_key  = activation_key_generator(username)
                print(get_secret_key)
                profile.activation_key = get_secret_key
                profile.key_expires = (timezone.now() + datetime.timedelta(days=2)),
                profile.save()

                send_some_email(email, username, get_secret_key)
                return redirect('/account/register/')
    form = ResetActivatioKey()
    context = {"form":form}
    return render(request, 'accounts/registration/reset_activation_key.html', context)

1 个答案:

答案 0 :(得分:3)

以下一行导致您遇到麻烦

profile.key_expires = (timezone.now() + datetime.timedelta(days=2)),

您将key_expires设置为元组而不是字符串。将其更改为

profile.key_expires = timezone.now() + datetime.timedelta(days=2)

同时从上述行末尾删除逗号。