我一直在尝试创建待办事项列表应用程序,直到添加添加编辑按钮,按下编辑按钮时它都没有问题,它显示了带有必须编辑的文本的编辑页面,但是应该用来更改数据库的提交按钮不起作用,我想我需要在views.py文件中添加一些内容,但是我不知道什么。
def edit(request, id):
created_items = Create.objects.get(id=id)
return render(request, 'my_app/edit.html', {"created_items": created_items})
urlpatterns = [
path('edit/<int:id>/', views.edit, name='edit'),
]
class Create(models.Model):
added_date = models.DateTimeField()
text = models.CharField(max_length=200)
{% extends 'index.html' %}
{% block body %}
<div align="center">
<h1>Edit your post!</h1>
<div class="container form-group">
<h1>↓</h1>
<form method="POST">{% csrf_token %}
<textarea class="form-control" name="content" id="id" rows="3" style="text-align: center;">{{ created_items.text }}</textarea>
<button type="submit" class="btn btn-outline-success" style="margin-top: 5px;">Submit</button>
</form>
</div>
</div>
{% endblock %}
答案 0 :(得分:0)
当您单击提交时,您将发送POST请求,并且需要使用以下功能来捕获该请求:
if request.method == 'POST':
edited_text = request.POST.get('text') ## 'text' might be changed. I don't know how you handle the request.
related_id = request.POST.get('id') ## You need to take updated id. (somehow?)
Create.objects.filter(id=related_id).update(text=edited_text) ## That will do the job.
希望有帮助,