如何创建分页项目的详细信息视图

时间:2019-04-26 12:25:12

标签: django python-3.x url view pagination

我有一个应用程序,我在其中测试打印机并经历一系列测试步骤。测试步骤中,我一次使用分页1个步骤。因此,您单击“下一步”调出下一步。问题是,我没有创建详细视图。我只有一个称为测试步骤的视图,可以单击下一步以浏览整个查询集。问题是,我无法单击以转到一个特定的测试,或者在不返回第一步的情况下暂停测试。我试图创建一个详细视图并拆分视图。但是我无法弄清楚如何通过分页使每个步骤成为一个详细视图。

这是测试详细信息视图和页面。从这里,我单击启动测试,这将使我进入下面的视图中的分页步骤。

def test_detail(request, id):
    test_instance = get_object_or_404(Test, id=id)
    steps = test_instance.teststep_set.all()
    printer_status_check = printer_check(test_instance) 
    status = printer_status_check['status']
    context = {
        "test": test_instance,
        "steps": steps,
        'printer_status': status
    }

    return render(request, "testing/test_detail.html", context)

因此,这是通过所有步骤控制所有功能的视图。我必须使用html表单,因为我无法弄清楚如何将表单传递给下一步。另外,我必须在不同的步骤使用不同的表单,因为我使用Ajax到达了不同的端点,或者根本没有端点。

所以问题出在测试详细信息页面上,我可以看到所有带有结果的步骤。但是我无法单击某个步骤并转到分页查询集中的特定步骤。

所以我没有详细步骤。当我尝试时,我不知道如何单击“下一步”,它会弹出该步骤的下一个详细视图。

def test_step(request, id):
    test_instance = get_object_or_404(Test, id=id)
    steps = test_instance.teststep_set.all().order_by('id')

    paginator = Paginator(steps, 1)
    page_request_var = 'page'   
    page = request.GET.get(page_request_var)

    try:
        paginated_queryset = paginator.page(page)
    except PageNotAnInteger:
        paginated_queryset = paginator.page(1)
    except EmptyPage:
        paginated_queryset = paginator.page(paginator.num_pages)

    if request.is_ajax():
        step = paginated_queryset[0]
        if step.action == 'PrintRequest':   
            response = print_card(test_instance)
            print(response)
            ExtraResponseData = response['ExtraResponseData']
            JobID = response['JobID']
            StatusMessage = response['StatusMessage']
            success = response['Success']
            JobComplete = response["JobComplete"]
            Message = response["Message"]
            PrintStatus = response["PrintStatus"]
            step_notes = request.POST.get("step_notes")
        #-----------------------------
            update_step = TestStep.objects.get(id=step.id)
            data = {
                "ExtraResponseData": ExtraResponseData,
                "JobID": JobID,
                "StatusMessage": StatusMessage,
                "success": success,
                "JobComplete": JobComplete,
                "Message": Message,
                "PrintStatus": PrintStatus
                }   
            update_step.value = Message
            update_step.response_body = data
            update_step.notes = step_notes
            pass_or_fail = evaluate(Message, step)
            update_step.passed = pass_or_fail
            update_step.save()
            return JsonResponse(data)

        else:
            response = printer_check(test_instance)
            status = response['status']
            code = response['code']
            body = response['body']
            step_notes = request.POST.get("step_notes")
        #-----------------------------
            update_step = TestStep.objects.get(id=step.id)
            update_step.value = status
            update_step.response_code = code
            update_step.response_body = body
            update_step.notes = step_notes
            pass_or_fail = evaluate(status, step)
            update_step.passed = pass_or_fail
            update_step.save()
            data = {
                "status": status,
                "code": code,
            }   
            return JsonResponse(data)
    else:
        status = None
        code = None
        body = None
        success = None
        if request.method == 'POST':
            step = paginated_queryset[0]
            update_step = TestStep.objects.get(id=step.id)
            step_notes = request.POST.get("step_notes")
            update_step.notes = step_notes
            if request.POST.get("NO"):
                update_step.value = "No"
                update_step.passed = False
            elif request.POST.get("YES"):
                update_step.value = "Yes"
                update_step.passed = True
            else:
                update_step.value = "OK"
                update_step.passed = True
            update_step.save()
        #-----------------------------

    context = {
        "test": test_instance,
        "steps": steps,
        "qs": paginated_queryset,
        "page_request_var": page_request_var,
        "status": status,
        "code": code,
        "body": body,
        "success": success
    }

    return render(request, "testing/test_steps.html", context)

