我有两个模型用于餐厅,另一个用于操作时间。操作时间与餐厅有外键关系,因为一周内不同日子的操作时间可能不同。我想表明餐厅在当前时间和当天是开放还是关闭。在views.py中对此进行编码或为此创建模板标记会更好吗?因为惯例说视图应该很薄。
用于餐厅和营业时间的模型
class Restaurant(models.Model):
owner = models.ForeignKey(User)
name = models.CharField(max_length=150, db_index=True)
address = models.CharField(max_length=100)
class OperatingTime(models.Model):
MONDAY = 1
TUESDAY = 2
WEDNESDAY = 3
THURSDAY = 4
FRIDAY = 5
SATURDAY = 6
SUNDAY = 7
DAY_IN_A_WEEK = (
(MONDAY, 'Monday'),
(TUESDAY, 'Tuesday'),
(WEDNESDAY, 'Wednesday'),
(THURSDAY, 'Thursday'),
(FRIDAY, 'Friday'),
(SATURDAY, 'Saturday'),
(SUNDAY, 'Sunday'),
)
# HOURS = [(i, i) for i in range(1, 25)]
restaurant = models.ForeignKey(Restaurant,related_name="operating_time")
opening_time = models.TimeField()
closing_time = models.TimeField()
day_of_week = models.IntegerField(choices=DAY_IN_A_WEEK)
def __str__(self):
return '{} ---- {}'.format(self.opening_time, self.closing_time)
views.py
def home(request):
restaurant = Restaurant.objects.all()
print('restaurant',restaurant)
operating_time = OperatingTime.objects.all()
print('operating time',operating_time)
for operating_time in operating_time: # Tried to find if restaurant is opened or closed based on opening time & closing time in current time and day for each restaurant
opening = operating_time.opening_time
closing = operating_time.closing_time
print('opening',opening)
current_time = datetime.now()
current_time = current_time.time()
if current_time < closing or opening< current_time:
print('opening')
else:
print('closed')
return render(request, 'restaurant/homepage.html', {'restaurant':restaurant})
我怎么能找到这个?在视图上编码或创建模板标记更好吗?
答案 0 :(得分:0)
我现在在我的工作地点与django工作了大约一个月,并且被教导不要在观点中加入任何逻辑。该视图应仅用于传递数据库中的数据或将数据发布到数据库。逻辑应该在模板标签中完成。