Необходимо перезаписать поле visit_date после перехода на одну из статей, пока получается новая запись в БД без привязки к id статьи.
def detail(request, article_id):
try:
a = Article.objects.get(id=article_id)
except:
raise Http404('Статья не найдена')
visit_date = Article(visit_date=datetime.now())
visit_date.save()
print('check data', visit_date)
latest_comment_list = a.comment_set.order_by('-id')[:10]
return render(request, 'articles/detail.html', {'article': a, 'latest_comment_list': latest_comment_list,
'visit_data': visit_date})
class Article(models.Model):
ar_title = models.CharField('article title', max_length=200)
publishes_date = models.DateTimeField()
visit_date = models.DateTimeField()
ar_text = RichTextField(blank=True)
def __str__(self):
return self.ar_title
def was_published_recently(self):
return self.publishes_date >= (timezone.now() - datetime.timedelta(days=7))
def was_visited_recently(self):
return self.visit_date >= (timezone.now() - datetime.timedelta(days=7))
class Meta:
verbose_name = "Статья"
verbose_name_plural = "Статьи"