Skip to main content
 首页 » 编程设计

python之GET 方法返回不同的错误值

2025年05月04日8落叶

我想使用“GET”方法从表单中获取值并使用它执行操作。现在,我的 html 是这样的:

<form action="{% url 'myman' %}"> 
    <input type='hidden' name='agname' maxlength='300' value='{{mac.user.username}}' /> 
    <input  type='text'  name='amount' maxlength='300' required /> 
    <input type="submit" value="Send" /> 
</form> 

View 是这样的:

@login_required 
def myman(request): 
 
    yoname=request.GET.get('agname') 
    print yoname  # did this to see the value in terminal, debugging purposes 
    damount = request.GET.get('amount') 
    print damount  # did this to see the value in terminal, debugging purposes 
 
    requested_decimal_amount=Decimal(damount) 
    # other code follows 
    return redirect('dashboard') 

我收到此错误无法将 None 转换为 Decimal

在我的终端里,url是这样的:

" GET example.com/main/agname=jesse&amount=200 HTTP/1.1" 
None 
None 

我将 views.py 函数更改为:

@login_required 
def myman(request): 
 
    yoname=request.GET['agname'] 
    print yoname  # did this to see the value in terminal, debugging purposes 
    damount = request.GET['amount'] 
    print damount  # did this to see the value in terminal, debugging purposes 
 
    requested_decimal_amount=Decimal(damount) 
    # other code follows 
    return redirect('dashboard') 

我遇到了这个错误:

MultiValueDictKeyError at /main/ 
 
"'agname'" 

在终端我得到:

" GET example.com/main/agname=jesse&amount=200 HTTP/1.1" 
jesse 
200 

对于第二个函数,它返回了值,但我不明白为什么它会给我一个错误!

关于错误的更多信息:

Request Method:     GET 
Request URL:    http://127.0.0.1:8000/main/ 
Django Version:     1.11.5 
Exception Type:     MultiValueDictKeyError 
Exception Value:     
"'agname'" 
 
Exception Location:     C:\Python27\Scripts\env\lib\site-packages\django\utils\datastructures.py in __getitem__, line 85 
Python Executable:  C:\Python27\Scripts\env\Scripts\python.exe 
Python Version:     2.7.11 

在控制台中

[10/Oct/2017 20:57:13] "GET /main/ HTTP/1.1" 500 84654 
jesse 
200 
[10/Oct/2017 21:04:30] "GET /main/?agname=jesse&amount=200 HTTP/1.1" 302 0 
Internal Server Error: /main/ 
Traceback (most recent call last): 
      File "C:\Python27\Scripts\env\lib\site- 
   packages\django\core\handlers\exception.py", line 41, in inner 
  response = get_response(request) 
  File "C:\Python27\Scripts\env\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response 
  response = self.process_exception_by_middleware(e, request) 
 File "C:\Python27\Scripts\env\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response 
   response = wrapped_callback(request, *callback_args, **callback_kwargs) 
  File "C:\Python27\Scripts\env\lib\site-packages\django\contrib\auth\decorators.py", line 23, in _wrapped_view 
   return view_func(request, *args, **kwargs) 
  File "C:\Python27\Scripts\env\Scripts\mana\manamainapp\views.py", line 456, in myman 
    yoname=request.GET['agname'] 
  File "C:\Python27\Scripts\env\lib\site-packages\django\utils\datastructures.py", line 85, in __getitem__ 
     raise MultiValueDictKeyError(repr(key)) 
     MultiValueDictKeyError: "'agname'" 

我错过了什么?

请您参考如下方法:

您应该明确并定义您的表单方法;

<form action="{% url 'myman' %}" method="get"> 

这样,你的第一种方法获取参数就正确了;

yoname = request.GET.get('agname', 'default value') 

别忘了,get() 的第二个参数如果键不在字典中,则提供默认值。

不过如果你真的喜欢request.GET['key']尝试确保键存在,因为显式引用字典键会导致 KeyError如果它不存在,并且您的错误 ( MultiValueDictKeyError) 是它的变体;

if request.GET.get('q'): 
    q = request.GET['q'] 

我可能会这样写;

@login_required 
def myman(request): 
 
    if 'agname' in request.GET: 
        yoname = request.GET.get('agname') 
    if 'amount' in request.GET: 
        damount = request.GET.get('amount') 
        requested_decimal_amount=Decimal(damount) 
 
    return redirect('dashboard') 

读一读; https://html.com/attributes/form-method/

<form method=”GET”>

The method attribute of the form element tells the web browser how to send form data to a server. Specifying a value of GET means the browser will add the form contents to the end of the URL. This offers a number of advantages for simple forms. It allows the browser to cache the results of the form submission, and it also allows the user to bookmark the page once the form has been submitted. As such, GET is generally used for simple forms where security is not a concern.