2017-01-30 1 views
0

J'ai un formulaire sur l'une de mes pages Web Django, qui affiche actuellement des informations sur les budgets pour un projet particulier. Budget & Project sont les deux modèles dans la base de données.Soumission de formulaire Django - comportement inattendu

Chaque projet peut avoir plusieurs budgets, mais seul l'un de ces budgets sera le «budget actuel».

La façon dont l'information est affichée, c'est qu'il y a un certain nombre d'icônes de «tuiles» appelées «Présentations» (une pour chaque budget). Lorsque l'une des mosaïques «Présentation» est sélectionnée, un formulaire s'affiche sous les mosaïques, pour que l'utilisateur puisse renseigner les informations relatives à cette présentation (notes, qui participe, date, etc.). La dernière icône «mosaïque» de la liste sera toujours une mosaïque vierge, intitulée «Ajouter une présentation», sur laquelle l'utilisateur peut cliquer pour ajouter une nouvelle présentation pour le projet. Elle affichera une copie vierge du formulaire sous les mosaïques existantes. .

Le formulaire comprend deux champs pour le téléchargement d'images (les images sont associées à ce projet particulier à travers l'objet budgétaire qu'ils sont téléchargés.

Une fois que le formulaire a été rempli, et les fichiers d'image ajoutés à Dans le formulaire, l'utilisateur doit cliquer sur le bouton "Soumettre" au bas du formulaire pour télécharger tout ceci dans la base de données, puis, en revenant sur cette page, il doit pouvoir cliquer sur l'icône "vignette" pour cette présentation. , et le formulaire devrait être affiché, rempli automatiquement avec les informations qu'ils avaient entrées précédemment, et avec tous les fichiers d'image qu'ils ont téléchargés attachés à celui-ci

fonctionne actuellement comme prévu, cependant, pour une raison quelconque, lorsque vous cliquez sur le bouton "Télécharger" au bas du formulaire, pour télécharger les images et autres informations saisies dans le formulaire pour le budget du projet, il semble ajouter deux autres tuiles « Présentation » à la page web ... et je ne comprends vraiment pas pourquoi ce qui se passe ...

le code HTML pour ce formulaire est:

<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> 

et la vue upload_budget_pdfs() qui est appelée lorsque le formulaire est soumis est:

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])) 

T Les tuiles «Présentation» vierges ne sont ajoutées à la page que lorsque vous envoyez un formulaire si des «PDF Packages» y sont attachés (c.-à-d. images ajoutées au formulaire en utilisant le premier bouton 'upload image') ... Elles ne sont pas ajoutées à la page si le formulaire est envoyé lorsqu'il n'y a pas de 'PDF Packages' attaché - peu importe s'il y a 'drawing' 'PDFs attachés à celui-ci (c.-à-d. Les images ajoutées au formulaire en utilisant le second bouton' upload image ').

La section du code HTML utilisé pour télécharger les images pour les au formulaire «packages PDF de & « Dessins » est:

{% 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 %} 

Je me demande si je dois être en utilisant la ligne:

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

dans le cadre de l'instruction {% with ... %} en haut de cette section de HTML? Mais je ne sais pas comment j'utiliserais le with avec deux arguments différents en même temps ...? Est-ce possible?

Répondre

0

vous pourriez faire quelques tests. essayez de modifier votre formset .. dites avec 1 forme supplémentaire et zéro formes supplémentaires et voyez ce qui se passe.