模板上的Django get_absolute url错误

时间:2022-09-04 12:15:48

With django I have this following error for the .get_absolute_url method:

使用django,我对.get_absolute_url方法有以下错误:

NoReverseMatch: Reverse for 'post_detail' with arguments '(2016, '04', '17', 'ab quae natus laboriosam pariatur inventore odio quisquam aliquam est quas aliquid')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['blog/(?P<year>\\d{4})/(?P<month>\\d{2})/(?P<day>\\d{2})/(?P<post>[-\\w]+)/$']

ipython shell

     41     author = models.ForeignKey(User, related_name='blog_posts')
     42     body = models.TextField()
---> 43     publish = models.DateTimeField(default=timezone.now)
     44     created = models.DateTimeField(auto_now_add=True)
     45     updated = models.DateTimeField(auto_now=True)

my model look like this

我的模特看起来像这样

class Post(models.Model):
    STATUS_CHOICES = (
            ('draft', 'Draft'),
            ('published', 'Published'),
            )
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250, unique_for_date="publish")
    author = models.ForeignKey(User, related_name='blog_posts')
    body = models.TextField()
    publish = 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()
    published = PublishedManager()


    class Meta:
        ordering = ('-publish', )

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('blog:post_detail', args=[self.publish.year, 
            self.publish.strftime('%m'),
            self.publish.strftime('%d'),
            self.slug])

I don't understand this part of the error and keyword arguments '{}' not found. because previously it said that it found this argument ...with arguments '(2016, '04', '17', ...

我不明白这部分错误和关键字参数“{}”未找到。因为之前它说它发现了这个论点......带有参数'(2016,'04','17',...

1 个解决方案

#1


0  

I had to slugify all the slugs in my db

我不得不将我的数据库中的所有slu slu slu ..

from django.template.defaultfilters import slugify

bpost = Post.objects.all()
for p in bpost():
    p.title = slugify(title)
    p.save()

#1


0  

I had to slugify all the slugs in my db

我不得不将我的数据库中的所有slu slu slu ..

from django.template.defaultfilters import slugify

bpost = Post.objects.all()
for p in bpost():
    p.title = slugify(title)
    p.save()