Govinda Totla
Govinda Totla

Reputation: 606

How to access multiple values in request body of django?

I want to select multiple values for an attribute in my Django website.

    if request.method == 'POST':
    print(request.POST)
    print(request.POST['category'])

The output of the above code is when I select the second and third category together is -

<QueryDict: {'csrfmiddlewaretoken': ['HYArlTZpPYIDX404ImuX4UjzC03qaa3zTa18Wd7hVw2AYaMln8ZaVfaJ8TsNtbZp'], 'category': ['2', '3']}>
3

I am unable to understand what I am doing wrong. Please help me out.

Thanks.

Upvotes: 5

Views: 2303

Answers (1)

Sammy J
Sammy J

Reputation: 1066

If it is multiple items you are sending in POST request then use request.POST.getlist('category') if it is single item then request.POST.get('category')

Upvotes: 6

Related Questions