Opened 3 years ago

Closed 3 years ago

#33053 closed Bug (invalid)

How do I get List of values from django form template to form.py Cleaned_data

Reported by: Gurveer Singh Owned by: nobody
Component: Forms Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Gurveer Singh)

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

Change History (4)

comment:1 by Gurveer Singh, 3 years ago

Description: modified (diff)

comment:2 by Gurveer Singh, 3 years ago

Description: modified (diff)

comment:3 by Gurveer Singh, 3 years ago

Description: modified (diff)

comment:4 by Tim Graham, 3 years ago

Resolution: invalid
Status: newclosed

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

Note: See TracTickets for help on using tickets.
Back to Top