我在执行代码:-
MultiValueDictKeyError at /count/ 'textbox' Request Method: GET Request URL: http://127.0.0.1:8000/count/ Django Version: 2.1 Exception Type: MultiValueDictKeyError Exception Value: 'textbox' Exception Location: /Users/rajans/anaconda3/lib/python3.7/site-packages/django/utils/datastructures.py in getitem, line 79 Python Executable: /Users/rajans/anaconda3/bin/python Python Version: 3.7.0 Python Path: ['/Users/rajans/Documents/djangoprojects/wordcount', '/Users/rajans/anaconda3/lib/python37.zip', '/Users/rajans/anaconda3/lib/python3.7', '/Users/rajans/anaconda3/lib/python3.7/lib-dynload', '/Users/rajans/anaconda3/lib/python3.7/site-packages', '/Users/rajans/anaconda3/lib/python3.7/site-packages/aeosa'] Server time: Tue, 5 Feb 2019 06:39:06 +0000
Python
from django.http import HttpResponse
from django.shortcuts import render
def homepage(request):
return render(request,'home.html')
def contact(request):
return HttpResponse("<h2> This is a contact_us page</h2><br> You can write to rajan.sharma@freshworks.com for any query")
def count(request):
data=request.GET['textbox']
data_list=split(data)
data_len=len(data_list)
return render(request,'count.html',{'length':data_len})
--------
在count.html
<h1>Counted</h1><br/>
The length is :- {{length}}
看起来您没有将教科书变量作为参数传递到URL中。在
作为您的URL/尝试调用URL?教科书=“您要传递的值”
另外,建议使用.get方法作为
您使用的是http://127.0.0.1:8000/count/这个url,当这个url调用函数时,它将与函数中的GET方法一起使用。您试图访问“textbox”,但在第一次访问textbox参数时,它显示错误
您需要修改视图函数并编写一个条件
使用MultiValueDict的get方法,如果
textbox
不存在,也可以提供默认值。在相关问题 更多 >
编程相关推荐