Hello, I am struggling with trying to filter child objects based on parent
object selection on index.html (sites.html in example). On my sites.html, I
list the office sites and a button to click for each, upon clicking I want
to load the profile list for users only at that site, presently it's
loading all profiles for all sites, which I assume is because the profiles
are loaded statically no matter which button is clicked on sites.html.
I'm thinking I need a JS onclick event?
models.py:
class Site(models.Model):
name = models.CharField(max_length=50)
date = models.DateField()
manager = models.CharField(max_length=50)
def save(self, *args, **kwargs):
super(Site, self).save(*args, **kwargs)
class Profile(models.Model):
Days = '1st'
Mids = '2nd'
Nights = '3rd'
Work_Schedule_Choices = [
(Days, 'Day Shift'),
(Mids, 'Mid Shift'),
(Nights, 'Night Shift'),
]
sitename = models.ForeignKey(Site, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
schedule = models.CharField(max_length=3,choices=Work_Schedule_Choices,
default=Days)
totalusers = models.PositiveSmallIntegerField(default=1, validators=[
MinValueValidator(1), MaxValueValidator(50)])
views.py:
def sites(request):
sitelist = Site.objects.all()
return render (request, 'App/sites.html', {'sitelist' : sitelist})
def sitedetail(request):
site = Site.objects.filter()
if request.method == 'GET':
return render(request, 'App/site-detail.html', {'site': site,
'profile_set': Profile.objects.all()})
sites.html (index)
{% extends 'App\base.html' %}
{% load crispy_forms_tags %}
{% block title %}Site Home{% endblock %}
{% block content %}
{% for Site in sitelist %}
<div <div class="container">
<div class="row justify-content-center">
<h5>{{ Site.name }}</h5>
<a href="{% url 'site-detail' %}" class="btn">View</a>
</div>
</div>
{% empty %}
<div class="noproject-wrapper center">
<h3 class="grey-text">Sorry, you haven't created any sites yet.</h3>
<a href="{% url 'site-create' %}" class="btn-large grey">
<i class="material-icons white-text left">Add Site</i>
<span class="bold">Add Site</span>
</a>
</div>
{% endfor %}
</div>
{% endblock %}
site-detail.html
{% extends 'App\base.html' %}
{% load crispy_forms_tags %}
{% block title %}Site Detail{% endblock %}
{% block content %}
<h1>This web form sucks</h1>
<ul>
{% for profile in profile_set %}
<li>{{ profile.title }}</li>
{% endfor %}
</ul>
{% endblock %}
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/b4bd97c3-1a36-436d-823a-b4f7003a29d0%40googlegroups.com.