我的代码是这样的:
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})
当我调用该视图时的输出:
7 <class 'int'>
7 <class 'int'>
这表明月份是7(int),但是当我像这样修改它时:
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})
我现在从get_object_or_404
得到404。奇怪的是,我在本地计算机上具有相同的代码,并且可以正常工作。那是在服务器上。
我的模型是这样的:
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()}'
我做了makemigrations
和migrate
,有人知道这里会发生什么吗?
编辑:
我做了一个非常相似的过滤器查询,原始的sql查询是这样的:
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;
当我在数据库上执行该操作时,它给了我错误的语法。然后,我在其上使用了https://www.piliapp.com/mysql-syntax-check/
。它也给了我错误的语法。 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 .
{= grgregthyrj) OR' at line 1
然后我删除了BETWEEN,就像这样:
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;
根据该工具,它是正确的。也许是问题所在?