0

使用HttpResponseRedirectandreverse重定向到另一个视图后,我得到一个 ValueError。这是处理已发布表单的视图(该表单使用 aModelChoiceField从我的Make模型中获取一组下拉选项:

def browse(request):
    thing_list = Thing.objects.all()
    if request.method == 'POST':
        form = BrowseForm(request.POST)
        if form.is_valid():
            make = form.cleaned_data['make']
            return HttpResponseRedirect(reverse('browse_makes', kwargs={'make':make}))
    else:
        form = BrowseForm()
    return render(request, 'browse.html', {'form':form, 'thing_list':thing_list})

.. 然后重定向到下一个视图,添加make为 kwarg 用于过滤下一个查询集:

def makes(request, make):
    thing_list = Thing.objects.filter(make=make)
    return render(request, 'browse-makes.html', {'thing_list':thing_list})

网址:

url(r'^browse/$', 'myapp.views.browse.browse', name='browse'),
url(r'^browse/(?P<make>[\w-]+)/$', 'myapp.views.browse.makes', name='browse_makes'),

ValueError: invalid literal for int() with base 10: 'Samsung'从表单中选择选项时的结果。谷歌搜索后,仍然不知道如何解决这个问题..感谢任何想法如何解决这个错误!


编辑:

缩略型号:

class Thing(models.Model):
    user = models.ForeignKey(User)
    created_on = models.DateTimeField(auto_now_add=True)
    updated_on = models.DateTimeField(auto_now=True)
    slug = models.SlugField()
    make = models.ForeignKey(Make)

追溯:

Traceback: File "/lib/python2.7/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs) File "/myproject/myapp/views/browse.py" in makes
  21.     thing_list = Thing.objects.filter(make=make) File "/lib/python2.7/django/db/models/manager.py" in filter
  143.         return self.get_query_set().filter(*args, **kwargs) File "/lib/python2.7/django/db/models/query.py" in filter
  624.         return self._filter_or_exclude(False, *args, **kwargs) File "/lib/python2.7/django/db/models/query.py" in _filter_or_exclude
  642.             clone.query.add_q(Q(*args, **kwargs)) File "/lib/python2.7/django/db/models/sql/query.py" in add_q
  1250.                             can_reuse=used_aliases, force_having=force_having) File "/lib/python2.7/django/db/models/sql/query.py" in add_filter
  1185.                 connector) File "/lib/python2.7/django/db/models/sql/where.py" in add
  69.             value = obj.prepare(lookup_type, value) File "/lib/python2.7/django/db/models/sql/where.py" in prepare
  320.             return self.field.get_prep_lookup(lookup_type, value) File "/lib/python2.7/django/db/models/fields/related.py" in get_prep_lookup
  137.             return self._pk_trace(value, 'get_prep_lookup', lookup_type) File "/lib/python2.7/django/db/models/fields/related.py" in _pk_trace
  210.         v = getattr(field, prep_func)(lookup_type, v, **kwargs) File "/lib/python2.7/django/db/models/fields/__init__.py" in get_prep_lookup
  310.             return self.get_prep_value(value) File "/lib/python2.7/django/db/models/fields/__init__.py" in get_prep_value
  537.         return int(value)

Exception Type: ValueError at /browse/Samsung/ Exception Value: invalid literal for int() with base 10: 'Samsung'
4

1 回答 1

1

如果您发布您的Make模型,我可以给出完整的答案。但一般来说,您直接引用make外键 - 这是一个 int(MakeID)。您正在将此 int 与导致错误的字符串'samsung'进行比较。make=make

根据您的Make模型具有的字符串字段,您需要引用这些字段之一。例如,如果Make有一个name接受字符串的字段:

thing_list = Thing.objects.filter(make__name=make)

否则,您需要传递makeID 而不是'samsung'

于 2013-01-17T18:37:29.020 回答