因此在步骤模板中。我单击此处,将显示下一步。好吧,我希望该步骤成为详细信息视图或达到该步骤的其他方法。

<section class="section-padding">
    <div class="container-fluid">
        {% for step in qs %}
                {% if step.test_step_name == 'Attempt Print' %}
                        <a class="pull-right btn btn-primary" href="{{ test.get_absolute_url }}">Finish Test</a>
                {% else %}
                        <a class="pull-right btn btn-default" href="{{ test.get_absolute_url }}">Pause Test</a>
                {% endif %}
        {% endfor %}
            {% for step in qs %}
                    <div class="step-container">
                        <div class="step-description">
                                <h1>{{ step.test_step_name }}</h1>
                                <p>{{ step.description }}</p>

                        </div>
                                {% if step.user_defined == False %}
                                        <form id="{% if step.ajax == True %}step_form{% endif %}" action="" method="POST">
                                                {% csrf_token %}
                                                <input type="submit" value="OK" class="btn btn-primary">
                                                <textarea name="step_notes"rows="4" cols="70" class="step_notes mt-5 form-control" placeholder="Take notes to associate with this test step..."></textarea>
                                        </form>
                                {% else %}
                                <form id="{% if step.ajax == True %}step_form{% endif %}" action="" method="POST">
                                        {% csrf_token %}
                                        <input type="submit" value="No" name="NO" class="btn btn-warning">
                                        <input type="submit" value="Yes" class="btn btn-success" name="YES">
                                        <textarea name="step_notes"rows="4" cols="70" class="step_notes mt-5 form-control" placeholder="Take notes to associate with this test step..."></textarea>
                                </form>
                                {% endif %}
                    </div>
            {% endfor %}

            <div class="ajaxProgress">
                <img src="{% static 'img/30.gif' %}">
            </div>


        </div>
        <nav aria-label="Page navigation example">
                <ul class="pagination pagination-template d-flex justify-content-center">
                  {% comment %} {% if qs.has_previous %}                
                        <li class="page-item">
                          <a href="?{{ page_request_var }}={{ qs.previous_page_number }}" class="page-link">
                             <i class="fa fa-angle-left"></i>
                          </a>
                        </li>
                  {% endif %} {% endcomment %}
                       <li class="page-item">
                          <a href="?{{ page_request_var }}={{ qs.number }}" class="page-link active">
                                  {{ qs.number }}
                          </a>
                      </li>
                  {% if qs.has_next %}
                     <li class="page-item">
                        <a href="?{{ page_request_var }}={{ qs.next_page_number }}" class="page-link"> 
                           Next <i class="fa fa-angle-right"></i>
                        </a>
                     </li>
                  {% endif %}
                </ul>
        </nav>



        <div class="log-container">
                {% for step in steps %} 
                        {% if step.value is not None %}
                                {% if step.passed is True %}
                                <h6 class="step_pass_value"><strong>{{ forloop.counter }}</strong>.<em> {{ step.value }}</em><i class="fas fa-check"></i></h6>
                                {% else %}
                                <h6 class="step_fail_value"><strong>{{ forloop.counter }}</strong>.<em> {{ step.value }}</em><i class="fas fa-times"></i></h6>
                                {% endif %}
                        {% endif %}
                {% endfor %}

                {% if status is not None %}
                        <div> 
                           <h6><strong>{{ status }}</strong><h6>
                        </div>
                {% elif success is not None %}
                        <div> 
                           <h6><strong>{{ success }}</strong><h6>
                        </div>
                {% endif %}


        </div>
    </section>

我尝试在此模板上创建一个详细视图并创建一个steps块,当您单击下一步时,它将通过steps block template标记传递下一步。尽管我无法弄清楚如何单击下一步,但它引用了详细视图中的步骤,而不引用了test_steps视图中的分页查询集中的步骤。

0 个答案:

没有答案