Wizard
Wizard

Reputation: 22143

List in QueryDict retrieved as string

I re-construct my code as minimal as:

<form action="#" method="post"> {% csrf_token %}
    <input type="text" name="title">
    <textarea name="content" id="" cols="30" rows="10"></textarea>
    <input type="text" name="tags">
    <input type="submit">   
</form>

I print the POST data from views.py

print(self.request.POST)
print(self.request.POST["title"])
print(self.request.POST["content"])
print(self.request.POST["tags"])
print(type(self.request.POST["tags"]))

Come by with

<QueryDict: {'csrfmiddlewaretoken': ['r4QIefk3YNSU0EivHwq8dwmHUNPFK4WwcvJyt5FK6gAQUdTl6IJ2m8V1Z71OZ1kU'], 'title': ['list'], 'content': ['display'], 'tags': ['as,string']}>
list
but
as,string
<class 'str'>

Within QueryDict, they are lists of ['list'] ['display'] ['as,string']

but is retrieved as string.

What's happening here?

Upvotes: 1

Views: 998

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47374

QueryDict is subclass of MultiValueDict which allows to handle multiple values for the same key. By default MultiValueDict's __getitem__ method returns last element in list . But as @WillemVanOnsem said in his comment MultiValueDict also provides getlist method which returns all values associated with selected key.

Upvotes: 2

Related Questions