my code is like that:
def post_detail(request, year, month, day, slug):
print(month, type(month))
post = get_object_or_404(Post,
slug=slug,
status='published',
published__year=year
)
print(post.published.month, type(post.published.month))
return render(request, 'blog/post/detail.html', {'post': post})
The output, when I'm calling that view:
7 <class 'int'>
7 <class 'int'>
This shows, that the month is 7 (int), however when I modify it like:
def post_detail(request, year, month, day, slug):
print(month, type(month))
post = get_object_or_404(Post,## Heading ##
slug=slug,
status='published',
published__year=year,
published__month=month
)
print(post.published.month, type(post.published.month))
return render(request, 'blog/post/detail.html', {'post': post})
I now get 404 from get_object_or_404
. Weird thing, I have the same code on my local machine and it works. That one is on the server.
My model is like:
class PublishedManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status='published')
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Roboczy'),
('published', 'Opublikowany'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique_for_date='published')
author = models.ForeignKey(User, related_name='posts', on_delete=models.CASCADE)
body = models.TextField()
published = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
objects = models.Manager()
active = PublishedManager()
def get_absolute_url(self):
date = timezone.localdate(self.published)
return reverse('blog:post_detail',
args=[date.year,
date.strftime('%m'),
date.strftime('%d'),
self.slug])
class Meta:
ordering = ('-published',)
def __str__(self):
return f'{self.title} by {self.author.username.title()}'
I did makemigrations
and migrate
, somebody knows what could be happening here?
EDIT:
I made exacly alike filter query, the raw sql query is like that:
SELECT `blog_post`.`id`, `blog_post`.`title`, `blog_post`.`slug`, `blog_post`.`author_id`, `blog_post`.`body`, `blog_post`.`published`, `blog_post`.`created`, `blog_post`.`updated`, `blog_post`.`status` FROM `blog_post` WHERE (EXTRACT(DAY FROM CONVERT_TZ(`blog_post`.`published`, 'UTC', 'Europe/Warsaw')) = 23 AND EXTRACT(MONTH FROM CONVERT_TZ(`blog_post`.`published`, 'UTC', 'Europe/Warsaw')) = 7 AND `blog_post`.`published` BETWEEN 2017-12-31 23:00:00 AND 2018-12-31 22:59:59.999999 AND `blog_post`.`slug` = grgregthyrj) ORDER BY `blog_post`.`published` DESC;
When I executed that on my database it gave me WRONG SYNTAX. Then I used https://www.piliapp.com/mysql-syntax-check/
on it. It gave me WRONG SYNTAX too. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '23:00:00 AND 2018-12-31 22:59:59.999999 AND
blog_post.
slug= grgregthyrj) OR' at line 1
Then I removed BETWEEN and it was like:
SELECT `blog_post`.`id`, `blog_post`.`title`, `blog_post`.`slug`, `blog_post`.`author_id`, `blog_post`.`body`, `blog_post`.`published`, `blog_post`.`created`, `blog_post`.`updated`, `blog_post`.`status` FROM `blog_post` WHERE (EXTRACT(DAY FROM CONVERT_TZ(`blog_post`.`published`, 'UTC', 'Europe/Warsaw')) = 23 AND EXTRACT(MONTH FROM CONVERT_TZ(`blog_post`.`published`, 'UTC', 'Europe/Warsaw')) = 7 AND `blog_post`.`slug` = grgregthyrj) ORDER BY `blog_post`.`published` DESC;
It was correct according to that tool. Maybe that it the problem?