I want the user to use the BooleanField to pick which meals said animal eats (breakfast, lunch, dinner, a combination of these possibly). And if the checkbox is checked I then want it to produce a schedule on screen where you can check off if the animal has eaten said meal that day. The issue is whenever I add my schedule, it will not produce a schedule where I can check off if the animal has eaten. (I know this explanation may sound confusing).
Here are my models for Animal and Schedule:
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)
Here is my views for adding a schedule and viewing it(on the animal view):
@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)
Here is my adding schedule template:
{% 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 %}
And here is the template where the schedule is supposed to be seen:
{% 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 %}
I can't seem to figure out how to get the schedule actually added and make the instance of schedule.breakfast == True
to produce a checkbox.
{% for schedule in schedules %}
Change this line to
{% for s in schedule %}
You are returning {'schedule':schedule} in the context
Now user s.breakfast etc for each condition
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.