Opened 3 years ago
Last modified 3 years ago
#33053 closed Bug
How do I get List of values from django form template to form.py Cleaned_data — at Initial Version
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
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
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