0

I have a form in my template django that was ok, it saves ok in a model. Well, now, i want create a new form inside this form. This form, i create inside the template, but now, i want get de data to save in another model. (I can't use the formset, i'm ready using it). This form are create with javascript when user click in the option inside the list.

I'm using Class-based view to create a view.

My question is, how can i get this data from this form that was created dynamically by the user?

    <div class="form-group">

        <label class="col-sm-2 control-label">{{form.name.label}}</label>
        <div class="col-sm-10">
        {{form.name}}
        {% if form.name.errors %}
            <span class="glyphicon glyphicon-remove form-control-feedback"></span>
                <span class="help-block">{{form.name.errors.as_text}}</span>
        {% endif %}
        </div>

        <label class="col-sm-2 control-label">{{form.date_max.label}}</label>
        <div class="col-sm-10">
        {{form.date_max}}
        {% if form.date_max.errors %}
            <span class="glyphicon glyphicon-remove form-control-feedback"></span>
                <span class="help-block">{{form.date_max.errors.as_text}}</span>
        {% endif %}
        </div>

        <label class="col-sm-2 control-label">{{form.demand_desc.label}}</label>
        <div class="col-sm-10">
        {{form.demand_desc}}
        {% if form.demand_desc.errors %}
            <span class="glyphicon glyphicon-remove form-control-feedback"></span>
                <span class="help-block">{{form.demand_desc.errors.as_text}}</span>
        {% endif %}
        </div>
        <div class="optional-imege-form">{{formset}}</div>
        <div id="create-new-image-button">Add</div>
    </div>
    <div class="format-list">
        <h3>Lista de formatos:</h3>
        <ul>
            {% for key, value in format_names_fields_dict.items %}
                <li class="format-create" data-format-name-id='{{key}}' data-fields-value='{% for i in value %}{{i}},{% endfor %}'>{{key}}</li>
            {% endfor %}
        </ul>
    </div>
    <div class="col-sm-12 container-with-format-forms">

    </div>
    <div class="ground-light-popup"></div>
    <div class=""></div>
    <div class="pop-up-set-store">
        <label class="col-sm-3 control-label">Por favor, selecione a loja:</label>
        <div class="col-sm-9">
            {{form.demand_store}}
        </div>
        <div class="col-sm-12">
            <div class="btn btn-success store-button">Loja selecionada >></div>
        </div>
    </div>
    <div class="pop-up-set-store-2">
        <h3>Selecione as áreas que será necessária na demanda</h3>
        <div class="col-sm-12">
            <table>
                <tr>
                    <td>{{form.moda.label}}</td>
                    <td> {{form.moda}}</td>
                </tr>
                <tr>
                    <td>{{form.texto.label}}</td>
                    <td> {{form.texto}}</td>
                </tr>
                <tr>
                    <td>{{form.design.label}}</td>
                    <td> {{form.design}}</td>
                </tr>
            </table>
        </div>
        <div class="col-sm-12">
            <div class="btn btn-success area-button">Área(s) selecionada >></div>
        </div>
    </div>
    <div>
        <div class="col-sm-offset-2 col-sm-10">
            <input name="Criar" class="btn btn-default" type="submit" value="Criar">    
        </div>
    </div>
    <script type="text/javascript">
        $(document).ready(function(){
            $formatName = $('.format-create');
            $formatsContainer = $('.container-with-format-forms');

            for(i=0;i<$formatName.length;i++){
                $textFromFormat = $($formatName[i]).text();
                $textSplited = $textFromFormat.split('_');
                $formatJustName = $textSplited[0];
                $formatJustId = $textSplited[1];
                $($formatName[i]).text($formatJustName);
            }
            $($formatName).click(function(){
                $formatData = $(this).data();
                $fieldsToFormat = $formatData.fieldsValue.split(',');
                $myFormatHtml = "<form method='post' id='" + $formatJustId + "'>";
                $myFormatHtml += "<div class='col-sm-12 format-content-field'>";
                $myFormatHtml += "<h4>" + $(this).text() + "</h4>";
                $myFormatHtml += "<input name='format_name' value='" + $formatJustId + "' type='hidden'>";
                for(i=0;i<$fieldsToFormat.length-1;i++){
                    $fieldsToFormatName = $fieldsToFormat[i].split('_');
                    $myFormatHtml += "<label class='col-sm-2'>";
                    if($fieldsToFormatName[2] == 'True'){
                        $myFormatHtml += $fieldsToFormatName[0] + "* </label><input class='col-sm-10' name='field_name' id='id_field_name' type='text' required>"
                    }else{
                        $myFormatHtml += $fieldsToFormatName[0] + "</label><input class='col-sm-10' name='field_name' id='id_field_name' type='text'>"
                    }
                }
                $myFormatHtml += '</div>';
                $myFormatHtml += '</form>'

                $($formatsContainer).append($myFormatHtml);
                $(this).addClass('ready-selected');
                $(this).unbind();
            });
        })
    </script>
4
  • The form data in Django is passed from request.POST to your form class that cleans the data for you deleting your custom data added in javascript. You could access request.POST and manually extract the data you are interested in. Commented Jul 25, 2017 at 14:37
  • Apparently seems to work, I'ill try to save in the model and I already inform. Commented Jul 25, 2017 at 14:57
  • This worked, if you create a answer, i'll set it like a correct. Thanks!! Commented Jul 25, 2017 at 17:38
  • You better answer your own question with the exact code solved your issue to help some other coder with your exact same problem. If you'd like to give me credits you can use the @mrnfrancesco reference ;) Commented Jul 25, 2017 at 18:21

1 Answer 1

1

After the comment from @mrnfrancesco, i apply the following logic to save in data model:

class CreateDemandView(FormView):
...
    def form_valid(self, form):
        demand = form.save(commit=False)
        demand.save()

        format_name = self.request.POST.getlist('format_name')

        for i in format_name:
            fields_name = self.request.POST.getlist('field_name_' + i)
            format_name_id = NewFormatByStore.objects.get(pk=i)
            data_content_field = self.request.POST.getlist('field_format_data_' + i)

            j = 0 
            while j < len(fields_name):
                format_data_demand = FomartWithDataFromDemand(demand_name=demand, format_name=format_name_id, field_name=fields_name[j], field_format_data=data_content_field[j])
                format_data_demand.save()
                j += 1

...

I get the data from POST and save the respective data in model.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.