英文:
Django edit_post view not prepopulating form fields with existing data
问题
我正在开发一个Django博客应用程序,我有一个用于编辑帖子的视图。但是,当我点击编辑按钮时,表单字段不会显示预填充的现有数据。
这是我的`edit_post`视图代码:
```python
def edit_post(request, post_id):
post = get_object_or_404(Post, id=post_id)
if request.method == "POST":
form = PostForm(request.POST, instance=post)
if form.is_valid():
form.save()
return redirect("main_page")
else:
form = PostForm(instance=post)
context_dict = {
"form": form,
"post": post,
}
return render(request, "blog/edit_post.html", context_dict)
all_posts.html -> 按钮,重定向到编辑帖子页面。
edit_post.html: 表单部分的代码。
我已经确认PostForm正确配置了来自Post模型的必填字段。我还尝试过使用@never_cache
装饰器,但没有解决这个问题。
我不确定为什么在提交表单后表单字段没有预填充现有数据。只有在我手动重新加载页面时才起作用。
对于如何解决这个问题的任何建议或见解,将不胜感激。
<details>
<summary>英文:</summary>
I'm developing a Django blog application and I have a view to edit a post. However, when I click the Edit button, the form fields do not appear prepopulated with the existing data.
Here's my `edit_post` view code:
def edit_post(request, post_id):
post = get_object_or_404(Post, id=post_id)
if request.method == "POST":
form = PostForm(request.POST, instance=post)
if form.is_valid():
form.save()
return redirect("main_page")
else:
form = PostForm(instance=post)
context_dict = {
"form": form,
"post": post,
}
return render(request, "blog/edit_post.html", context_dict)
all_posts.html -> button that redirects to the Edit Post page.
<form method="POST" action="{% url 'edit_post' post.id %}">
{% csrf_token %}
<button type="submit" class="btn btn-secondary me-2 btn-sm">Edit</button>
</form>
edit_post.html: part of the code where the form is.
<form method="POST">
{% csrf_token %}
{{ form.media }}
<div class="mb-3">
<label for="{{ form.title.id_for_label }}" class="form-label">Title</label>
{{ form.title }}
</div>
<div class="mb-3">
<label for="{{ form.summary.id_for_label }}" class="form-label">Summary</label>
{{ form.summary }}
</div>
<div class="mb-3">
<label for="{{ form.image_url.id_for_label }}" class="form-label">Image URL</label>
{{ form.image_url }}
</div>
<div class="mb-3">
<label for="{{ form.body.id_for_label }}" class="form-label">Body</label>
{{ form.body }}
</div>
<div class="mb-3">
<label for="{{ form.tags.id_for_label }}" class="form-label">Tags</label>
{{ form.tags }}
</div>
<button type="submit" class="btn btn-primary">Update Post</button>
</form>
I have verified that the PostForm is correctly configured with the required fields from the Post model. I also tried using the `@never_cache` decorator, but it didn't resolve the issue.
I'm not sure why the form fields are not prepopulated with the existing data after submitting the form. It only works when I manually reload the page.
Any suggestions or insights on how to fix this issue would be greatly appreciated.
</details>
# 答案1
**得分**: 1
尝试以下两种方法来解决这个问题。
检查这段代码,确保你的 post 对象不为空。
```python
post = get_object_or_404(Post, id=post_id)
其次,从上下文中移除 post 对象,它不是必需的。
context_dict = {
"form": form,
}
英文:
Try these two things to solve the issue.
Check this code, your post object should be not null.
post = get_object_or_404(Post, id=post_id)
Second remove post object from context, it's not required.
context_dict = {
"form": form,
}
答案2
得分: 1
好的,“the problem is the button”这部分是指问题出在按钮上,它会将用户带到“编辑帖子视图”。
“But your button strangely is also sending a POST request.”这部分是指但你的按钮却奇怪地也发送了一个POST请求。你需要更改这个。
“This can be just a normal link like this:”这部分是指这可以只是一个普通的链接,就像这样:
“Just because you are passing a parameter to the url does not mean that this has to be a POST request!”这部分是指仅仅因为你向URL传递了参数,并不意味着它必须是一个POST请求!
“What is happening in your case: Your post-edit-view divides its logic into POST and GET requests.”这部分是指在你的情况下发生了什么:你的“编辑帖子视图”将其逻辑分为了POST和GET请求。
“Usually you expect the GET part of the view being triggered when a user "goes" to the edit-view.”这部分是指通常你期望在用户“进入”编辑视图时会触发视图的GET部分。
“Then the form would be populated correctly via PostForm(instance=post)
.”这部分是指然后表单将通过PostForm(instance=post)
正确填充。
“In your case you access the view already with a POST request, so the POST arm of the view gets triggered.”这部分是指在你的情况下,你已经使用了一个POST请求访问了视图,所以视图的POST部分被触发了。
“Here you don't send any data with the request, so this PostForm(request.POST, instance=post)
populates the form with empty data.”这部分是指在这里,你没有在请求中发送任何数据,所以PostForm(request.POST, instance=post)
会用空数据填充表单。
英文:
Ok, the problem is the button which brings the user to the post-edit-view!
You only want to issue a POST request when you want to send data with the request. But your button strangely is also sending a POST request. You have to change that. This can be just a normal link like this:
<a href="{% url 'edit_post' post.id %}" class="btn btn-secondary me-2 btn-sm">
Edit
</a>
Just because you are passing a parameter to the url does not mean that this has to be a POST request!
What is happening in your case: Your post-edit-view divides its logic into POST and GET requests. Usually you expect the GET part of the view being triggered when a user "goes" to the edit-view. Then the form would be populated correctly via PostForm(instance=post)
. In your case you access the view already with a POST request, so the POST arm of the view gets triggered. Here you don't send any data with the request, so this PostForm(request.POST, instance=post)
populates the form with empty data.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论