模板上的Django get_absolute url错误

时间:2016-04-17 20:11:32

标签: django django-models

使用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)

我的模型看起来像这样

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])

我不理解错误and keyword arguments '{}' not found.的这一部分,因为之前它说它发现了这个参数...with arguments '(2016, '04', '17', ...

1 个答案:

答案 0 :(得分:0)

我必须slugify我的db

中的所有slu ..
from django.template.defaultfilters import slugify

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