在Django中找不到反向匹配

时间:2014-09-24 01:16:05

标签: django python-2.7

我弄坏了什么。错误如下:反向'wiki_article_detail',参数'(u'',)'和关键字参数'{}'未找到。我不知道'u'在论证中的来源。这是模型:

class Article(models.Model):
    """Represents a wiki article"""
    title = models.CharField(max_length=100)
    slug = models.SlugField(max_length=50, unique=True)
    text = models.TextField()
    author = models.ForeignKey(User)
    is_published = models.BooleanField(default=False, verbose_name="Publish?")
    created_on = models.DateTimeField(auto_now_add=True)
    objects = models.Manager()
    published = PublishedArticlesManager()
    country = models.CharField(max_length=100)
    category = models.CharField(max_length=100)



    def __unicode__(self):
        return self.title

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.title)
        super(Article, self).save(*args, **kwargs)

    @models.permalink
    def get_absolute_url(self):
        return ('wiki_article_detail', (), { 'slug': self.slug })

模板是:

<body>

{% if object_list %}

    <h2 class="articlePageTitle">All Articles</h2>
    <h3>Filter by country</h3>
    <h3>Filter by category</h3>

    <ul>
        {% for article in object_list %}
        <li>
            <a href="{% url wiki_article_detail article.slug %}">{{ article.title }}</a>
        </li>
{% endfor %}
    </ul>

{% else %}
    <h2>No articles have been published yet.</h2>
{% endif %}

    <a href="{% url wiki_article_add %}">Create new article</a

</body> 

调试器指示此行中的错误:

<a href="{% url wiki_article_detail article.slug %}">{{ article.title }}</a>

之前一切正常,我遇到了一些数据库问题并且不得不重新创建db文件,但那里没有多少。但现在,我可以写一篇文章并查看它,但我无法进入/ all列表。

urls.py片段:

 url(r'^all/$',
        'django.views.generic.list_detail.object_list',
       {
            'queryset': Article.published.all(),
        },
        name='wiki_article_index'), 

错误中引用的urls.py片段:

url(r'^article/(?P<slug>[-\w]+)$', 
        'django.views.generic.list_detail.object_detail',
        {
            'queryset': Article.objects.all(),
        },
        name='wiki_article_detail'),

1 个答案:

答案 0 :(得分:0)

尝试以下方法: {{article.title}}