Django表单提交 - 意外行为

时间:2017-01-30 15:55:02

标签: python django with-statement

我在我的一个Django网页上有一个表单,该表单目前显示有关特定项目的预算的信息。预算与安排项目都是数据库中的模型。

每个项目可以有多个预算,但这些预算中只有一个是“当前预算”。

信息的显示方式是,有许多称为“演示文稿”的“图块”图标(每个预算一个)。当选择其中一个“演示”图块时,表单将显示在图块下方,供用户填写有关该演示文稿的信息(注释,参加者,日期等)。列表中的最后一个“图块”图标将始终为空白图块,标题为“添加演示文稿”,用户可以单击该图块为项目添加新演示文稿,它将在现有图块下方显示表单的空白副本

表单包含两个用于上传图片的字段(图片将通过上传到的预算对象与该特定项目相关联。

填写表单并将图像文件添加到表单后,用户应单击表单底部的“提交”按钮将此全部内容上传到数据库。然后,当返回到此页面时,他们应该能够点击该特定演示文稿的“图块”图标,并且应该显示表单,自动填充他们之前输入的信息,以及他们拥有的任何图像文件上传附件。

这当前按预期工作,但是,出于某种原因,当单击表单底部的“上传”按钮,将图像和输入到表单中的其他信息上传到项目预算时,似乎在网页上添加两个空白的“演示”图块......我真的不明白为什么会发生这种情况......

此表单的HTML是:

<form method="POST" enctype="multipart/form-data" data-vat-status="{{project.vat_status}}" data-view-url="{% url 'projects:concept_save_ajax_2' project.id %}" class="autosave_form formset full-width" action="{% url 'projects:upload_budget_pdfs' project.id %}">
    {% csrf_token %}
    <div id="presentations" class="app-wrap center-apps middle">
        {% with get|apps:'Budgets' as costing_app %}
            {% for presentation in presentations %}
                <div id="presentation-{{presentation.id}}" class="app sm {% if presentation.current_marker %}{{costing_app.color}}{% else %}{{app.color}}{% endif %}">
                    <a href="" class="filler"></a>
                    <a class="show-presentation bottom-right" name="presentation-{{presentation.id}}"><img class="icon" src="{% static 'img/edit-white.png' %}"></a>

                    <ul class="flush">
                        <li class=""><h2 class="p-t-lg">Presentation {{forloop.counter}}</h2></li>
                        <li>{{presentation.presentation_date|date:"d M y"|xor}}</li>
                        <li>{{presentation.details|xor|truncatechars:50}}</li>
                        {% if presentation.current_marker %}<li>({% if project.deposit_received%}Deposit{% else %}Current{% endif %} budget)</li>{% endif %}
                    </ul>
                </div>

            {% if forloop.last %}
            {% endif %}

            {% endfor %}
                {# Add a new presentation #}
            <div id="presentation-new" class="app sm {{costing_app.color}} outline">
                <a id="new_presentation" data-view-url="{% url 'projects:save_new_presentation' project.id %}" class="filler show-presentation" name="presentation-new"></a>
                <a name="presentation-new"></a>
                <span class="big-head">+</span>
                <h2 class="no-m">Add presentation</h2></li>
            </div>
        {% endwith %}
    </div>

    <div class="middle">
        {{presentation_formset.management_form}}
        {{drawing_formset.management_form}}

        <div class="col-9 centered-block p-t-lg">
            <table class="left fixed text-sm slim">
        {# New presentation without budget #}
                <tbody>

                </tbody>




        {# Edit presentation details #}
                {% for presentation_form in presentation_formset %}
                    <tbody id="pres{{forloop.counter}}" class="presentation-form" name="presentation-{{presentation_form.instance.id|xor:'new'}}" style="display: none;">


                        {% if not forloop.last and presentation_form.instance.budget_items.count %}
                            <tr class="split-rows">
                                <td colspan="3">Exc VAT {% if not presentation_form.instance.current_marker %}{{presentation_form.instance.grand_total_exc_vat|money:'£'}}{% else %}{{project.budget_overview.updated_exc_vat|money:'£'}}{% endif %}</td>
                                <td colspan="3">Inc VAT {% if not presentation_form.instance.current_marker %}{{presentation_form.instance.grand_total_inc_vat|money:'£'}}{% else %}{{project.budget_overview.updated_inc_vat|money:'£'}}{% endif %}</td>
                            </tr>
                        {% endif %}
                            <tr>
                                {% for hidden in presentation_form.hidden_fields %}
                                    <td class="hidden">{{ hidden }}</td>
                                {% endfor %}
                            </tr>
                            {% for field in presentation_form.visible_fields %}
                                <tr class="split-rows">
                                    {% if not field.name == 'pdf_package_dep' %}
                                        <td colspan="6"><label>{{field.label}}</label></td>
                                    {% endif %}
                                </tr>

                                <tr class="split-rows">

                                    <td colspan="6">
                                        {% if not field.name == 'pdf_package_dep' %}
                                            {% if field.name == 'presentation_date' %}
                                                {% with presentation_form.instance.meeting as meeting %}
                                                    {% include "projects/includes/meeting_bit.html" with employee=request.user.employee meeting=meeting UID=presentation_form.instance.id %}
                                                {% endwith %}
                                                {# <a class="ical_trigger button" data-view-url="{% url 'events:add_to_cal' %}" {% if not field.value %}style="display:none"{% endif %}>Add to calendar</a> #}
                                            {% else %}
                                                {{field}}
                                            {% endif %}
                                        {% endif %}
                                    </td>
                                </tr>
                            {% endfor %}

                            {% if presentation_form.instance.id %}

                                {# PDF uploads #}
                                {% with drawing_form=drawing_formset|getval:forloop.counter0  %}
                                    {# budget_pdf_form=budget_pdf_formset|getval:forloop.counter0 #}
                                    <tr>
                                        {% if not forloop.last %}
                                            <td colspan="3"><label>Budget PDF package</label></td>
                                        {% endif %}

                                        <td colspan="3"><label>Drawings</label></td>
                                    </tr>

                                    <tr>
                                        {% if not forloop.last %}
                                            <td colspan="3" class="center">
                                                {% if presentation_form.instance.pdf_package_dep %}
                                                    <a class="button file-download pdf" href="{% url 'costing:pdf_open' presentation_form.instance.id %}?pdf=package_dep" target="_blank"></a><a class="pdf-clear" data-view-url="{% url 'costing:pdf_clear' presentation_form.instance.id %}?pdf=package_dep"><img class="icon m-l-sm m-b-md" src="{% static "img/bin.png" %}"></a>
                                                {% else %}
                                                    {{presentation_form.pdf_package_dep}}
                                                {% endif %}
                                            </td>       
                                        {% endif %}
                                        {% for d_field in drawing_form.visible_fields %}
                                            {% if drawing_form.instance.pdf %}
                                                <td colspan="3" class="center">
                                                    <a class="button file-download pdf" href="{% url 'costing:pdf_open' presentation_form.instance.id %}?pdf=drawings" target="_blank"></a><a class="pdf-clear" data-view-url="{% url 'costing:pdf_clear' presentation_form.instance.id %}?pdf=drawings"><img class="icon m-l-sm m-b-md" src="{% static "img/bin.png" %}"></a>
                                                    <!--ERF(27/01/2017 @ 1135) Need a hidden field to actually hold the file that's uploaded to the form -->
                                                    <input type="hidden" name = "conceptDrawing" value="{{d_field.title}}">
                                                    <!--ERF(27/01/2017) Need to include the formset in the template -->
                                                    {#{ drawing_formset.as_table }#}
                                                    {{drawing_form.as_table}} 
                                                    {{ drawing_formset.management_form }}
                                                </td>       
                                            {% else %}
                                                <td colspan="3">{{d_field}}</td>
                                                {% for d_hidden in drawing_form.hidden_fields %}
                                                    <td class="hidden">{{d_hidden}}</td>
                                                {% endfor %}
                                            {% endif %}
                                        {% endfor %}
                                        <tr>
                                            <td colspan="1" class="p-t-md"></td>
                                            <td colspan="4" class="p-t-md"><input type="submit" value="upload"></td>
                                            <td colspan="1" class="p-t-md"></td>
                                        </tr>
                                    </tr>
                                {% endwith %}
                            {% endif %}


                        <tr>
                            <td colspan="3">

                                <a class="button email_trigger m-t-md" style="width:auto;" data-view-url="{% url 'comms:open_email_template' project.id %}?template=6&budget={{presentation_form.instance.id}}">Email client meeting report</a>
                            </td>
                        </tr>


                        <tr>
                            <td class="p-t-md">
                                <a {% if forloop.last %}id="refresh_presentations"{% endif %}class="update_presentation button fill">Done</a>
                            </td>
                            <td colspan="2">
                                {% if presentation_form.instance.id and not presentation_form.instance.budget_items.count %}
                                    <a class="button fill" href="{% url 'costing:delete_presentation' presentation_form.instance.id %}">Delete</a>
                                {% endif %}
                            </td>
                        </tr>
                    </tbody>

                {% endfor %}
            </table>
        </div>
    </div>
</form>

以及提交表单时调用的upload_budget_pdfs()视图是:

def upload_budget_pdfs(request, project_id):
    project = Project.objects.get(id=project_id)
    print("Value of project in 'upload_budget_pdfs()': ", project)

    if request.method == 'POST':

    presentations = project.budget_versions.select_related('meeting').prefetch_related('budget_items', 'cci_items', 'presenters').filter(version_number__isnull=False).annotate(vn=F('version_number') * -1).order_by('presentation_date', 'created', '-vn')
    print("Value of presentations in 'upload_budget_pdfs()': ", presentations)
    drawing_formset = DrawingUploadFormset(request.POST, request.FILES, prefix="drawings", queryset=Drawing.objects.filter(budget__in=presentations).order_by('budget__presentation_date', 'budget__created'))

    print("Value of drawing_formset in 'upload_budget_pdfs()': ", drawing_formset)

    if drawing_formset:
        print "Before", [b.id for b in project.budget_versions.all()]

        try:
            for drawing_form in drawing_formset: 
                print 'for loop entered in upload_budget_pdfs() - line 1034'
                print "Value of drawing_form: ", drawing_form
                if drawing_form.instance.budget:
                    print 'if statement entered - line 1036 '
                    print 'Instance', drawing_form.instance.budget
                    drawing = drawing_form.save(commit=False)
                    drawing.budget = drawing_form.instance.budget
                    drawing.save()
                print drawing, [b.id for b in project.budget_versions.all()]
        except Exception as e:
            print '%s (%s)' % (e.message, type(e))
    else: 
        print("Drawing formset not valid. ", drawing_formset.errors)



    budget_formset = BudgetPresentationFormset(request.POST, request.FILES, instance=project, prefix="presentations")

    try:
        if budget_formset.is_valid() and budget_formset.has_changed():
            print("if budget_formset.is_valid() statement entered (line 1063) ")
            #updated_budget_presentations = budget_formset.save()
            budget_formset.save()
            print("Value of update_budget_presentations: ", updated_budget_presentations)
        elif budget_formset.has_changed(): print 'Budget formset not valid.',budget_formset.errors
    except Exception as e:
        print '%s (%s)' % (e.message, type(e))

    return HttpResponseRedirect(reverse('projects:concept', args=[project_id]))

如果附有“PDF套餐”(即使用第一个“上传图片”按钮添加到表单中的图片),则只有在提交表单时才会将空白的“演示”图块添加到页面中...它们是如果在没有附加“PDF包”的情况下提交表单,则不会添加到页面中 - 无论是否附加了“绘图”PDF(即使用第二个'上传图像添加到表单中的图像) '按钮)。

HTML的部分用于上传“PDF包”的图像&amp;表格的“图纸”是:

{% with drawing_form=drawing_formset|getval:forloop.counter0  %}
    {# budget_pdf_form=budget_pdf_formset|getval:forloop.counter0 #}
    <tr>
        {% if not forloop.last %}
            <td colspan="3"><label>Budget PDF package</label></td>
        {% endif %}

        <td colspan="3"><label>Drawings</label></td>
    </tr>

    <tr>
        {% if not forloop.last %}
            <td colspan="3" class="center">
                {% if presentation_form.instance.pdf_package_dep %}
                    <a class="button file-download pdf" href="{% url 'costing:pdf_open' presentation_form.instance.id %}?pdf=package_dep" target="_blank"></a><a class="pdf-clear" data-view-url="{% url 'costing:pdf_clear' presentation_form.instance.id %}?pdf=package_dep"><img class="icon m-l-sm m-b-md" src="{% static "img/bin.png" %}"></a>
                {% else %}
                    {{presentation_form.pdf_package_dep}}
                {% endif %}
            </td>       
        {% endif %}
        {% for d_field in drawing_form.visible_fields %}
            {% if drawing_form.instance.pdf %}
                <td colspan="3" class="center">
                    <a class="button file-download pdf" href="{% url 'costing:pdf_open' presentation_form.instance.id %}?pdf=drawings" target="_blank"></a><a class="pdf-clear" data-view-url="{% url 'costing:pdf_clear' presentation_form.instance.id %}?pdf=drawings"><img class="icon m-l-sm m-b-md" src="{% static "img/bin.png" %}"></a>

                    <input type="hidden" name = "conceptDrawing" value="{{d_field.title}}">

                    {#{ drawing_formset.as_table }#}
                    {{drawing_form.as_table}} 
                    {{ drawing_formset.management_form }}
                </td>       
            {% else %}
                <td colspan="3">{{d_field}}</td>
                {% for d_hidden in drawing_form.hidden_fields %}
                    <td class="hidden">{{d_hidden}}</td>
            {% endfor %}
            {% endif %}
        {% endfor %}
        <tr>
            <td colspan="1" class="p-t-md"></td>
            <td colspan="4" class="p-t-md"><input type="submit" value="upload"></td>
            <td colspan="1" class="p-t-md"></td>
        </tr>
    </tr>
{% endwith %}

我想知道我是否需要使用该行:

{# budget_pdf_form=budget_pdf_formset|getval:forloop.counter0 #}

作为本部分HTML顶部的{% with ... %}语句的一部分?但我不知道如何同时使用with两个不同的论点......?这可能吗?

1 个答案:

答案 0 :(得分:0)

你可以做一些测试。尝试修改你的formset ..说一个额外的形式和零额外的形式,看看会发生什么。