2016-08-02 61 views
0

我看了一下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 

在網頁本身這個錯誤顯示,當你按下保存:

Select a valid choice. [u'critical'] is not one of the available choices. 

這個錯誤出現當你勾選「重要」框時,你就可以開始使用了。

回答

1

在你的模型有STATUS_CHOICES的值(每個元組第一個值)CriticalMediumLow與第一個字母爲大寫,但你沒有在形式全部小寫。您應修改模型中的選項以使用與表格1相同的STATUS_CHOICES