"Key 'current' not found in 'InputForm'. Choices are: ." in Djangoforms

Viewed 65

I have a model from which I am making a django form. my model have all the feild but form has not ? I don't know where is the error? models.py:

from django.db import models

class flightInfo(models.Model):

    airCode=models.CharField(max_length=20)
    aircraft_departure=models.CharField(max_length=20)
    departure_time=models.TimeField(auto_now=False, auto_now_add=False)
    aircraft_destination=models.CharField(max_length=20)
    arrivale_time=models.TimeField(auto_now=False, auto_now_add=False)


    airCode2=models.CharField(max_length=20)
    aircraft_departure2=models.CharField(max_length=20)
    departure_time2=models.TimeField(auto_now=False, auto_now_add=False)
    aircraft_destination2=models.CharField(max_length=20)
    arrivale_time2=models.TimeField(auto_now=False, auto_now_add=False)

    airCode3=models.CharField(max_length=20)
    aircraft_departure3=models.CharField(max_length=20)
    departure_time3=models.TimeField(auto_now=False, auto_now_add=False)
    aircraft_destination3=models.CharField(max_length=20)
    arrivale_time3=models.TimeField(auto_now=False, auto_now_add=False)

    start_date=models.DateField()
    end_date=models.DateField()
    current =models.DateField()
    day1=models.BooleanField(default=False)
    day2=models.BooleanField(default=False)
    day3=models.BooleanField(default=False)
    day4=models.BooleanField(default=False)
    day5=models.BooleanField(default=False)
    day6=models.BooleanField(default=False)
    day7=models.BooleanField(default=False)
   
    def __str__(self):
        return self.airCode

forms.py:

from django import forms
from . models import *


class InputForm(forms.Form):

    class Meta:
        model = flightInfo
        
        fields = ['airCode', 'start_date', 'day7', 'current']

views.py:

from django.shortcuts import render
from . models import *
from django.http import HttpResponseRedirect, HttpResponse

from .forms import InputForm


def detail(request):
    print(request.GET)
    print(request.POST)
    if request.method == 'POST':
        print( request.POST.get('current'))

        form = InputForm(request.POST)

        print (form['current'].value())
        print (form.data['current'])

        if form.is_valid():

            print( form.cleaned_data['current'])
            print( form.instance.my_field)

            form.save()
        return HttpResponse('<h1>Hello World</h1>')

and urls.py:

from django.urls import path , include
from . import views
urlpatterns = [

    path('', views.detail),
   
    
]

Please help me .And yeah I am getting date from a form to make an instance of django form and not from django form making a html form.

1 Answers

The InputForm should be extending ModelForm if you are trying to create a form based off of a model. You can learn more about model forms here

from django.forms import ModelForm
from .models import *

class InputForm(ModelForm):

    class Meta:
        model = flightInfo
        
        fields = ['airCode', 'start_date', 'day7', 'current']

Related