Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7547881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:34:08+00:00 2026-05-30T09:34:08+00:00

I had a Django form that submitted a list of values to my view.

  • 0

I had a Django form that submitted a list of values to my view. I first tried retrieving the list using the get method but discovered that it only returned the last one and I should be using getlist. After some stumbling around I found a closed Django bug that explained the motivation for this behavior:

The reasoning behind this is that an API method should consistently return either a string or a list, but never both. The common case in web applications is for a form key to be associated with a single value, so that’s what the [] syntax does. getlist() is there for the occasions (like yours) when you intend to use a key multiple times for a single value.

I’m just wondering whether this is actually a best practice – it contradicts the way the get method works on other data structures, ie. dictionaries.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:34:09+00:00Added an answer on May 30, 2026 at 9:34 am

    HTTP requests do support multiple values assigned to a one parameter (key). That’s why people can use them and do (sometimes) use them. That’s also why Django introduced the MultiValueDict structure.

    Division into get() and getlist() is beneficial, because it helps you avoid errors and keeps your view code simple. Consider other behaviors, they all require more code to do exactly the same thing:

    • get() always returning list.

      In most use cases you pass just one value to a one key, so you would need to add [0] and provide default value as a list.

      param = request.GET.get('param', ['default value',])[0]

    • get() returning single value or a list, depending on number of values.

    It is a drawback in HTML selects with multiple options allowed. People can select zero, one or more values. That means you’ll need to convert single value to list or in opposite direction by yourself:

     params = request.GET.get('params', [])
     # Here you have absolutely no idea if this is a list or a single value
     # But you will need only one of that types
    
     # If you need list: ---------------------------------
     if not isinstance(params, list):
         params = [params,]
    
     objs = TestModel.objects.filter(id__in=params).all()
    
     # If you need single value: -------------------------
     if isinstance(params, list):
         params = params[0]   # Error if params is empty list...
    
    
    obj = TestModel.objects.get(id=params)
    
    • get() always returning single value. So how do you handle multiple values without getlist in that case?

    So to answer your question there is an added value of get/getlist behaviour.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When rendering a django form using the as_p method, you traditionally get something like:
I posted this question on the django-users list, but haven't had a reply there
I'm trying to create a form in Django. That works and all, but I
I had an existing Django nonrel application using MongoDB. Seemed that the related fields
I had the following idea: Say we have a webapp written using django which
Had to download Command Line Tools from Apple to get make to work, but
I had django-piston working a week ago but recently I'm unable to call any
I had this idea of creating desktop apps using django. The principe being: -
When configuring uwsgi django application for first time i had weird error. NGIX (configured
I had created a Django view which is returning some data after reading from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.