我想从表格中获取价值' 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
在我的终端中,网址是这样的:
" 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'"
我错过了什么?
答案 0 :(得分:0)
你应该明确&amp;定义你的表单方法;
<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”>
表单元素的method属性告诉Web浏览器如何将表单数据发送到服务器。指定GET值意味着浏览器会将表单内容添加到URL的末尾。这为简单形式提供了许多优点。它允许浏览器缓存表单提交的结果,并且还允许用户在提交表单后为页面添加书签。因此,GET通常用于不需要考虑安全性的简单形式。
答案 1 :(得分:-1)
" GET example.com/main/agname=jesse&amount=200 HTTP/1.1"
您是否在请求网址中看到一件事没有任何'?'
符号将后来的数据表示为get参数。因此,您没有获得django视图请求中的任何数据。
您需要在表单标记中添加method="GET"
,以告知浏览器使用字段数据作为GET参数而不是URL