0

I am using Django 1.9 to work my project. I have to use checkbox to enter text. When I use text area to type in it doesn't work, but I do not know how to change it to checkbox.

textareaform.html

<div class="form-group col-md-3">
                        <label for="{{ form.menwomenkids.id_for_label }}">please write "men","women" or "kids"</label>
                        {{ form.menwomenkids|add_class:"form-control" }}
                    </div>

It will show the form and it can work.

enter image description here

However, after I changed to checkbox, I do not how to do it?

checkboxform.html

<div class="form-group col-md-3">
                        <label for="{{ form.menwomenkids.id_for_label }}"></label>
                        <input type="checkbox" name="men" value="Men"> men<br>
                        <input type="checkbox" name="women" value="Women"> women<br>
                        <input type="checkbox" name="kids" value="Kids"> kids<br>
                    </div>

It will shows the checkbox form, but checked it did not enter text.

enter image description here

2 Answers 2

1

Try using the same name attribute.

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

4 Comments

thanks , after I changed <input type="checkbox" name="men" value="Men"> men<br> ===> <input type="checkbox" name="menwomenkids" value="Men"> men<br> it can work
and how can I make it only can check one box ?
By adding the same name to all choises ex. name="gender"
thank you,I change to type="radio" ,and it only allow one select
1

Do it with django forms, add a field forms.MultipleChoiceField.

In choices pass your choices as a tuple eg:

choices = [
    ('men', 'men'),
    ('women', 'women'),
    ('kids', 'kids'),
]

Then just render your form in your template with

{{ your_form.as_p }}

check your structure.

or set name="person_type" for each input, and access it in your view with

selected_items = request.GET.getlist("person_type")

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.