我正在制作一个django应用程序,显示大学网站的例行订单。特定订单应在网站上保留一周,然后消失。这是我的模特。
class RoutineOrder(models.Model):
Subject = models.TextField()
Details = RichTextUploadingField()
LastEdited = models.DateTimeField(auto_now=True)
Added = models.DateTimeField(auto_now_add=True)
def get_absolute_url(self):
return reverse('nitwro:home')
class Home(TemplateView):
template_name = 'nitwro/home.djt'
startdate = datetime.today()
enddate = startdate + timedelta(days=6)
def get_context_data(self, **kwargs):
context = super(Home, self).get_context_data(**kwargs)
context['allorders'] = RoutineOrder.objects.filter().order_by('-Added')
return context
怎么做才能做到这一点?
答案 0 :(得分:2)
将传递给视图的对象过滤为仅过去一周创建的对象;
func PDTForamte(endTime: String) -> String{
// create dateFormatter with UTC time format
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss+zzzz"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
guard let date = dateFormatter.date(from: endTime) else {
return endTime.components(separatedBy: "T").first ?? "" //return date before 'T'.
}
let pdtFormatter = DateFormatter()
pdtFormatter.dateStyle = .long
pdtFormatter.timeStyle = .long
if let americaZone = NSTimeZone(name: "America/Los_Angeles") {
pdtFormatter.timeZone = americaZone as TimeZone
}
let dateString: String = pdtFormatter.string(from: date)
let calendar = NSCalendar.autoupdatingCurrent
//We subtract 1 min because American Date here be 12:00:00 AM but It should be return to the previous day
let newDate = calendar.date(byAdding: .minute, value: -1, to: pdtFormatter.date(from: dateString)!) ?? Date()
pdtFormatter.dateFormat = "yyyy-MM-dd"
let newDateString: String = pdtFormatter.string(from: newDate)
return newDateString
}
使用小写字段名称也是一种好习惯。