0

I have a template that creates a text entry field and a checkbox. When the checkbox is unchecked, the text field is disabled and cleared, when it's checked, it's enabled, and the user may or may not have typed in it. In my controller I need to distinguish between the 2 cases when the checkbox is unchecked, and the checkbox is checked but the text field is blank. I can get the value of the text field, but not of the checkbox. Is there some way to do this? I've googled this, and I see it's been asked a few times here, but noneof the solutions seem to work for me.

5
  • 1
    Please show some code. Showing if a checkbox is checked is a fairly basic forms task. Commented May 2, 2012 at 14:00
  • 1
    Code would be nice. Also consider using the Django Debug Toolbar. If this is a jQuery (or other JS) related situation, Firebug is your friend. Commented May 2, 2012 at 14:07
  • Perhaps my question isn't clear. I am not having any problems creating the checkbox, nor getting its state in my jQuery code. I need to get its state back in my python controller. Commented May 2, 2012 at 14:20
  • No, your question is perfectly clear. It's impossible to answer it though, because you still haven't shown any code. Commented May 2, 2012 at 14:48
  • It's part of a very large, complex app. There's no simple, minimal example I can post. Commented May 2, 2012 at 15:15

2 Answers 2

1
request.POST.get('my_checkbox_field')

P.S. In Django, they're called "views" not controllers.

UPDATE (based on comment)

I'm taking "controller" to mean "view" since Django doesn't have a concept of controllers and they're closest to views. If that's not the case, by all means correct me. Given that, all function-based views at the very least require a request parameter. If you're using class-based views, then request is simply stored on the view object, so you just need to modify it to self.request. I suggest you take some more time to thoroughly read the docs, as this is pretty much bare minimal understanding stuff that is well documented.

Sign up to request clarification or add additional context in comments.

7 Comments

That gives me:*** NameError: name 'request' is not defined
Isn't the view the template that causes the html to get rendered? And the controller is the python code that selects the data?
No, Django uses loose MVC, it has models, views, and templates. Views and templates are close to controllers and views, respectively, in MVC, but not exactly.
Yes, we are using class based views. self.request gives me: * AttributeError: 'MeasurementData' object has no attribute 'request'. I am clearly new to django, but I have read the docs. I've been very productive in a short time, but this is one thing I cannot figure out.
MeasurementData sounds like a model. Make sure your "view" is truly a subclass of View or one of its subclasses (DetailView, ListView, etc.)
|
1

Are you looking for this?

def myview(request):
    form = MyForm()

    if request.method == 'POST':
        form = MyForm(request.POST)

        if form.is_valid():
            checkbox = request.POST.get('my_checkbox', False) # will be True if checked

            if checkbox:
                # check textfield content
            else:
                # do something else

            return render_to_response(template, kwvars, context_instance=RequestContext(request))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.