【发布时间】:2018-08-01 13:42:29
【问题描述】:
我正在尝试将 todolist 项目的 id 传递给我的 html 代码,以便在我编辑 todolist 项目并单击提交时一切正常。
在views.py中:
def edit(request, id):
todo = Todo.objects.get(id=id)
context = {
'todo': todo
}
if(request.method == 'POST'):
title = request.POST['title']
text = request.POST['text']
todo.title = title
todo.text = text
todo.save()
return redirect('/todos')
else:
return render(request, 'edit.html', context)
在 urls.py 中:
url(r'^details/(?P<id>\w{0,50})/edit/$', views.edit, name='edit')
在 HTML 中:
<h2>Edit Todo- {{ todo.title }}</h2>
<form action="{% url 'edit' request.todo.id %}" method="post">
{% csrf_token %}
<label for="title">Title</label> <br/>
<input type="text" name="title" id="title"/>
<br>
<label for="text">Text</label> <br/>
<textarea type="text" name="text" id="text"></textarea>
<br><br>
<input type="submit" value="Submit"/>
</form>
我认为问题出在我的 html 代码中的 url。我不知道如何传递我的 todolist 项的 id 参数。
我不断收到的错误是
invalid literal for int() with base 10: ''
请帮忙
【问题讨论】:
-
只需 {% url 'edit' todo.id %} 在表单操作中,或将其留空
标签: python html django pycharm