【问题标题】:How to pass to view a form and a queryset in Django?如何在 Django 中查看表单和查询集?
【发布时间】:2021-05-29 14:08:03
【问题描述】:

我还是菜鸟和 Django,我没有遇到这个问题。

我正在尝试向我的所有客户发送一个表单和一个查询集,但是查询集没有返回。

我在 trabalhos\views.py 中执行此操作,查询集来自 clientes\models 中的模型

谁能帮帮我?这真是让我头疼。

谢谢

def novotrabalho_view(request, *args, **kwargs):
    if request.method == 'POST':
        form = novoTrabalho(request.POST)
        if form.is_valid():
            codigo = form.cleaned_data['codigo']
            nome = form.cleaned_data['cliente']
            paciente = form.cleaned_data['paciente']
            dispositivo = form.cleaned_data['dispositivo']
            tecnico_responsavel = form.cleaned_data['tecnico_responsavel']
            

            c = trabalho(codigo=codigo, cliente=nome, paciente=paciente, dispositivo=dispositivo, tecnico_responsavel=tecnico_responsavel)
            c.save()



    form = novoTrabalho()
    listaclientes = cliente.objects.all()
    

    return render(request, "novotrabalho.html", {'form': form, 'listaclientes': listaclientes} )

【问题讨论】:

  • “未返回查询集”是什么意思?
  • 我无法在我的视图中迭代查询集,数据不存在。感谢您的回复。
  • clients吗?你能检查数据库是否有cliente 记录,它为print(listaclientes) 打印什么?
  • 当然可以。我有几个客户。如果我将 listaclientes 作为 HttpResponse 返回,我得到:cliente 对象 (1)cliente 对象 (2)cliente 对象 (3)cliente 对象 (4)cliente 对象 (5)cliente 对象 (6)
  • 到视图或模板。你在这里分享了观点。模板是novotrabalho.html

标签: python django forms view model


【解决方案1】:

发现错误...在视图中...我的 for 循环是:{ % for c in listaclientes %} 我将问题更改为:{% for c in listaclientes %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-05
    • 2010-11-01
    • 1970-01-01
    • 2011-06-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多