需要使用表单

时间:2018-08-01 18:05:40

标签: django django-models django-forms django-templates django-views

我有一个正在编辑数据库条目并将更改保存回数据库的表单。保存更改后,我想重定向到contract.html,并在其中填充该条目的详细信息。

下面是视图:

def edit_res(request, pk=None):
    if request.method == 'POST':
        resident_form = EditItemForm(request.POST)
        if resident_form.is_valid():
            key = int(request.POST['id'])
            resident = Resident.objects.get(pk=key)
            resident_form = EditItemForm(request.POST, instance=resident)
            resident_form.save()
        #form = EditItemForm(request.POST, instance=key)
        #resident = get_object_or_404(Resident, pk=key)
        #form = EditItemForm(request.POST, instance=key)
        #if form.is_valid():
            #form.save()
            #return HttpResponse('Saved form')
            return redirect('Contract')

    else:
        item = Resident.objects.get(pk=pk)
        form = EditItemForm(instance=item)

    args = {}
    args.update(csrf(request))
    args['form'] = form
    args['id'] = pk
    return render_to_response('reg_form/edit.html', args)

当前,保存已编辑的条目后,我将重定向到contract.html页。

我正在尝试编写一个函数,可以在上面调用该函数以从数据库返回数据:

def res(request, in_res_id):
    data = Resident.objects.get(res_id=in_res_id)
    return TemplateResponse(request, 'contract.html', {'data': data})

我的contract.html就像我要填充res_fname的列值一样:

This skilled nursing facility Admission Agreement is entered into between
Located at and _{{ data.res_fname }}
(hereinafter “Resident”)

我该如何解决?

0 个答案:

没有答案