python 2.7 - How to get a data from form in django? -


i have form in template django ok, saves ok in model. well, now, want create new form inside form. form, create inside template, now, want de data save in model. (i can't use formset, i'm ready using it). form create javascript when user click in option inside list.

i'm using class-based view create view.

my question is, how can data form created dynamically 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>             {% key, value in format_names_fields_dict.items %}                 <li class="format-create" data-format-name-id='{{key}}' data-fields-value='{% 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 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 á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> 

after comment @mrnfrancesco, apply following logic 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')          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 data post , save respective data in model.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -