I encountered a MultiValueDictKeyError while working in Django. The issue arises when trying to logout a user from my website using the following code:
In HTML
</li>
<li class="nav-item mr-3">
<a href="javascript:{document.getElementById('logout').submit()}" class="nav-link">
<i class="fas.fa-sign-out-alt"></i>Logout
</a>
<form action="{% url 'logout'%}" method="POST" id="logout">
{% csrf_token %}
<input type="hidden>
</form>
</li>
Below are the login and logout functions defined in 'accounts/views.py':
def login(request):
# request method
if request.method == 'POST':
#Saving the username and password in variables
username = request.POST['username']
password = request.POST['password']
# Check if the username and password match using the authenticate function
user = auth.authenticate(username=username, password=password)
# If the user is found with the entered credentials, log them in
if user is not None:
auth.login(request, user)
messages.success(request, "Welcome to BTRE.")
return redirect('dashboard')
# If the credentials do not match, display an error message and redirect to the login page
else:
messages.error(request, "Incorrect Username and/or Password. Please enter correct credentials")
return redirect('login')
else:
return render (request, 'accounts/login.html')
def logout(request):
if request.method == 'POST':
auth.logout(request)
messages.success(request,"You are now logged out")
return redirect('index')
Environment:
Request Method: POST Request URL:
Django Version: 3.0.2 Python Version: 3.8.1 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.humanize', 'pages.apps.PagesConfig', 'listings.apps.ListingsConfig', 'realtors.apps.RealtorsConfig', 'accounts.apps.AccountsConfig'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last): File "C:\Users\DD\Desktop\btre\venv\lib\site-packages\django\utils\datastructures.py", line 76, in getitem list_ = super().getitem(key)
During handling of the above exception ('username'), another exception occurred: File "C:\Users\DD\Desktop\btre\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner response = get_response(request) File "C:\Users\DD\Desktop\btre\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\Users\DD\Desktop\btre\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\DD\Desktop\btre\accounts\views.py", line 12, in login username = request.POST['username'] File "C:\Users\DD\Desktop\btre\venv\lib\site-packages\django\utils\datastructures.py", line 78, in getitem raise MultiValueDictKeyError(key)
Exception Type: MultiValueDictKeyError at /accounts/logout Exception Value: 'username'