我需要利用前端的BooleanField

时间:2018-10-26 03:49:08

标签: python django

我希望用户使用BooleanField来选择动物吃的饭菜(早餐,午餐,晚餐,可能是这些的组合)。如果选中了该复选框,那么我希望它在屏幕上生成时间表,您可以在该时间表中检查动物是否当天吃了上述食物。问题是,每当我添加时间表时,都不会产生一个时间表,在该时间表中我可以检查动物是否已食用。 (我知道这种解释可能会令人困惑)。

这是我的动物和时间表模型:

class Animal(models.Model):
    """The actual animal, embeded in animaltype"""
    animal_type = models.ForeignKey(AnimalType, on_delete=models.CASCADE)
    name = models.CharField(max_length=60, default='')
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        if len(self.name)>20:
            return self.name[:20] + "..."
        else:
            return self.name

class Schedule(models.Model):
    """Model for animal eating schedule"""
    animal = models.ForeignKey('Animal', on_delete=models.CASCADE)
    breakfast = models.BooleanField(default=False)
    lunch = models.BooleanField(default=False)
    dinner = models.BooleanField(default=False)

这是我添加时间表并查看时间表的观点(在动物视图上):

@login_required
def new_schedule(request, animal_id):
    """add a new schedule for the animal"""
    animal = Animal.objects.get(id=animal_id)

    if request.method != 'POST':
        #create 3 different checkboxes for the meals and if they check it, add that meal to their schedule
        form = ScheduleForm()
    else:
        #POST data submitted
        form = ScheduleForm(data=request.POST)
        if form.is_valid():
            new_schedule = form.save(commit=False)
            new_schedule.animal = animal
            new_schedule.save()
            return HttpResponseRedirect(reverse('zoo_animal_feeders:animal', args=[animal_id]))

    context = {'animal':animal, 'form':form}
    return render(request, 'zoo_animal_feeders/new_schedule.html', context)

@login_required
def animal(request, animal_id):
    """shows a single animal and the schedule associated"""
    animal = Animal.objects.get(id=animal_id)

    schedule = animal.schedule_set.order_by('-date_added')
    context = {'animal':animal, 'schedule':schedule}
    return render(request, 'zoo_animal_feeders/animal.html', context)

这是我的添加时间表模板:

{% extends "zoo_animal_feeders/base.html" %}
{% load bootstrap3 %}

{% block header %}
  <h2><a href="{% url 'zoo_animal_feeders:animal' animal.id %}">{{ animal }}</a></h2>
  <h2>Add new schedule:</h2>
{% endblock header %}

{% block content %}

  <form action="{% url 'zoo_animal_feeders:new_schedule' animal.id %}" method="post" class="form">
    {% csrf_token %}
    {% bootstrap_form form %}

    {% buttons %}
      <button name='submit'>add schedule</button>
    {% endbuttons %}
  </form>

{% endblock content %}

这是应该看到时间表的模板:

{% extends 'zoo_animal_feeders/base.html' %}

{% block header %}
  <h2>{{ animal }}</h2>
{% endblock header %}

{% block content %}
  <p>Schedule:</p>
  <p><a href="{% url 'zoo_animal_feeders:new_schedule' animal.id %}">add schedule</a></p>
  {% for schedule in schedules %}
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3>
          <small>
            <a href="{% url 'zoo_animal_feeders:edit_schedule' schedule.id %}">edit schedule</a>
          </small>
        </h3>
      </div>
      <div class="panel-body">
        <!--This is where I need to add the checkboxes that you can check and stay checked until midnight each night-->
        {% if schedule.breakfast == True %}
          {{ schedule.breakfast }}
        {% endif %}
        {% if schedule.lunch == True %}
          {{ schedule.lunch }}
        {% endif %}
        {% if schedule.dinner == True %}
          {{ schedule.dinner }}
        {% endif %}
      </div>
    </div>
  {% empty %}
    <li>There are no schedules for this animal yet.</li>
  {% endfor %}

{% endblock content %}

我似乎无法弄清楚如何真正添加时间表,并使schedule.breakfast == True的实例产生一个复选框。

1 个答案:

答案 0 :(得分:0)

{% for schedule in schedules %}

将此行更改为

 {% for s in schedule %}

您正在上下文中返回{'schedule':schedule}

现在每个条件下的用户s.breakfast等