更新我意识到我在下面的解释隐藏了我真正的问题。最终我要完成的是创建一个dashboard.html模板,并能够从我的流量和电子邮件模型中呈现访问/数据。
我正在使用Django 1.8.3和Python 3.4.3
我正在构建一个软件,它将获取输入值并在视图中显示它们 - 简单。目前,我有两个模型/视图,其中包含用于查看数据的各个模板。这可以按预期工作。
我也在创建一个仪表板,应该从我的两个模型中提取数据,这是让我感到悲伤的部分。我在查询集上阅读了很多内容,但是找不到一个关于如何在我完全理解的项目中实现的示例。
让您了解所呈现的内容;此数据是从Google Analytics导出的表格数据,转换为csv,然后从Django admin导入应用程序。我们的想法是能够使用“for”循环将数据作为列表进行查看,这样可行,仪表板能够生成计算数据的可视化图形。
非常感谢任何帮助。我不确定你需要多少信息,所以我将尽可能多地包括在内。谢谢。
models.py
from django.db import models
class Traffic(models.Model):
date = models.DateField()
traffic = models.PositiveIntegerField(null=True)
ifs_signups = models.IntegerField(null=True)
campaigns = models.IntegerField(null=True)
sales = models.PositiveIntegerField(null=True)
def month(self):
return self.date.strftime("%B")
def quarter(self):
item = self.date.strftime("%B")
q1 = (["January", "February", "March"])
q2 = (["April", "May", "June"])
q3 = (["July", "August", "September"])
q4 = (["October", "November", "December"])
if item in q1:
return "1st Quarter"
elif item in q2:
return "2nd Quarter"
elif item in q3:
return "3rd Quarter"
elif item in q4:
return "4th Quarter"
else:
return "No Date Specified"
def conversions(self):
conversion = self.sales / self.traffic
return "%r" % conversion
class Email(models.Model):
date = models.DateField()
subject = models.CharField(max_length=255)
day_of_week = models.CharField(max_length=255)
subject_type = models.CharField(max_length=255)
content_type = models.CharField(max_length=255)
email_list = models.CharField(max_length=255)
recipients = models.PositiveIntegerField(null=True)
unsubscribes = models.PositiveIntegerField(null=True)
bounces = models.PositiveIntegerField(null=True)
open = models.PositiveIntegerField(null=True)
clicks = models.PositiveIntegerField(null=True)
def month(self):
return self.date.strftime("%B")
def open_rate(self):
open_rate = self.open / self.recipients
return "%r" % open_rate
def click_through_rate(self):
click_through_rate = self.clicks / self.open
return "%r" % click_through_rate
views.py
from django.shortcuts import render, redirect
from django.views.generic import ListView
from .models import Traffic
from .models import Email
class TrafficListView(ListView):
model = Traffic
template_name = 'dashboard/pages/traffic.html'
def get_context_data(self, **kwargs):
context = super(TrafficListView, self).get_context_data(**kwargs)
return context
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)
return context
答案 0 :(得分:2)
我建议远离ListView并实现自己的简单TemplateView。
<form method="POST">
<input type="checkbox" name="fields[]" value="1" />
<input type="checkbox" name="fields[]" value="2" />
</form>