简体   繁体   中英

invalid literal for int() with base 10 in django

I'm trying to make a profile page with an <owner> argument in the link the owner in the models is a ForeignKey to the user but with any value i submit it saids invalid literal for int() with base 10:

models.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,
    )

views.py

def my_profile(request, owner):
    template='my_profile.html' 
    context = {
        'profile' : get_object_or_404(UserCommunityProfile, owner=owner),
    }
    return render(request, template, context)

TraceBack

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'

url

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

owner field is ForeignKey which is stored as integer in DB by default. But you are trying to pass string value raouf as owner value to get_object_or_404 method. You need to change lookup argument to owner__username if you want to get user by username:

get_object_or_404(UserCommunityProfile, owner__username=owner)

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