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

  • Home
  • SEARCH
  • 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 8530519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:15:58+00:00 2026-06-11T09:15:58+00:00

Problem I change my approach… My url : passing my_code on it (r’^test_form_2/(?P<my_code>\d+)/$’, TestForm_2),

  • 0

Problem

I change my approach…

My url : passing my_code on it

(r'^test_form_2/(?P<my_code>\d+)/$', TestForm_2),

My views : grab the my_code to choose the right choice list on the dic_list

def TestForm_2(request,my_code):

    dic_list = {'1':(('1','A'),('2','B'),('3','C')),
                '2':(('1','D'),('2','E'),('3','F')),
                '3':(('1','G'),('2','H'),('3','I'))
                }

    if request.method =="POST" :
        form = TestForm2(request.POST)

        if form.is_valid():

            return HttpResponse ('OK')

    else :

        my_choices_list = dic_list[my_code]
        form = TestForm2(my_choice=my_choices_list)

    return render_to_response ('zip_finder/page_form.html')

and the form :

class TestForm2(forms.Form):

    my_select = forms.ChoiceField(choices='',label='Choisissez dans la liste')

    def __init__(self, my_choice=None, **kwargs):
        super(TestForm2, self).__init__(**kwargs)
        if my_choice:

            self.fields['my_select'] = forms.ChoiceField(choices=my_choice,label='Choisissez dans la liste')

enter image description here

I click submit and then :

Caught ValueError while rendering: too many values to unpack

I have a look to the HTML and can’t see the selected option on my selection :

 <select id="id_my_select" class="select" name="my_select">
<option value="1">D</option>
<option value="2">E</option>
<option value="3">F</option>
</select>

SOLUTION : Use ModelChoiceField

My url : passing my_code on it

  (r'^test_form_2/(?P<my_code>\d+)/$', TestForm_2),

My views : grab the my_code to choose the right choice list on a queryset object

 def TestForm_2(request,my_code):



        if request.method =="POST" :
            form = TestForm2(request.POST)

            form.fields['ville_source'].queryset = Villes.objects.filter(code_postal=my_code)
            if form.is_valid():

                return HttpResponse ('OK')

        else :


            form = TestForm2(my_code=my_code)

        return render_to_response ('zip_finder/page_form.html')

My form :

class TestForm4(forms.Form):


    ville_source = ModelChoiceField(queryset=Villes.objects.all(),required=True,label='Choisissez votre ville source')


    def __init__(self, *args, **kwargs):
        code_postal = kwargs.pop('my_code', None)
        super(TestForm4, self).__init__(*args, **kwargs)

        if code_postal:


            self.fields['ville_source'].queryset = Villes.objects.filter(code_postal=code_postal)
  • 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-06-11T09:15:59+00:00Added an answer on June 11, 2026 at 9:15 am
    1. Queryset lst_ville is empty, it must be filtering like this, using the unicode value of code_postal:

      .filter(code_postal='<django.forms.fields.CharField object at 0x26bf550>').
      
    2. Use ModelChoiceField instead of CharField:

      # make sure that lst_ville is not an empty queryset
      ville_source = forms.ModelChoiceField(queryset=lst_ville,label='Choisissez votre ville source')
      
    3. For field filtering based on the value of another field of the form, it becomes a little complicated. The simplest solution for you is to just use django-selectables or django-autocomplete-light.

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

Sidebar

Related Questions

I have a slight problem... we made a change to our url structure the
I am facing background color change problem while using jquery. When the page loads
I'm having a problem getting a change event to register with the following code:
I implemented the table sorter yesterday and i have a problem trying to change
I have problem with c# script who change user AD password, when try change
Hey here is my problem : i made a flash and i can`t change
I have a problem to dismiss my progbar and to change to view that
I had a problem that was making the original array to change, the curious
I faced a problem that I can not find a way to change the
I have a problem, I need to change body of method when this class

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.