user1375910
user1375910

Reputation: 11

django modelform BooleanField checkbox

I have a model with a BooleanField

model.py

class Entry(models.Model):
    test = models.BooleanField()

class TestForm(ModelForm):
    class Meta:
        model = Entry

view.py

def registreren(request):
    context = {'form': TestForm()}
    if request.method == "POST":
        form = TestForm(request)
        if form.is_valid():
            form.save()

    context.update(csrf(request))
    return render_to_response("test.html", context)

test.html

<body>
        <form action="/registreren/" method="post">
                {% csrf_token %}
                {{form}}
                <input type="submit" value="Hit it!" />

        </form>
</body>

when I save the form I only get false values even if I click the checkbox in the form. So either if the checkbox is clicked or not in the form all instances of Entry.test are False.

I have no idea what goes wrong. All other values are send correctly.

Upvotes: 1

Views: 3138

Answers (1)

You are passing the request object to the form instead of the request.POST dictionary-like object which contains your POST data.

if request.method == "POST":
    form = TestForm(request.POST)
    if form.is_valid():
        form.save()

I thought you said the other fields are showing up? They shouldn't!

Upvotes: 3

Related Questions