django - Filtering Objects in Database by Keyword -


i using django 1.8.3 , python 3.4.3

i try explain makes sense, please bear me. i've imported csv file database 50 rows , 10 columns of data, consists of data pulled google analytics , our email marketing campaign data. 1 of columns 'day_of_week'. need count of database rows have keyword of day need...the way can figure out how code below, man, sure seems more dynamic , cleaner.

is there way filter in way can use tag in template, or cleaner this? thank help.

class emaillistview(listview):     model = email     template_name = 'dashboard/pages/email.html'      def get_context_data(self, **kwargs):         context = super(emaillistview, self).get_context_data(**kwargs)         days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']         months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october',                   'november', 'december']         subject_type = ['offer', 'sell', 'fear', 'learn']         content_type = ['offer', 'sell', 'fear', 'learn']         email_list = ['fmg - business', 'fmg - residential', 'ae', 'iba']          total_campaigns_monday = {}         total_campaigns_tuesday = {},         total_campaigns_wednesday = {},         total_campaigns_thursday = {},         total_campaigns_friday = {},         total_campaigns_saturday = {},         total_campaigns_sunday = {},         total_recipients = {}         total_unsubscribes = {}         total_bounces = {}         total_open = {}         total_clicks = {}          campaigns in days:             total_campaigns_monday = email.objects.filter(day_of_week='monday').count()             total_campaigns_monday = email.objects.filter(day_of_week='tuesday').count()             total_campaigns_monday = email.objects.filter(day_of_week='wednesday').count()             total_campaigns_monday = email.objects.filter(day_of_week='thursday').count()             total_campaigns_monday = email.objects.filter(day_of_week='friday').count()             total_campaigns_monday = email.objects.filter(day_of_week='saturday').count()             total_campaigns_monday = email.objects.filter(day_of_week='sunday').count() 

and snippet of template ( notice first different others.)

{% if email_list %} <tr>    <td>monday</td>    <td>{{ total_campaigns_monday }}</td>    <td>{{ total_recipients.monday }}</td>    <td>{{ total_unsubscribes.monday  }}</td>    <td>{{ total_bounces.monday  }}</td>    <td>{{ total_open.monday  }}</td>    <td>{{ total_clicks.monday  }}</td>    <td>{% average total_open.monday total_recipients.monday %}</td>    <td>{% average total_clicks.monday total_open.monday %}</td>    </tr> {% endif %} 

i hope understood queston.

replace total_campaigns_<day_of_week> statements , for loop with

total_campaigns = {} day in days:     total_campaigns[day] = email.objects.filter(day_of_week=day).count() 

in templates can use {{ total_campaigns.monday }} or {{ total_campaigns.sunday }} or similar.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -