[Django] #33053: How do I get List of values from django form template to form.py Cleaned_data

74 views
Skip to first unread message

Django

unread,
Aug 24, 2021, 6:27:44 PM8/24/21
to django-...@googlegroups.com
#33053: How do I get List of values from django form template to form.py
Cleaned_data
----------------------------------------+------------------------
Reporter: GurveerSaini | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 3.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
----------------------------------------+------------------------
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

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

Django

unread,
Aug 24, 2021, 6:29:18 PM8/24/21
to django-...@googlegroups.com
#33053: How do I get List of values from django form template to form.py
Cleaned_data
------------------------------+--------------------------------------

Reporter: GurveerSaini | 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 GurveerSaini:

Old description:

New description:

----

{{{

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

--

--
Ticket URL: <https://code.djangoproject.com/ticket/33053#comment:1>

Django

unread,
Aug 24, 2021, 6:32:12 PM8/24/21
to django-...@googlegroups.com
#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:

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

----

{{{

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

unread,
Aug 24, 2021, 6:34:50 PM8/24/21
to django-...@googlegroups.com
#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

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

New description:

----

{{{

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 a Input box , which gets the value of input box and
creates a hidden input with entered value and name blog_tags every time
when user hit enter

{{{
<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:3>

Django

unread,
Aug 24, 2021, 9:43:38 PM8/24/21
to django-...@googlegroups.com
#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: closed
Component: Forms | Version: 3.2
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => invalid


Comment:

Please see TicketClosingReasons/UseSupportChannels for ways to get help
with usage questions. Thanks!

--
Ticket URL: <https://code.djangoproject.com/ticket/33053#comment:4>

Reply all
Reply to author
Forward
0 new messages