45825

Create a dictionary with a HTML-form in Django

Question:

Is there any possible method to create a form with one or more elements like:

<form action="{% url "foo" %}" method="POST"> <div id="report_item"> <input type="text" name="dontknow"> <input type="date" name="dontknow"> </div> <div id="report_item"> <input type="text" name="dontknow"> <input type="date" name="dontknow"> </div> </form>

And to get a dictionary from this form as below:

{data: {1: { text: 'bla', date: '2014-03-02' }, {2: { text: 'second text', date: '2014-03-01' } }

Answer1:

Not quite, but you probably want to look at <a href="https://docs.djangoproject.com/en/dev/topics/forms/formsets/" rel="nofollow">formsets</a>. That gives you a series of repeated forms, which you can output on the template by just iterating through. Once you've validated the POST data if you really need a dictionary you can do something like:

data = {i: form.cleaned_data for i, form in enumerate(formset.forms)}

Recommend

  • Leaflet z-index
  • Is it better to use the “hidden” CSS attribute or fetch each set of new images?
  • Counting problem C#
  • What Makes These Two Array Adds Different?
  • Linq Merge lists
  • Remove changes from one element when event occurs on another element?
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • MongoDb aggregation
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • FileReader+canvas image loading problem
  • Insert into database using onclick function
  • Deselecting radio buttons while keeping the View Model in synch
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • MySQL WHERE-condition in procedure ignored
  • When should I choose bucket sort over other sorting algorithms?
  • what is the difference between the asp.net mvc application and asp.net web application
  • Web-crawler for facebook in python
  • Unanticipated behavior
  • Matrix multiplication with MKL
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • trying to dynamically update Highchart column chart but series undefined
  • Proper way to use connect-multiparty with express.js?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Free memory of cv::Mat loaded using FileStorage API
  • How to set the response of a form post action to a iframe source?
  • How do I configure my settings file to work with unit tests?
  • Memory offsets in inline assembly
  • How to stop GridView from loading again when I press back button?
  • Turn off referential integrity in Derby? is it possible?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize