Django MultiValueDictKeyError: 'category' When Submitting Form
'm working on a Django To-Do app, and I'm trying to implement an edit feature where users can modify a task's category and importancy.
I have an edit.html template that contains a form, and when I submit it, I get the following error:
django.utils.MultiValueDictKeyError: 'category'
Code Details
edit.html (Template with Form)
{% block content %}
<h2>Edit Task</h2>
<form action="{% url 'todo:edit' task.id %}" method="post">
{% csrf_token %}
<input
type="text"
name="category"
value="{{ task.category }}"
/>
<input
type="text"
name="importancy"
value="{{ task.importancy }}"
/>
<input type="submit" value="Edit">
</form>
{% endblock content %}
views.py
from django.shortcuts import render, redirect, get_object_or_404
from .models import Task
def edit(request, task_id):
task = get_object_or_404(Task, id=task_id)
if request.method == 'POST':
print(request.POST) # Debugging to check form data
task.category = request.POST['category'] # This line causes the error
task.importancy = request.POST['importancy']
task.save()
return redirect('todo:index')
return render(request, 'todo/edit.html', {'task': task})
I checked my form to make sure the name="category" is correct, and it matches what I’m trying to access in request.POST. Double-checked the form method to ensure it's POST and not GET. Looked at my URLs to see if anything there could be interfering, but it seems fine.
The django.utils.MultiValueDictKeyError error occurs when we try to access a key in a dictionary that doesn't exist.
Try one of the below option to first check if key is exists or not, if exists then get the value of that key.
task.category = request.POST.get('category', task.category)
OR
if 'category' in request.POST:
task.category = request.POST['category']
Instead of directly accessing the value through ["category"]
, use get instead so whenever a missing value is presented this will return None.
task.category = request.POST.get('category')
please refer to this
The issue was in the index.html file, specifically with the edit button that redirects to the edit page. The problem was caused by the form's method being set to POST
instead of GET
, which resulted in unexpected behavior.
index.html
{% block content %}
<form method="POST"> <!-- This should be GET -->`
{% csrf_token %}
<table border = 1>
<thead>
<th style="padding: 5px 10px;">Task</th>
<th style="padding: 5px 10px;">Category</th>
<th style="padding: 5px 10px;">Importancy</th>
<th style="padding: 5px 10px;">Date added</th>
</thead>
<tbody>
{% for task in tasks %}
<tr>
<td style="padding: 5px 10px;">{{ task.task_name }}</td>
<td style="padding: 5px 10px;">{{ task.category }}</td>
<td style="padding: 5px 10px;">{{ task.importancy }}</td>
<td style="padding: 5px 10px;">{{ task.date_added }}</td>
<td style="padding: 5px 10px;">
<button name='edit' type='submit' formaction= '{% url "todo:edit" task.id %}'>edit</button>
<button >delete</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</form>
After correcting it, everything now works as expected.
I sincerely apologize for the confusion and for any time wasted on this. I'm still new to this field and actively learning, so I truly appreciate everyone's patience and support. Thank you for your help!