【问题标题】:How to fix error "invalid literal for int() with base 10: 'goelv' " when trying to capture string尝试捕获字符串时如何修复错误“int() 的无效文字,基数为 10:'goelv'”
【发布时间】:2012-08-14 05:16:10
【问题描述】:

我有以下 urls.py

urlpatterns = patterns('accounts.views',
  url(r'^user/(?P<id>\w+)/$', 'profile'),
)

我有以下views.py

def profile(request, id):
  user = UserProfile.objects.get(user__username=id)
  product = Mattress.objects.filter(owner = id)   #PROBLEM IS IN THIS LINE
  c = RequestContext(request, {
    'action': 'update/',
    'button': 'Update', 
    'profile': user,
    'product': product,
  })
  return render_to_response('registration/user_profile.html', c)

当我输入网址“http://127.0.0.1:8000/user/goelv/”时,id“goelv”被成功捕获。但是,我收到错误 可以追溯到我在行 product=Mattress.objects.filter(owner = id) 中的 views.py。

作为参考,我有以下 models.py,其中所有者字段仅与用户对象相关

class Product(models.Model):
  owner = models.ForeignKey(User, null=True, blank=True)
  title = models.CharField(max_length=50)
  manufacturer = models.CharField(max_length=75)
  product_description = models.TextField(max_length=2000)

谁能看到我做错了什么或如何解决这个问题?感谢您的帮助!

【问题讨论】:

    标签: python django django-models django-views django-urls


    【解决方案1】:

    如果你指的是id,那么它应该是integer而不是字符串。

    不过,这会解决您的问题。

    product = Mattress.objects.filter(owner = user)
    

    注意:在过滤器中使用user 而不是id,因为您已经检索到用户对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 2019-06-15
      • 2019-08-10
      相关资源
      最近更新 更多