django编辑模型表单不起作用

时间:2015-11-21 23:51:23

标签: python django django-orm

我试图通过传递模型实例来制作编辑模型表格但是它不会更新数据..

这是代码:

- - - - - - - - views.py

def edit_form(request,student_usn):
    student=Student.objects.get(usn=student_usn)
    if request.method == "POST":
        form=StudentForm(request.POST,instance=student)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect("/thanks/")
    else:
        form=StudentForm(instance=student)
    return render_to_response('edit_form.html',{"form":form},context_instance=RequestContext(request))

--------- --------- edit_form.html

<html>
    <title>Edit form</title>
    <head><h1>Edit Student</h1></head>
    <body>
        <form action="." method="post">
            {% csrf_token %}
            {{form.as_table}}
            <input type="submit" value="Update">            
        </form>
    </body>
</html>

----- urls.py -

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^index/$', login,{'template_name': 'index.html'}),
    url(r'^profile/$',profile),
    url(r'^logout/$', logout),
    url(r'^profile/(.+)$',edit_form)


]

现在,一切正常......但数据没有更新.. !!

1 个答案:

答案 0 :(得分:0)

您的表单操作转到/thanks/,但您的方法是edit_form。您应该更改指向edit_form的网址的操作。