如何在 Django 中显示我的模板中保存的 3 个表单的数据?

发布于 2025-01-11 08:16:59 字数 1255 浏览 0 评论 0原文

假设我有一个 form_1 (PresupuestosClientesForm) 和 form_2 (PresupuestosVehiculosForm),这些表单存储信息并且它们位于不同的 html 页面中。我想要一个视图,在同一模板中显示保存在表单 1 和 2 中的信息。我怎样才能做到这一点?

保存的数据

根据我的观点,我只检索表单,而不检索表单view.py

def step7(request):

    presupuestosclientesform=PresupuestosClientesForm(request.POST or None,request.FILES or None)
    presupuestosvehiculosform=PresupuestosVehiculosForm(request.POST or None,request.FILES or None)

    if request.method == 'POST':
        pass
    if presupuestosclientesform.is_valid():
        presupuestosclientesform.save()
        return redirect('presupuestos:index')
    return render(request,'Presupuestos/new-estimate-7-preview.html',{'presupuestosclientesform':presupuestosclientesform,'presupuestosvehiculosform':presupuestosvehiculosform})


index.html

<div class="text-muted">
    <p class="mb-1"><i class="uil uil-envelope-alt me-1"></i> 
       {{presupuestosclientesform}}
    </p> 
</div>
<div class="text-muted">
    <p class="mb-1"><i class="uil uil-envelope-alt me-1"></i> 
       {{presupuestosvehiculosform}}
    </p> 
</div>

let's say I have a form_1 (PresupuestosClientesForm) and form_2 (PresupuestosVehiculosForm), those forms store information and they are in different html pages. I would like to have a view that presents the information saved in forms 1 and 2 in the same template. How can I do that?

With my view i am only retrieving the form but not the saved data of the form

view.py

def step7(request):

    presupuestosclientesform=PresupuestosClientesForm(request.POST or None,request.FILES or None)
    presupuestosvehiculosform=PresupuestosVehiculosForm(request.POST or None,request.FILES or None)

    if request.method == 'POST':
        pass
    if presupuestosclientesform.is_valid():
        presupuestosclientesform.save()
        return redirect('presupuestos:index')
    return render(request,'Presupuestos/new-estimate-7-preview.html',{'presupuestosclientesform':presupuestosclientesform,'presupuestosvehiculosform':presupuestosvehiculosform})


index.html

<div class="text-muted">
    <p class="mb-1"><i class="uil uil-envelope-alt me-1"></i> 
       {{presupuestosclientesform}}
    </p> 
</div>
<div class="text-muted">
    <p class="mb-1"><i class="uil uil-envelope-alt me-1"></i> 
       {{presupuestosvehiculosform}}
    </p> 
</div>

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手长情犹 2025-01-18 08:16:59

因此,要使用 Django 表单编辑数据,我们首先需要使用要编辑的数据预填充这些表单,然后将它们作为字典传递给 Django 模板。你可以用这种方式做到这一点...

def step7(request):
    instance1 = get_object_or_404(ModelName, id=value) #Fetch data that you want to change from the model according to your needs.
    instance2 = get_object_or_404(ModelName, id=value) #Fetch data that you want to change from the model according to your needs.
    
    if request.method == 'POST':
       form1 = AddForm(request.POST)
       if form.is_valid():
          form.save()
          return HttpResponseRedirect('url_name')    
    
    else:
      first_form = AddForm(instance=instance1) #Here is the change
      second_form = AddForm(instance=instance2) #Here is the change
      return render(request, 'TemplateName', {'first_form ': first_form, 'second_form': second_form  })

So to use Django forms for editing data, we first need to prepopulate those forms with the data that we want to edit then pass them as dictionaries to Django templates. You can do that in this way...

def step7(request):
    instance1 = get_object_or_404(ModelName, id=value) #Fetch data that you want to change from the model according to your needs.
    instance2 = get_object_or_404(ModelName, id=value) #Fetch data that you want to change from the model according to your needs.
    
    if request.method == 'POST':
       form1 = AddForm(request.POST)
       if form.is_valid():
          form.save()
          return HttpResponseRedirect('url_name')    
    
    else:
      first_form = AddForm(instance=instance1) #Here is the change
      second_form = AddForm(instance=instance2) #Here is the change
      return render(request, 'TemplateName', {'first_form ': first_form, 'second_form': second_form  })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文