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 9034801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:35:09+00:00 2026-06-16T08:35:09+00:00

So I present a checkbox to the user, as you can see inside my

  • 0

So I present a checkbox to the user, as you can see inside my forms.py file:

class AddFooToBarForm(forms.Form):
    to_original = forms.BooleanField(initial=True,
                                     error_messages={'required':
                                                         "Oh no! How do I fix?"})
    ...

Depending on whether the user checks or unchecks that checkbox, I do something different, as you can see inside my views.py file:

def add_foo_to_bar(request, id):
    ...
    try:
        bar = Bar.objects.get(pk=id)

        if request.method == 'POST': # handle submitted data
            form = AddFooToBarForm(bar, request.POST)
            if form.is_valid(): # good, now create Foo and add to bar
                ...
                to_original = form.cleaned_data['to_original']
                print 'to original is {0}'.format(to_original) # for debugging
                if to_original:
                    # do A
                else:
                    # do B

                ...

So I want to test that my site does indeed perform the correct actions, depending on whether the user checks the checkbox or not, inside my tests.py file:

class FooTest(TestCase):
    ...

    def test_submit_add_to_Bar(self):
        form_data = {
            ...
            'to_original': True,
        }
        response = self.client.post(reverse('add_foo_to_bar', args=(self.bar.id,)),
                                    form_data, follow=True)
        self.assertContains(...) # works

        ...

        form_data['to_original'] = None
        response = self.client.post(reverse('add_foo_to_bar', args=(self.bar.id,)),
                                    form_data, follow=True)
        print response # for debugging purposes
        self.assertContains(...) # doesn't work

I’ve tried

  • del form_data['to_original'] — this gives me the “Oh no! How do I fix?” error message
  • form_data['to_original'] = None — in my view function, I get True, so A is done instead of B
  • form_data['to_original'] = False — this gives me the “Oh no! How do I fix?” error message once again

So how should I test the user not checking the checkbox in Django? (I’m using the latest version, 1.4.3)

  • 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-16T08:35:10+00:00Added an answer on June 16, 2026 at 8:35 am

    When checkbox is not checked, its not present in submitted form. Also, when submitted value is 'on'.

    If you want to make BooleanField optional have required=False while defining the form field.

    Documentation BooleanField.

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

Sidebar

Related Questions

I have a simple user signup form with a checkbox that lets users get
Imagine the following scenario. I have a checkbox that the user can check to
I present the user with a table view of all the audio files listed
Consider a simple .NET form with a couple of radio buttons and a checkbox.
I have a link inside a label. The problem is, when user clicks 'back'
I'm using EmberJS to present a table to the user. I currently test it
I am creating a settings editor where plugin writers can define their own user
I have a form that should not be submitted until the user has ticked
I wanna show checkbox on top of the screen to allow the user to
I'm doing a Perl/CGI form that needs to present a variable number (1 -

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.