django ValueError: invalid literal for int() with base 10: ''

为君一笑 提交于 2019-12-31 03:54:13

问题


I have an issue with my site, receiving this error pretty often. It's the first time i've run into this and maybe someone can shed some light as to why?

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: ''

Here is my view.

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,}

回答1:


count is the empty string. You need to compensate for the possibility that count could be a non-integer string.




回答2:


You need to check URL in your template. you need to pass the integer id to url {{user.id}} because url need to have integer value in template. Ex. url:- /polls/{{user.id}}/

And in template first check your getting id or not like anywhere in template type sdhajdhgsjgd {{user.id}} kejhksdbjf and check your getting the integer value or not.

Hope this will work for others.



来源:https://stackoverflow.com/questions/5349378/django-valueerror-invalid-literal-for-int-with-base-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!