1

I have a form that is updating text but not updating checkboxes in my database. The checkboxes are a boolean model in the database (postgres) where f is false and t is true. I've looked at a few other tutorials and tried to use multiple choice without much success.

I'll apologize ahead of time because I'm self taught in django and this is my first deplorable app.

Here is the forms.py module

from django import forms

from .model import parcels

class tableedit(forms.ModelForm):
        class Meta:
                model = parcels
                fields = [
                        "some_number",
                        "boolean_checkbox_1",
                        "boolean_checkbox_2"

                ]

Here is the the model in model.py

from django.db import models
from django.contrib.postgres.fields import ArrayField
from django.core.urlresolvers import reverse


class TableName(models.Model):
        name = 'table_name'
        verbose_name = 'table_name'
        some_number = models.CharField('some_number', max_length=30)
        boolean_checkbox_1 = models.BooleanField('boolean_checkbox_1 ', max_length =1)
        boolean_checkbox_2 = models.BooleanField('boolean_checkbox_2', max_length = 1)
        # Returns the string representation of the model.
        def __str__(self):              # __unicode__ on Python 2
                return self.name

        def get_absolute_url(self):
                return reverse("table_edit:view", kwargs={"id": self.id})
        class Meta:
                managed = True
                db_table = 'table'

Here is my views.py

from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views.generic import UpdateView,View
from django.contrib import auth

from django.views.decorators.csrf import csrf_protect
from model import tableedit
from django.contrib.auth.decorators import login_required
from django import forms
import simplejson
from forms import tableedit

@login_required
@csrf_protect
def table_edit(request, p_id = None):
        data = parcels.objects.get(id = p_id)
        form = tableedit(request.POST or None, instance = data)


        if form.is_valid():
                instance = form.save(commit = False)
                instance.save()
                return HttpResponseRedirect(instance.get_absolute_url())

        context = {
                "some_number" : data.some_number,
                "boolean_checkbox_1" : data.boolean_checkbox_1,
                "boolean_checkbox_2" : data.boolean_checkbox_2,
                "form":form}

        return render(request, "static/table_edit.html", context)

Here is the form in the table_edit.html file

{% if next %}
   <input type = "hidden" name="next" value= {% url 'table.view' table.id %} \>
   {% endif %}
    {{ form.as_p }}


<form action=next  method="post">{% csrf_token %}

  <input type="submit" value="Update Item" />
</form>

1 Answer 1

1

Your {{ form.as_p }} is not inside your html <form> tag. It's unlikely that any data is being sent to the server from that form.

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

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.