Django:复选框选择显示为True / False值

时间:2019-04-11 19:31:25

标签: python django forms checkbox model

我是django的初学者,我正试图让我的日历显示一个复选框本身,而不是True / False值。我可以从表单中获取要保存的数据

models.py

function calcTotalRetailVal() {
    var num1 = $oneTimeCostField.val();
    var num2 = $recurringTotalCostField.val();
    var result = parseFloat(num1.replace(/(,|[^\d.-]+)+/g, '')) + parseFloat(num2.replace(/(,|[^\d.-]+)+/g, ''));

    if (!isNaN(result)) {
        $totalRetailAmountField.text('$' + result.toFixed(2));
    }
}
calcTotalRetailVal();
$oneTimeCostField.on("keydown keyup", function() {
    calcTotalRetailVal();
});
$recurringTotalCostField.on("keydown keyup", function() {
    calcTotalRetailVal();
});

forms.py

class Event(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    description = models.TextField(max_length=350)
    start_time = models.DateTimeField()
    #end_time = models.DateTimeField()

    event_checked = models.BooleanField()

    @property
    def get_html_url(self):
        url = reverse('cal:event_edit', args=(self.id, ))
        return f'<a href="{url}"> <label> {self.title} {self.event_checked}<label></a>'

    def __str__(self):
        return '{} - {} by {}'.format(self.title, self.description, self.user)

views.py

class EventForm(forms.ModelForm):

 event_checked = forms.BooleanField()

  class Meta:
    model = Event
    # datetime-local is a HTML5 input type, format to make date time show on fields
    widgets = {
      'start_time': DateInput(attrs={'type': 'datetime-local'}, format='%Y-%m-%dT%H:%M'),
    }
    fields = ('title', 'description', 'start_time')

  def __init__(self, *args, **kwargs):
    super(EventForm, self).__init__(*args, **kwargs)
    # input_formats parses HTML5 datetime-local input to datetime field
    self.fields['start_time'].input_formats = ('%Y-%m-%dT%H:%M',)

How my calendar looks with some events

1 个答案:

答案 0 :(得分:0)

我不知道这是否是您的目标

class EventForm(forms.ModelForm):

 event_checked = forms.BooleanField()

  class Meta:
    model = Event
    # datetime-local is a HTML5 input type, format to make date time show on fields
    widgets = {
      'event_checked' forms.CheckboxInput(), # <-- added this 
      'start_time': DateInput(attrs={'type': 'datetime-local'}, format='%Y-%m-%dT%H:%M'),
    }
    fields = ('title', 'description', 'start_time')

  def __init__(self, *args, **kwargs):
    super(EventForm, self).__init__(*args, **kwargs)
    # input_formats parses HTML5 datetime-local input to datetime field
    self.fields['start_time'].input_formats = ('%Y-%m-%dT%H:%M',)