0

所以我基本上有这个代码:

@render_to('hello/home.html')
def home(request):
    info = Info.objects.get(pk=1)
    if request.method == "POST":
        form = InfoForm(request.POST, instance=info)
    else:
         form = InfoForm(instance=info)
   return {"info": info, "form": form}

aaand 它不像我想象的那样工作。如果我只使用模型实例或 POST 初始化表单,它会起作用,但两者都不起作用。有没有一种(不错的?)方法可以使用从模型实例填充的数据创建表单,并使用 request.POST 中的数据对其进行更新?

4

1 回答 1

1

您正在编写的代码已经在框架中。

网址.py

from django.views.generic import UpdateView
from myapp.forms import InfoForm
urlpatterns = patterns('',
    url(r'^info/(?P<pk>[-_\w]+)/update/$', UpdateView.as_view(model= Info,template_name="hello/home.html",form_class=InfoForm), name='info_update'), 
)
# you might need to include a success url in the **kwargs i.e. success_url="/thankyou/"
于 2013-01-03T20:06:55.173 回答