form.save()不起作用我的form.save用于更新数据不起作用 [英] form.save() not working my form.save for updating data isnt working

查看:215
本文介绍了form.save()不起作用我的form.save用于更新数据不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

form.save()不起作用我的form.save用于更新数据在我尝试更新我的帖子时不起作用,它显示了原始未编辑的帖子,它没有保存更新的版本。我不知道是什么原因导致了错误idk如果它的观点或模板中的任何内容(如果有人可以帮助我,我将非常感激,请提供帮助)。

form.save() not working my form.save for updating data isnt working when i try to update my post it shows the original unedited post it dosent save the updated version.i donnt know whats causing the error idk if its views or anything in template if anyone could help i will be very grateful please help.

这里是代码:


views.py

views.py


from django.shortcuts import render
from django.shortcuts import HttpResponseRedirect
from .models import Post
from .forms import PostForm


def post_list(request):
    posts = Post.objects.all()

    context = {
        'post_list': posts
    }
    return render(request, "posts/post_list.html", context)


def post_detail(request, post_id):
    post = Post.objects.get(id=post_id)
    context = {
        'post': post
    }
    return render(request, "posts/post_detail.html", context)


def post_create(request):
    form = PostForm(request.POST or None)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect('/posts')
    context = {
        "form": form,
        "form_type": 'Create'
    }
    return render(request, "posts/post_create.html", context)


def post_update(request, post_id):
    post = Post.objects.get(id=post_id)
    form = PostForm(request.POST or None, instance=post)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect('/posts')
    context = {
        "form": form,
        "form_type": 'Update'
    }
    return render(request, "posts/post_update.html", context)


def post_delete(request, post_id):
    post = Post.objects.get(id=post_id)
    post.delete()
    return HttpResponseRedirect('/posts')



urls.py

urls.py


from django.urls import path
from .views import post_list, post_detail, post_create, post_update, post_delete


urlpatterns = [
    path('', post_list),
    path('create/', post_create),
    path('<post_id>/', post_detail),
    path('<post_id>/update', post_update),
    path('<post_id>/delete', post_delete),
]



post_update.html

post_update.html


<h1>welcome to post {{ form_type }}</h1>

<form method="POST" action=".">
    {% csrf_token %}
    <p>{{ form.as_p }}</p>
    <button type="submit">{{ form_type }}</button>
</form>


推荐答案

action ='。 " 带您从< post_id> /更新< post_id> / 。您可以通过以下几种方法解决此问题:

action="." takes you from <post_id>/update to <post_id>/. You can fix this a few ways:


  1. 将其更改为 action = 将从< post_id> / update 提交到< post_id> / update

  2. 在URL中添加斜线,即 path('< post_id> / update /',...)。然后 action = .. 将从< post_id> / update / 提交到< post_id> / update /

  3. 使用 {%url%} 标记代替硬编码那个行动。在您的情况下,您需要进行一些更改,因此,这对您来说是一个挑战。 反向URL 上的文档应该会有所帮助。

  1. Change it to action="", which will submit from <post_id>/update to <post_id>/update.
  2. Add a slash to the URL, i.e. path('<post_id>/update/', ...). Then action="." will submit from <post_id>/update/ to <post_id>/update/
  3. Use the {% url %} tag instead of hardcoding the action. In your case there's a few changes you'd need to make, so I'll leave that as a challenge for you. The docs on reversing URLs should help.

这篇关于form.save()不起作用我的form.save用于更新数据不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