【问题标题】:GET Method Returns Different Errors With ValuesGET 方法返回不同的错误值
【发布时间】:2017-10-10 19:21:03
【问题描述】:

我想用 '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>

意见是这样的:

@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')

我收到此错误Cannot convert None to 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'"

我错过了什么?

【问题讨论】:

  • 在你的 html 表单元素中不应该是 method='get' 吗?
  • 即使没有说明方法,默认方法也是get。我在某处读过它
  • 没有重复!我已经说明了原因。

标签: python django


【解决方案1】:

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

<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/

&lt;form method=”GET”&gt;

form 元素的method 属性告诉Web 浏览器如何将表单数据发送到服务器。指定 GET 值意味着浏览器会将表单内容添加到 URL 的末尾。这为简单的表格提供了许多优点。它允许浏览器缓存表单提交的结果,并且它还允许用户在表单提交后为页面添加书签。因此,GET 通常用于不考虑安全性的简单表单。

【讨论】:

  • @YoYo 好的,发布您的完整堆栈跟踪,否则我们只是在猜测。您使用的是request.GET['agname'] 还是request.GET.get('agname')
  • 我都测试过,我得到了上面列出的不同错误。
  • 发布您的堆栈跟踪。
  • 不,堆栈跟踪是调用列表、文件(包括行号)和执行的行。基本上您在错误上方的控制台中看到的内容。请张贴。
  • 谢谢。现在我仍然相信这是您使用request.GET['key'] 而不是request.GET.get('key', 'default') 这就是您的问题。我已经更新了我的答案来解释。另外,请参见此处; stackoverflow.com/questions/23531030/…
【解决方案2】:
" GET example.com/main/agname=jesse&amount=200 HTTP/1.1" 

您是否在请求 URL 中看到一件事,没有任何 '?' 符号将后面的数据表示为获取参数。所以你在 django 视图的请求中没有得到任何数据。

您需要在表单标签中添加method="GET",这将告诉浏览器使用字段数据作为GET参数而不是URL

【讨论】:

  • 您能告诉我您提交表单时的请求网址吗?
  • 我将在其中输入值的 url 是 example.com/thebuyer/,它有一个模板 Buyer.html 采用上述表单属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2020-08-10
  • 2016-02-01
  • 1970-01-01
相关资源
最近更新 更多