#33053: How do I get List of values from django form template to form.py
Cleaned_data
-------------------------------+--------------------------------------
     Reporter:  Gurveer Singh  |                    Owner:  nobody
         Type:  Bug            |                   Status:  new
    Component:  Forms          |                  Version:  3.2
     Severity:  Normal         |               Resolution:
     Keywords:                 |             Triage Stage:  Unreviewed
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+--------------------------------------
Description changed by Gurveer Singh:

Old description:

> I am actually try to post a list of values ['good','nice','happy'] to the
> backend.
>
> I have following files in my django app.
>

>
> {{{
> models.py
> class TagModel(models.Model):
> name = models.CharField(max_length=255)
>
> class BlogModel(models.Model):
>   title = models.CharField(max_length=255)
>   tags = models.ManyToManyField(TagModel)
> }}}
>
> ----
>
> {{{
>
> forms.py
> class BlogForm(forms.ModelForm):
> title = forms.CharField(widget=forms.TextInput(attrs={
>     'placeholder':_('Enter Blog Title'),
>     'class' : 'form-control border-success'
> }))
>  blog_tags = forms.ChoiceField(widget=forms.TextInput(attrs={
>     "name":"blog_tags"
> }))
>
> def clean_title(self):
>     title = self.cleaned_data.get('title')
>     print(title)
>     return title
>
> def clean_blog_tags(self):
>     //this line does not get whole list of data
>     tags = self.cleaned_data.get('blog_tags')
>     print(tags) // problem is here
>     return tags
>
> class Meta:
>     model = PostModel
>     fields = ['title','blog_tags']
> views.py
> class create_blog(CreateView):
> template_name = 'blogbackend/create_blog.html'
> form_class=BlogForm
>
>  def get_form_kwargs(self):
>     kwargs = super(create_blog,self).get_form_kwargs()
>     print(kwargs)
>     return kwargs
> }}}
>
> Html Template
> [[Image('https://i.stack.imgur.com/lzqxO.jpg')]]
>
> What happen on template is :-
>
> I render the title field on the view
>
> I added a JS code on Input box , which gets value a create a hidden input
> with name blog_tags
>
> which is same as the filed name in forms.py file
>
> After user submit the form
>
> I successfully receives the both input values title and blog_tags in my
> views.py file and successfully display on console with helper function
> get_form_kwargs()
>
> Data is look like this
>
> {{{
>
>  {'initial': {}, 'prefix': None, 'data': <QueryDict:
>  {'csrfmiddlewaretoken':
> ['4Cm5mvGFv4skPJNTzRI8fKZKq9i7edQbwNmOgCPbDTtu8JQHqE5cd9rQLA8Kzhpu'],'title':
> ['first'],'blog_tags' : ['good','nice','happy']}>, 'instance':None}
> }}}
>

> Problem
>
> When I am trying to access the value inside function clean_blog_tags() it
> only prints list value "happy" on console
> could anyone please help me to get whole list of values in clean function
>
> Thanks GS

New description:

 I am actually try to post a list of values ['good','nice','happy'] to the
 backend.

 I have following files in my django app.



 {{{
 models.py
 class TagModel(models.Model):
 name = models.CharField(max_length=255)

 class BlogModel(models.Model):
   title = models.CharField(max_length=255)
   tags = models.ManyToManyField(TagModel)
 }}}

 ----

 {{{

 forms.py
 class BlogForm(forms.ModelForm):
 title = forms.CharField(widget=forms.TextInput(attrs={
     'placeholder':_('Enter Blog Title'),
     'class' : 'form-control border-success'
 }))
  blog_tags = forms.ChoiceField(widget=forms.TextInput(attrs={
     "name":"blog_tags"
 }))

 def clean_title(self):
     title = self.cleaned_data.get('title')
     print(title)
     return title

 def clean_blog_tags(self):
     //this line does not get whole list of data
     tags = self.cleaned_data.get('blog_tags')
     print(tags) // problem is here
     return tags

 class Meta:
     model = PostModel
     fields = ['title','blog_tags']
 views.py
 class create_blog(CreateView):
 template_name = 'blogbackend/create_blog.html'
 form_class=BlogForm

  def get_form_kwargs(self):
     kwargs = super(create_blog,self).get_form_kwargs()
     print(kwargs)
     return kwargs
 }}}

 Html Template
 [[Image('https://i.stack.imgur.com/lzqxO.jpg')]]

 What happen on template is :-

 I render the title field on the view

 I added a JS code on Input box , which gets value a create a hidden input
 with name blog_tags

 {{{
 <input type="hidden" name="blog_tags" value="..." />
 }}}


 which is same as the filed name in forms.py file

 After user submit the form

 I successfully receives the both input values title and blog_tags in my
 views.py file and successfully display on console with helper function
 get_form_kwargs()

 Data is look like this

 {{{

  {'initial': {}, 'prefix': None, 'data': <QueryDict:
  {'csrfmiddlewaretoken':
 ['4Cm5mvGFv4skPJNTzRI8fKZKq9i7edQbwNmOgCPbDTtu8JQHqE5cd9rQLA8Kzhpu'],'title':
 ['first'],'blog_tags' : ['good','nice','happy']}>, 'instance':None}
 }}}


 Problem

 When I am trying to access the value inside function clean_blog_tags() it
 only prints list value "happy" on console
 could anyone please help me to get whole list of values in clean function

 Thanks GS

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33053#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/070.e51ab3ce37a701f75f845a9520735b30%40djangoproject.com.

Reply via email to