2016-08-02 2 views
0

나는 Stackoverflow의 다른 솔루션을 살펴 봤지만 아무 문제가없는 것 같습니다.양식에서 체크 박스 값을 저장하는 문제

저장해야하는 정보를 입력하는 양식이 있습니다. 사용자에게 세 가지 옵션을 제공하여 틱 할 수 있습니다. 그러나 값이 유효하지 않기 때문에 양식을 저장하지 않습니다. 당신이 키를 누르면 웹 페이지 자체에

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 

이 오류가 표시 저장 : 여기

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 

내 형태 :

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

이 오류는 여기 온다

내 모델입니다 'Critical'이라는 상자를 체크하면 귀하의 모델에서

답변

1

당신은 STATUS_CHOICES의 값 (각 튜플의 첫 번째 값) Critical, Medium, 대문자로 첫 글자와 Low을 가지고 있지만 폼의 모든 소문자를했다. 양식 에서처럼 STATUS_CHOICES을 사용하도록 모델에서 선택 사항을 수정해야합니다.

관련 문제