django ValueError: 无法将 '' 转换为 int()
我在我的网站上遇到了一个问题,最近经常收到这个错误信息。
这是我第一次碰到这种情况,也许有人能帮我解释一下为什么会这样?
Traceback (most recent call last):
File "/opt/python2.6/lib/python2.6/site-packages/django/core/handlers/base.py", line 92, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/www/django_test1/fundedbyme/project/views.py", line 194, in browse
items = Project.objects.filter(categories__slug=cat_name, status='AP')[:count]
File "/opt/python2.6/lib/python2.6/site-packages/django/db/models/query.py", line 151, in __getitem__
stop = int(k.stop)
ValueError: invalid literal for int() with base 10: ''
这是我的视图。
def browse(request, template_name='projects/browse.html'):
cat_name = request.GET.get('category', None)
city_name = request.GET.get('city', None)
count = request.GET.get('count','12')
if cat_name is not None:
items = Project.objects.filter(categories__slug=cat_name, status='AP')[:count]
category = get_object_or_None(Category, slug=cat_name)
if city_name is not None:
items = Project.objects.filter(location_slug=city_name, status='AP')[:count]
category = Project.objects.filter(location_slug=city_name, status='AP')[:1]
category = category[0].location
total = items.count()
new_count = int(count) + 12
context = {'items':items,'cat_name':category,'total':total,'new_count':new_count,}
2 个回答
0
你需要在你的模板里检查一下网址(URL)。
你需要把整数类型的用户ID传递给网址,比如用{{user.id}},因为网址在模板中需要这个整数值。
例如,网址可以是:/polls/{{user.id}}/
在模板中,首先要检查一下你是否能获取到这个ID。你可以在模板的任何地方输入一些文字,比如sdhajdhgsjgd {{user.id}} kejhksdbjf,然后看看你能不能得到这个整数值。
希望这个方法对其他人也有帮助。
3
count
是一个空字符串。你需要考虑到 count
可能是一个非整数的字符串。