0
votes

I keep getting this error while trying to submit my django form. I'm quite new to django and this error has got me stuck for days.

here's my model.py

class Person(models.Model):
    name = models.OneToOneField(User, on_delete=models.CASCADE, unique=True)
    choices = (
        ('Staff', 'staff'),
        ('Building Manager', 'BM'),
    )
    type = models.CharField(max_length=30, choices=choices)

    def __str__(self):
        return '{0} {1} {2}'.format(self.pk, self.name.first_name, self.name.last_name)


class Building(models.Model):
    address = models.CharField(max_length=100)
    person = models.ForeignKey(Person, on_delete=models.SET_NULL, null=True, blank=True)

    def __str__(self):
        return self.address


class Device(models.Model):
    name = models.CharField(max_length=30)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    total = models.IntegerField()
    building = models.ForeignKey(Building, on_delete=models.SET_NULL, null=True)

    def __str__(self):
        return self.name


class Ticket(models.Model):
    name = models.ForeignKey(Person, on_delete=models.CASCADE)
    issue = models.CharField(max_length=255)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    device = models.ForeignKey(Device, on_delete=models.CASCADE)
    room = models.CharField(max_length=255)

urls.py

urlpatterns = [
    path('', views.home, name='home'),
    path('ajax/load-devices/', views.load_device, name='ajax_load_device')
    ]

views.py

def home(request, cls=TicketForm):
    items = Building.objects.all()
    if request.method == "POST":
        form = cls(request.POST or None)

        if form.is_valid():
            form.save()
            return redirect(reverse('home'))

    else:
        form = cls()
        return render(request, 'home.html', {'form': form, 'items': items, 'header': 'Building'})


def load_device(request):
    category_id = request.GET.get('category')
    devices = Device.objects.filter(category_id=category_id).order_by('name')
    return render(request, 'device_dropdown.html', {'devices': devices})

forms.py

class TicketForm(forms.ModelForm):
    class Meta:
        model = Ticket
        fields = ['name', 'issue', 'category', 'device', 'room']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['device'].queryset = Device.objects.none()

        if 'country' in self.data:
            try:
                category_id = int(self.data.get('category'))
                self.fields['device'].queryset =  Device.objects.filter(category_id=category_id).order_by('name')
            except (ValueError, TypeError):
                pass  # invalid input from the client; ignore and fallback to empty Device queryset
        elif self.instance.pk:
            self.fields['device'].queryset = self.instance.category.device_set.order_by('name')

home.html

<form class="forms-sample" action="" id="TicketForm" method="post" data-device-url="{% url 'ajax_load_device' %}">
                      {% csrf_token %}

                      {% for field in form %}
                        <div class="form-group">
                          <label for="id_{{ field.name }}">{{ field.label }}</label>
                          <div>{{ field }}</div>
                        </div>
                      {% endfor %}

                      <button type="submit" value="Submit" class="btn btn-success mr-2">Send</button>
                  </form>

Then i tried moving the return statement in the views.py out of the else and the form wasnt saving. Please I've battled with this for a long time now and id appreciate if someone helped. Thanks

2
In your views.py file do you have all your imports, such as from django.shortcuts import render? Because otherwise you won't be able to use the render function.Rob Kwasowski
yeah i have all the importsJoshua

2 Answers

1
votes

-I would move the cls parameter out of the home function definition in views.py and import at top

-then next i would remove the or None out of defining the Ticket form like

-I imagine your form is not validating, which is not a handled in a else statement causing your view to not return a response object

see below:

from .forms import TicketForm

def home(request):
    items = Building.objects.all()
    if request.method == "POST":
        form = TicketForm(request.POST)

        if form.is_valid():
            form.save()
            return redirect(reverse('home'))
        else:
            return HttpResponse("Form Failed to Validate")

    else:
        form = cls()
        return render(request, 'home.html', {'form': form, 'items': items, 'header': 'Building'})
0
votes

if your request is a POST, but form.is_valid() returns false, you drop out of everything, and the function will (implicitly) return None.

I bet if you outdent that last return render(...) you will get what you want.