Django:日期输入字段中的错误

时间:2014-12-01 09:55:31

标签: django validation django-forms

我正在使用django表单来呈现html表单页面。除了我的datetime表单字段元素外,一切似乎都在工作。我需要将用户输入的日期值传递给一个MSSQL SP,但是,当我尝试打印输入的日期时,它会打印一个空白行。我正在使用 SelectDateWidget 作为我的日期表单字段。

关于我在这里做错了什么的建议

forms.py

class ConsistencyForm(forms.Form):

Customer=forms.ModelChoiceField(
    label="Customer Name",
    widget=forms.Select,
    queryset=Customer.objects.all(),
    empty_label=None,
    )

Severity_level=forms.ModelChoiceField(
     label="Severity Level",
    widget=forms.Select,
    queryset=Criticality.objects.all(),
    empty_label=None,
    help_text="</br>"
    )

run_date=forms.DateField(
    label="Run Date", widget=SelectDateWidget(years=range(2000, datetime.date.today().year+10)),initial=timezone.now(),
    help_text='</br>')

views.py

def consisreportgen(request):
if request.method == 'POST':
    form = ConsistencyForm(request.POST or None)
    if form.is_valid():
        customername=request.POST.get('Customer','')
        rundate=request.POST.get('run_date','')
        Customer=form.cleaned_data['Customer']
        run_date=form.cleaned_data['run_date']
        print(modules_choosen)
        print(customername)
        print(severity)
        print(rundate)

        return HttpResponseRedirect('/consistency/results/')

else:
    form = ConsistencyForm

return render(request,'consistency/consistency.html', {'form': form})

1 个答案:

答案 0 :(得分:0)

如果客户是模特,你就不能像这样给它分配价值

Customer=form.cleaned_data['Customer']

您应该创建Customer类的注入,然后给他一些值,如下所示:

customer = Customer()
customer.name = form.cleaned_data['Customer']