django MultiValueDictKeyError error, how do I deal with it
Use the MultiValueDict's get
method. This is also present on standard dicts and is a way to fetch a value while providing a default if it does not exist.
is_private = request.POST.get('is_private', False)
Generally,
my_var = dict.get(<key>, <default>)
MultiValueDictKeyError in Django
Sure, you are not passing username
as a GET
parameter while getting the http://127.0.0.1:8000/StartPage/
page.
Try this and observe username printed: http://127.0.0.1:8000/StartPage?username=test
.
Use get()
and avoid MultiValueDictKeyError
errors:
request.GET.get('username', '')
See also:
- Django request.GET
- Django - taking values from POST request
django returns MultiValueDictKeyError at / 'q'
try this
query = request.GET['q']
query = request.GET.get('q', '') # use get to access the q
The get() method returns the value of the item with the specified key.
How should I deal with a MultiValueDictKeyError?
Change your submit input to have name as submit
, otherwise it will not get into post data.
<input type="submit" value="Place order" class="submit" name="submit" />
Also, the value will be 'Place order'
, not 'place_order'
.
Related Topics
How to Install Python 3.6.5 on My Ubuntu 19.10 That Already Contains Python 3.7.5
Unicode Box Drawing Characters Not Printed in Ruby
Construct Pandas Dataframe from Items in Nested Dictionary
How to Upload File with Python Requests
Crontab Failed to Run Python Script at Reboot
Is There Something Wrong with This Python Code, Why Does It Run So Slow Compared to Ruby
Python Equivalent of Ruby's 'Method_Missing'
Detect Face Then Autocrop Pictures
How to Get the Pythonpath in Shell
Serving a Request from Gunicorn
Error Installing Uwsgi in Virtualenv
How to Convert a String to a Number If It Has Commas in It as Thousands Separators