admin管理员组文章数量:1389768
'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.
'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.
Share Improve this question asked Mar 17 at 7:31 REDAREDA 13 bronze badges 2 |3 Answers
Reset to default 0The 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!
本文标签: Django MultiValueDictKeyError 39category39 When Submitting FormStack Overflow
版权声明:本文标题:Django MultiValueDictKeyError: 'category' When Submitting Form - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744575120a2613576.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Form
s. – willeM_ Van Onsem Commented Mar 17 at 7:43