Django:ModelForm 的 save() 方法未更新数据库

发布于 2024-11-09 11:31:09 字数 1739 浏览 0 评论 0原文

我在保存和更新数据库值时遇到问题。

查看:

def code_prof_edit(request, ampCode):
    user_code = get_object_or_404(CodeUser, pk=ampCode)
    if request.method =='POST':
        form = CodeUserForm(request.POST, instance=user_code)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/codes/' + user_code.ampCode)
    else:
        form = CodeUserForm(instance=user_code)
        return render_to_response('edit_code_user.html', {'user_code':user_code, 'form':form})

相关模板表单标签:

<form action="." method="POST"> {% csrf_token %}
    {{form.as_p}}
    <input type="submit" value="Save" />
</form>

使用 post 方法,表单可以在浏览器中正确呈现,并预先填充所有原始值,但如果我更改值并单击提交,则不会进行任何更改。一切似乎都正常工作,因为用户被重定向到正确的页面,这意味着必须已调用保存,但键入的信息未保存。任何和所有的帮助将不胜感激。

编辑整个视图功能:

from django.shortcuts import render_to_response, get_object_or_404
from django.http import *
from django.template import RequestContext
from codeuser.models import CodeUser
from codeuser.forms import CodeUserForm
import pdb

def code_prof_edit(request, ampCode):
    user_code = get_object_or_404(CodeUser, pk=ampCode)
    if request.method =='POST':
        pdb.set_trace()
        form = CodeUserForm(request.POST, instance=user_code)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/codes/' + user_code.ampCode)
        else:
            return render_to_response('home.html')
    else:
        form = CodeUserForm(instance=user_code)
        return render_to_response('edit_code_user.html', {'user_code':user_code, 'form':form})

编辑:搞清楚了。在html表单标签中,action属性不正确。 “。”不起作用,我需要“./edit”才能获得正确的结果。此外,排除该属性也非常有效。

I'm having a problem with saving and updating a value to my database.

View:

def code_prof_edit(request, ampCode):
    user_code = get_object_or_404(CodeUser, pk=ampCode)
    if request.method =='POST':
        form = CodeUserForm(request.POST, instance=user_code)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/codes/' + user_code.ampCode)
    else:
        form = CodeUserForm(instance=user_code)
        return render_to_response('edit_code_user.html', {'user_code':user_code, 'form':form})

Relevant template form tag:

<form action="." method="POST"> {% csrf_token %}
    {{form.as_p}}
    <input type="submit" value="Save" />
</form>

Using the post method, the form correctly renders in the browser with all of the original values pre-populated, but if I change the values and click submit, none of the changes are made. Everything seems to work correctly in that the user is redirected to the correct page which means the save must have been called, but the info typed isn't being saved. Any and all help would be appreciated.

Edit entire view function:

from django.shortcuts import render_to_response, get_object_or_404
from django.http import *
from django.template import RequestContext
from codeuser.models import CodeUser
from codeuser.forms import CodeUserForm
import pdb

def code_prof_edit(request, ampCode):
    user_code = get_object_or_404(CodeUser, pk=ampCode)
    if request.method =='POST':
        pdb.set_trace()
        form = CodeUserForm(request.POST, instance=user_code)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/codes/' + user_code.ampCode)
        else:
            return render_to_response('home.html')
    else:
        form = CodeUserForm(instance=user_code)
        return render_to_response('edit_code_user.html', {'user_code':user_code, 'form':form})

Edit: Figured it out. In the html form tag, the action attribute was incorrect. "." wasn't working, I needed "./edit" to get the correct result. Also, excluding that attribute all together worked perfectly too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文