0

views.py 是

def when(request):

    if request.method == 'POST':

        reportform = ReportForm(data=request.POST)

        if reportform.is_valid():
            report = reportform.save(commit=False)
            report.save()
            return redirect('/member/media/')
    else:
        reportform = ReportForm()
    return render_to_response('incident/when.html',{
                  'newreport_menu': True, 
                  'form': reportform,
                   },
                   context_instance=RequestContext(request))

我可以在数据库中插入和保存数据。所以保存数据后,如果我再次打开此页面,输入的数据应该处于可编辑模式。即最后输入的数据应该显示在字段中。如何做到这一点。

谢谢

4

2 回答 2

0

instance在表单实例化调用中使用参数。

于 2013-05-03T13:25:55.153 回答
0
def when(request, instance_id=None):

    report = None
    if instance_id:
        report = Report.objects.get(pk=instance_id)

    if request.method == 'POST':

        reportform = ReportForm(data=request.POST, instance=report)

        if reportform.is_valid():
            report = reportform.save(commit=False)
            report.save()
            return redirect('/member/media/')
    else:
        reportform = ReportForm(instance=report)
    return render_to_response('incident/when.html',{
              'newreport_menu': True, 
              'form': reportform,
               },
               context_instance=RequestContext(request))

保留两个 url,一个用于创建,另一个用于编辑,但它们都将使用相同的视图。

url(r'^create/$', 'app.views.when', name='create_report'),
url(r'^edit/(?P<instance_id>\d+)/$', 'app.views.when', name='edit_report')
于 2013-05-03T13:29:59.180 回答