djang中以10为基数的int()的文本无效

2024-10-03 23:28:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在链接中使用<owner>参数创建一个配置文件页面模型中的所有者是用户的ForeignKey 但我提交的任何值都是invalid literal for int() with base 10:

模型.py

owner = models.ForeignKey(
        User,
        related_name="user_profile_user_key",
        verbose_name="owner of the profile",
        blank=False,
        null=False,
        on_delete = models.CASCADE,
    )

视图.py

^{pr2}$

回溯

Traceback:

File "C:\Users\Raouf\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\Users\Raouf\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\Raouf\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\Raouf\PycharmProjects\drop\community\views.py" in my_profile
  112.         'profile' : get_object_or_404(UserCommunityProfile, owner=owner),

File "C:\Users\Raouf\AppData\Local\Programs\Python\Python36\lib\site-packages\django\shortcuts.py" in get_object_or_404
  87.         return queryset.get(*args, **kwargs)

File "C:\Users\Raouf\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\query.py" in get
  394.         clone = self.filter(*args, **kwargs)

File "C:\Users\Raouf\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\query.py" in filter
  836.         return self._filter_or_exclude(False, *args, **kwargs)

File "C:\Users\Raouf\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
  854.             clone.query.add_q(Q(*args, **kwargs))

File "C:\Users\Raouf\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\sql\query.py" in add_q
  1253.         clause, _ = self._add_q(q_object, self.used_aliases)

File "C:\Users\Raouf\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\sql\query.py" in _add_q
  1277.                     split_subq=split_subq,

File "C:\Users\Raouf\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\sql\query.py" in build_filter
  1215.         condition = self.build_lookup(lookups, col, value)

File "C:\Users\Raouf\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\sql\query.py" in build_lookup
  1085.         lookup = lookup_class(lhs, rhs)

File "C:\Users\Raouf\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\lookups.py" in __init__
  18.         self.rhs = self.get_prep_lookup()

File "C:\Users\Raouf\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\related_lookups.py" in get_prep_lookup
  115.                 self.rhs = target_field.get_prep_value(self.rhs)

File "C:\Users\Raouf\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
  947.         return int(value)

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

网址

path('profiles/<owner>', community_views.my_profile, name='profile'),

Tags: djangoinpygetmodelslibpackageslocal
1条回答
网友
1楼 · 发布于 2024-10-03 23:28:46

owner字段是ForeignKey,默认情况下以整数形式存储在DB中。但您试图将字符串值raouf作为所有者值传递给get_object_or_404方法。如果要按用户名获取用户,则需要将查找参数更改为owner__username

get_object_or_404(UserCommunityProfile, owner__username=owner)

相关问题 更多 >