模板:(index.html)
“
<html>
<body>
<p>请输入两个数字</p>
<form action="/index" method="get">
a: <input type="text" id="a" name="a"> <br>
b: <input type="text" id="b" name="b"> <br>
<p>result: <span id='result'></span></p>
<button type="button" id='sum'>提交</button>
</form>
<!---<script type="text/javascript" src="/static/jquery-3.3.1.min.js"></script>--->
<script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#sum").click(function(){
var a = $("#a").val();
var b = $("#b").val();
$.get("/index",{'a':a,'b':b}, function(ret){
$('#result').html(ret.result)
})
});
});
</script>
</body>
</html>”
**url 代码:**
urlpatterns = [
url(r'testDb' , testDb),
url(r'query' , Query),
url(r'formdemo' , searchtest.search),
url(r'^seachDemo' , searchtest.search_form),
url(r'^index' , view.loginIndex),
]
python 代码view.py:
def loginIndex(request):
if(request.method == 'GET'):
a = int(request.GET.get('a'))
b = int(request.GET.get('b'))
return_json = {'result': a + b}
return HttpResponse(json.dumps(return_json), content_type='application/json')
return render(request,"index.html")
报错:
TypeError at /index
int() argument must be a string, a bytes-like object or a number, not 'NoneType'
Request Method: GET
Request URL: http://127.0.0.1:8000/index
Django Version: 2.0.3
Exception Type: TypeError
Exception Value:
int() argument must be a string, a bytes-like object or a number, not 'NoneType'
Exception Location: D:\PythonDemo\PythonDemo\view.py in loginIndex, line 12
Python Executable: D:\PythonDemo\venv\Scripts\python.exe
Python Version: 3.6.3
Python Path:
['D:\PythonDemo',
'D:\PythonDemo',
'D:\PythonDemo\venv\Scripts\python36.zip',
'X:\python\DLLs',
'X:\python\lib',
'X:\python',
'D:\PythonDemo\venv',
'D:\PythonDemo\venv\lib\site-packages',
'D:\PythonDemo\venv\lib\site-packages\setuptools-28.8.0-py3.6.egg',
'D:\PythonDemo\venv\lib\site-packages\pip-9.0.1-py3.6.egg',
'X:\PyCharm\PyCharm 2017.3.4\helpers\pycharm_matplotlib_backend']
Server time: Sun, 18 Mar 2018 07:35:40 +0000
这个报错也让我很懵逼,我把提交方式,和ajax方法改成post,就没有这个报错,程序可以正常运行