为什么我的代码显示此错误,因为我的整个代码是正确的?“list”对象没有属性“_meta”views.pyfrom django.shortcuts import render, redirect, get_list_or_404from .models import Taskfrom .forms import TaskFormdef update_task(request, pk): up_task = get_list_or_404(Task, id=pk) if request.method == 'POST': up_form = TaskForm(request.POST, instance=up_task) if up_form.is_valid(): up_form.save() return redirect('tasks') else: up_form = TaskForm(instance=up_task) context = { 'update_task_form': up_form } return render(request, 'task/update_task.html', context)urls.pyfrom django.urls import pathfrom . import viewsurlpatterns = [ path('', views.tasks, name='tasks'), path('task/<str:pk>/update/', views.update_task, name='update_task'),]
1 回答
浮云间
TA贡献1829条经验 获得超4个赞
使用get_object_or_404
--(Django 文档)而不是get_list_or_404
from django.shortcuts import get_object_or_404
# and in your view
up_task = get_object_or_404(Task, id=pk)
更新
将您的模板更新为,
<form action="{% url 'tasks' %}" method="POST">
{% csrf_token %}
{{ update_task_form }}
</form>
<a type="submit" href="">Update</a>
添加回答
举报
0/150
提交
取消