我要使用django来创建待办事项列表...我做了一些代码...但是它抛出了一个multivaluekeyerror
我尝试了c = request.POST.get('content',False) 但它总是给出False值
views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from .models import TodoItem
# Create your views here.
def home(request):
return render(request, 'home.html')
def work(request):
all_todo_items = TodoItem.objects.all()
return render(request, 'work.html', {'all_items': all_todo_items})
def addTodo(request):
c = request.POST['content']
new_item = TodoItem(content = c)
new_item.save()
return HttpResponseRedirect('/work/')
def deleteTodo(request, todo_id):
item_to_delete = TodoItem.objects.get(id=todo_id)
item_to_delete.delete()
return HttpResponseRedirect('/work/')
work.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="{% static 'css/work.css' %}">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<link rel="icon" href="{% static 'images/list.png' %}" type="image/png">
<title>Tasks</title>
</head>
<body>
<div class="container justify-content-center wrap1">
<div class="text-center heading">
<p><u>write your everyday task here!!!<u></p>
</div>
</div>
<ul style="list-style: none; color: #1b0573; font-weight: bold;" class="text-center">
{% for todo_item in all_items %}
<li>
<div class="row">
<div class="col-sm-6">
{{ todo_item.content }}
</div>
<div class="col-sm-2">
{{ todo_item.date_created }}
</div>
<div class="col-sm-1">
<form action="/deleteTodo/{{ todo_item.id }}" method="post" style="display: inline;">
{% csrf_token %}
<div class="form-group">
<button class="btn btn-outline-danger"><i class="fas fa-trash"></i></button>
</div>
</form>
</div>
</li>
{% endfor %}
</ul>
<div class="container">
<div class="row">
<div class="col-sm-11">
<form action="/addTodo/" method="post">
{% csrf_token %}
<div class="form-group">
<input type="text" class="form-control" placeholder="write your task" name="content">
</div>
</form>
</div>
<div class="col-sm-1">
<form action="/addTodo/" method="post">
{% csrf_token %}
<div class="form-group">
<button class="btn btn-outline-success"><i class="fas fa-plus"></i></button>
</div>
</form>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('work/', views.work, name = 'work'),
path('addTodo/', views.addTodo, name = 'work'),
path('deleteTodo/<int:todo_id>/', views.deleteTodo, name = 'work'),
]
我没有期待任何错误...但是它引发了一个multivaluekeyerror
提高MultiValueDictKeyError(key) django.utils.datastructures.MultiValueDictKeyError:'内容'
答案 0 :(得分:0)
您需要首先提交代码中包含<form>
的{{1}},以便在<input name="content">
中可以使用view
`。试试这个:
request.POST["conten"] to get value of