这篇文章主要讲解了“Django报错ValueError: invalid literal for int() with base 10的原因及解决方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Django报错ValueError: invalid literal for int() with base 10的原因及解决方法”吧!
DJango报错
ERROR:default:Traceback (most recent call last): File "/data/venv4archer/lib/python3.6/site-packages/django/core/handlers/base.py", line 132, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/data/venv4archer/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view return view_func(*args, **kwargs) File "/data/opt/archer_product_20181019/sql/views.py", line 1123, in modifydatasourceapply applyusername=datasourceapply.objects.filter(id=int(Datasourceapplyid)).values('user_name')ValueError: invalid literal for int() with base 10: ''
报错原因:
post传送过来的变量为空。
Datasourceapplyid = request.POST.get("datasourceid", "") print ('Datasourceapplyid', Datasourceapplyid) ValueError: invalid literal for int() with base 10: ''[26/Oct/2018 18:39:10] "POST /modifydatasourceapply/ HTTP/1.1" 500 12987Datasourceapplyid
解决方法:
修复post变量传入。
报错时,var datasourceid = $("#datasource_id").val();写成了var datasourceid = $("#datasourceid").val();,进而导致传输的变量为空值。
<form method="post" role="form"> {% csrf_token %} <div class="form-group"> <input type="hidden" class="form-control" id="datasource_id" name="datasourceid" value={{ datasourceDetail.id }} > </div> $("#btn-submitsql").click(function () { var datasourceid = $("#datasource_id").val(); var app_name = $("#app_name").val(); var env = $("#env").val(); var db_name = $("#db_name").val(); var db_privs = $("#db_privs").val(); var username = $("#username").val(); var port = $("#port").val(); var password = $("#password").val(); var ip_addr = $("#ip_addr").val(); $.ajax({ type: "post", async: false, url: "/modifydatasourceapply/", dataType: "json", data: { datasourceid: datasourceid, app_name: app_name, env: env, db_name: db_name, db_privs: db_privs, username: username, port: port, password: password, ip_addr: ip_addr },
感谢各位的阅读,以上就是“Django报错ValueError: invalid literal for int() with base 10的原因及解决方法”的内容了,经过本文的学习后,相信大家对Django报错ValueError: invalid literal for int() with base 10的原因及解决方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!