表格

时间:2016-08-02 16:32:35

标签: python django

我已经查看了Stackoverflow上的其他解决方案,但似乎没有任何帮助解决我遇到的问题。

我有一张表格,用于填写需要保存的信息。我给用户三个选项,他们可以打勾。但是,它不会保存表单,因为它表示该值无效。

这是我的模特:

from django.db import models
from django.utils import timezone


FLAG_CHOICES = (('Active', 'Active'), ('Inactive', 'Inactive'), )
STATUS_CHOICES=(('Critical', 'Critical'), ('Medium', 'Medium'), ('Low','Low'))


class Event(models.Model):
    event_status=models.CharField(max_length=10, choices=STATUS_CHOICES)
    event_title=models.CharField(max_length=50)
    event_description=models.CharField(max_length=500)
    event_flag=models.CharField(max_length=10, choices=FLAG_CHOICES)
    date_active=models.DateField(default=timezone.now())
    time_active=models.TimeField(default=timezone.now())

    def __str__(self):
        return self.event_title

这是我的表格:

from django import forms
from server_status.models import Event

FLAG_CHECKBOX = [('active', 'Active'), ('inactive', 'Inactive'), ]
STATUS_CHOICES=[('critical', 'Critical'), ('medium', 'Medium'), ('low','Low'),]


class Add_Event_Form(forms.ModelForm):
    event_title = forms.CharField(max_length=50, help_text="Please enter an informative title.")
    event_status = forms.MultipleChoiceField(choices=STATUS_CHOICES, widget=forms.CheckboxSelectMultiple,
                                             help_text="Please select the status of the event")
    event_description = forms.CharField(max_length=500, initial="",
                                        help_text="Enter a short description of the event here")
    event_flag = forms.MultipleChoiceField(choices=FLAG_CHECKBOX, required=True, widget=forms.CheckboxSelectMultiple,
                                           help_text="Please select the status of this event.")
    date_active = forms.DateField(required=True, widget=forms.DateInput(attrs={'class': 'datepicker'}),
                                  help_text="Please select a date for this event.")
    time_active = forms.TimeField(required=True, widget=forms.TimeInput(format='%HH:%MM'),
                                  help_text="Please select a time for this event in HH:MM format.")


    class Meta:
        model = Event
        fields = '__all__'  # I want all fields to be editable

按Save:

时,网页上会显示此错误
Select a valid choice. [u'critical'] is not one of the available choices.

当您勾选“严重”框时会出现此错误。

1 个答案:

答案 0 :(得分:1)

在您的模型中,您拥有STATUS_CHOICES的值(每个元组中的第一个值)CriticalMediumLow,首字母为大写,但是您做了所有小写的形式。您应该修改模型中的选项,以使用与表单1相同的STATUS_CHOICES