Django raising ValueError: invalid literal for int() with base 10 on POST

Django is consistently throwing a ValueError on every POST request to the server. From cruising around forms and SE, it seems like this an issue with my Django models. However, I'm not sure why the models would throw this error or why they would be called by Django since this is thrown in a POST event.

View in question:

def channel(request, channel):
    user_form = weblog_userForm(request.POST or None)
    if request.method == 'POST' and user_form.is_valid():
        nickname = user_form.cleaned_data['nickname']
        message = user_form.cleaned_data['message']
        password = user_form.cleaned_data['password']
        postDetails = {}

        ... process request and eventually ...

        return HttpResponse(json.dumps(postDetails), content_type="application/json")

My application models:

class Line(models.Model):
    message = models.TextField(blank=True, default="")
    nick = models.CharField(max_length=50)
    hostname = models.CharField(max_length=300)
    channel = models.CharField(max_length=200)
    timestamp = models.DateTimeField(auto_now_add=True)
    LINE_TYPES = (
        ('PMSG', 'PRIVMSG'),
        ('SMSG', 'SOCKETMSG'),
        ('WMSG', 'WEBMSG'),
        ('NTCE', 'NOTICE'),
        ('ACTN', 'ACTION'),
        ('JOIN', 'JOIN'),
        ('PART', 'PART'),
        ('QUIT', 'QUIT'),
        ('NICK', 'NICK'),
        ('TPIC', 'TOPIC'),
    )
    msgType = models.CharField(max_length=4, choices=LINE_TYPES, default='PRIVMSG')

    def __str__(self):
        return self.message

class banned_ips(models.Model):
    bannedIp = models.GenericIPAddressField()

Traceback:

Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/django/weblogs/log/views.py", line 96, in channel
    json_data = serializers.serialize("json", list(reversed(Line.objects.filter(id__gt=latest_line_id, channel=channel).order_by('-id')[:100])))
File "/usr/local/lib/python3.5/dist-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 836, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 854, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py", line 1252, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py", line 1276, in _add_q
    split_subq=split_subq,
File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py", line 1214, in build_filter
    condition = self.build_lookup(lookups, col, value)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py", line 1084, in build_lookup
    lookup = lookup_class(lhs, rhs)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/lookups.py", line 18, in __init__
    self.rhs = self.get_prep_lookup()
File "/usr/local/lib/python3.5/dist-packages/django/db/models/lookups.py", line 68, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 947, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: ''

Any help or ideas would be appreciated, really scratching my head on this one.


Both guillermo chamorro and pocket kings found the catch for me here. In a (seemingly unrelated) block of logic under that posted in my views.py snip, is the following code:

if (request.is_ajax()):
    latest_line_id = request.GET.get('latest_id', '')
    if latest_line_id == '-1': # Return all lines
        json_data = serializers.serialize("json", list(reversed(Line.objects.filter(channel=channel).order_by('-id')[:100])))
    else:
        json_data = serializers.serialize("json", list(reversed(Line.objects.filter(id__gt=latest_line_id, channel=channel).order_by('-id')[:100])))
    return HttpResponse(json_data, content_type="application/json")

Despite logically never being called, both reponsders are correct that the ValueError failure is stemming from Django nevertheless attempting to evaluate json_data .

If latest_line_id is blank (as it would be in a POST request), it will be set to '' , which will trigger a ValueError by Django when attempting a db query based on this id. This can be proven by trying to query your filters yourself in the Django shell (by using $python manage.py shell ):

(note: if using the django shell, don't forget to import your models with something like from <yourapp>.models import <yourModel> )

>>> list(Line.objects.filter(channel="test").filter(id__lte='').order_by('id'))
Traceback (most recent call last):
...
ValueError: invalid literal for int() with base 10: ''

Lesson learned, be very diligent when editing your views and remember to double check all your logic blocks and be careful when reading your traceback (an extra pair of eyes can help ;) ). Hopefully my oversight can save someone else the time I lost.

链接地址: http://www.djcxy.com/p/86480.html

上一篇: 线程和Python中的线程

下一篇: Django引发ValueError:对于int()无效的字面量,POST上的基数为10