I want to get values from a form with 'GET' method and perform an operation with it. Now, my html is like this:
<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>
The views is like this:
@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')
I got this error Cannot convert None to Decimal
In my terminal, the url is like this:
" GET example.com/main/agname=jesse&amount=200 HTTP/1.1"
None
None
I changed my views.py
function to this:
@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')
I got this error:
MultiValueDictKeyError at /main/
"'agname'"
In the terminal I got:
" GET example.com/main/agname=jesse&amount=200 HTTP/1.1"
jesse
200
For the second function, it returned the values but I don't get why it's giving me an error!
More on the error:
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
In the console
[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'"
What am I missing?