I have created a two django views home() and machine_detail(). Home view renders an home.html template and pass it a dictionary containing equipment names. my side bar consists of the items in this dictionary which is dynamically generated below these names. The equipment model is related to Machines model and I have used django foreign key relation to make a drop down for each equipment showing all the machines related to that specific equipment. all the machines are in an anchor tag and upon click i want to show a detail of machine page but why cant i see my side bar contents in machine detail template it is extending home.html but still the sidebar is not showing anyhting. please help me
Equipment Model
name = models.CharField(max_length=255)
quantity = models.IntegerField()
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.PROTECT)
contractor = models.ForeignKey(Contractor, on_delete=models.PROTECT, null=True, blank=True)
Machines Model
name = models.CharField(max_length=50)
type_of_machine = models.ForeignKey(Equipment, on_delete=models.CASCADE, related_name='typeOfMachine')
spares = models.ManyToManyField(Spares,related_name='machines' ,blank=True)
dop = models.DateField(verbose_name="Date of Purcahse", blank=True, null=True)
purchase_cost = models.FloatField(default=0)
model = models.CharField(max_length=50,blank=True, null=True)
image = models.ImageField(upload_to='images', blank=True, null=True)
Home View
def home(request):
eq_map = {}
equipment = models.Equipment.objects.all()
for e in equipment:
eq_map[e] = e.typeOfMachine.all()
return render(request, "user/sidebar.html",{'equipments':eq_map})
machine_Detail view
def machine_detail(request,pk):
machine_detail = models.Machines.objects.get(pk=pk)
return render(request, "user/machine_detail.html", {"machine_detail":machine_detail})
home.html
<ul class="list-unstyled components">
{% for equipment,machines in equipments.items %}
<li> {{equipment|safe}} <i class="fa fa-caret-down"></i>
{% for m in machines %}
<a href="{% url 'machineDetail' pk=m.pk %}"><li>{{m}}</li></a>
{% endfor %}
</li>
{% endfor %}
</ul>
machine-detail.html
{% extends "user/home.html" %}
{% load static %}
{% block title %}
Machine Detail
{% endblock title %}
As said before, your sidebar needs data in your dict "equipments".
This data could come from the current view in params added to the render function.
return render(request, 'my_template.html', {'sidebar_data': sidebar_data})
If you want to display it on several pages, you would have do provide this data in all views. The solution to your problem is to user a context processor which is a way to call a kind of global function from your template.