0

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 ANDblog_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?

Ginko
  • 385
  • 1
  • 15
  • Are you certain that the object (with `month == 7`) you are querying for does exist? – CoffeeBasedLifeform Jul 23 '18 at 05:48
  • When I did `post = get_object_or_404(Post, slug=slug, status='published', published__year=year )`, then `post.published.month` gave me 7 so it exists. I also see that post on frontend and when i list all posts by `Post.objects.all()` it is there – Ginko Jul 23 '18 at 05:55
  • What is the exception thrown? – Selcuk Jul 23 '18 at 05:55
  • ` No Post matches the given query.` – Ginko Jul 23 '18 at 05:57
  • You may want to examine how timezone of production is different from local especially if it's the first/last day of the month, no? – Oluwafemi Sule Jul 23 '18 at 06:01
  • Timezone is exacly the same :( But database is different, I have mysql on the server and sqlite locally – Ginko Jul 23 '18 at 06:05
  • Ouwafemi might be on to something: [Problems filtering django datetime field by month and day](https://stackoverflow.com/questions/21918802/problems-filtering-django-datetime-field-by-month-and-day) – CoffeeBasedLifeform Jul 23 '18 at 06:15
  • I updated the post, could you read the edit? – Ginko Jul 23 '18 at 06:30

0 Answers0