检索数据以在Django中填充表

时间:2019-07-30 22:17:01

标签: python django

我有一个视图,该视图呈现一个下拉菜单,用户可以在其中选择一个订单号并将其定向到另一个视图,在该视图中应该有一个表,该表中填充有与该订单#相关的所有数据。

每个订单#都具有与其相关的多个记录-我不确定如何正确获取并显示这些数据。

这是我正在尝试的-但是我一直在出错,而且我觉得我一开始就没有正确地做到这一点。

更新以显示错误: 'unicode'对象没有属性'_meta'    形式= CreateManifestForm(request.POST,instance = referenceid)

views.py

#rendering the dropdown
def manifest_reference_view(request):
    query_results = Orders.objects.all()
    reference_list = BlahBlah()

    context = {
        'query_results': query_results,
        'reference_list': reference_list,
    }
    return render(request, 'manifest_reference_view.html', context)

#this view should show table with all data relating to user selection
def manifest(request):
    if request.method == 'POST':

        reference = request.POST.get('Reference_Nos')
        referenceid = reference
        form = CreateManifestForm(request.POST, instance=referenceid)
        manifest = Manifests.objects.all().filter(reference__reference=referenceid)
        #order = Orders.objects.get(reference=reference)
        if form.is_valid():
            form.save()
            return redirect('display_orders')

    else:
        form = CreateManifestForm(instance=referenceid)

        return render(request, 'edit_manifest_frombrowse.html', {'form': form})

forms.py

class BlahBlah(forms.Form):
    Reference_Nos = forms.ModelChoiceField(queryset=Orders.objects.values_list('reference', flat=True).distinct(),
    empty_label=None)

class CreateManifestForm(forms.ModelForm):

    class Meta:
        cases = forms.IntegerField(widget=forms.TextInput(attrs={'placeholder': 'Enter Cases'}))
        model = Manifests
        fields = ('reference', 'cases', 'product_name', 'count', 'CNF', 'FOB')

edit_manifest_frombrowse.html

{% extends 'base.html' %}

{% block body %}

  <div class="container">
    <form id="create_mani_form" method="POST">

      <br>
      <br>
      <br>
      {% csrf_token %}

        <div class="column">
          <label for="form.reference" class="formlabels">Reference ID: </label><br>
          <!-- <input type="text" value="{{ reference_id }}">-->
           {{ form.reference }}
          <br>
        </div>
        <div class="description">
        <div class="column">
          <label for="form.description" class="formlabels">Description: </label>
          <br>
          {{ form.product_name}}
        </div>
        </div>
        <div class="column">
          <label for="form.cases" class="formlabels">Cases: </label>
          <br>
          {{ form.cases }}
          <br>
        </div>
        <div class="column">
          <label for="form.count" class="formlabels">Count: </label>
          <br>
          {{ form.count }}
          <br>
          <br>
        </div>
          <br>
          <br>


      <button type="submit" name="add_mani" style="border-color: #7395AE;">Add Line</button>
    </form>


    <br>
    <h4>Manifest</h4>

    <div class="table-responsive">
    <!--<table  id="manifest_table" class="table table-striped table-bordered table-sm " cellspacing="0"-->
    <table class="table table-striped table-bordered manifest_table" cellspacing="0" style="width="100%">

      <thead>
        <tr>
          <th style="width:2%;"</th>
          <th style="width:10%;">Ref ID</th>
          <th style="width:10%;">Cases</th>
          <th style="width:60%;">Description</th>
          <th style="width:10%;">Count</th>
        </tr>
      </thead>

      <tbody>

        {% for manifests in manifests %}

        <tr>
          <td>{{ manifests.reference }}</td>
          <td>{{ manifests.cases }}</td>
          <td>{{ manifests.product_name}}</td>
          <td>{{ manifests.count}}</td>
        </tr>

        {% endfor %}

      </tbody>
    </table>
    </div>
    <div class="text-center">
      <a href="{% url 'display_orders' %}" type="submit" class="btn btn-primary" name="button" align="right" style="color: #7395AE; border-color: #7395AE; background-color: #FFF;">Subit Manifest</a>
    </div>



{% endblock %}

无论用户在Reference_Nos下拉列表中选择什么,都应从清单模型中获取与该选择有关的数据,然后将其显示在表中。

0 个答案:

没有答案