I am starting to use Django and while I have read the book and googled away I am afraid that I am not making the connection on some concepts to be able to resolve this.
I need to show several values corresponding to particular companies. The user first selects three companies to compare and then the response shows the data for those companies.
In my view, I create the querysets that I iterate in my template to show the data as follows:
VIEWS EXCERPT:
C1 = form.cleaned_data['Company1']
C2 = form.cleaned_data['Company2']
C3 = form.cleaned_data['Company3']
company_names = [C1,C2,C3]
company1 = (Company_stats.objects.filter(period__exact=P, company_name__exact=C1))
company2 = (Company_stats.objects.filter(period__exact=P, company_name__exact=C2))
company3 = (Company_stats.objects.filter(period__exact=P, company_name__exact=C3))
company_list = [company1,company2,company3]
TEMPLATE EXCERPT:
{%for c in company_list%}
{%for z in c %}
{{ z.company_name }}
{{ z.nxt_m_ret_est }}
{{ z.nxt_m_ret_rat }}
{% endfor %}
{% endfor %}
This works – however, as I have many more moving parts (I have simplified the view for the purpose of my question) it turns into an endless laundry list of querysets – I am 100% sure this is not DRY (in spite of the laundry reference 😉 Not to mention the length of the context.
I would really appreciate it if someone could help me learn to do this in the proper way. Thank you very much in advance!
Try this:
Even if this doesn’t exactly fit your need, something similar should work.