【问题标题】:Reverse match not found in Django在 Django 中找不到反向匹配
【发布时间】:2014-09-24 01:16:04
【问题描述】:

我弄坏了东西。错误内容为:“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 问题,不得不重新创建 db 文件,但里面没有太多东西。但是现在,我可以写一篇文章并查看它,但我无法进入 /all 列表。

urls.py sn-p:

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

错误中引用的urls.py sn-p:

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

【问题讨论】:

  • 请从您的urls.py发布适当的sn-p
  • 添加了 urls.py sn-p
  • 顺便说一下,Python 中字符串前面的 'u' 只是表示它是一个 unicode 字符串 - docs.python.org/2/tutorial/introduction.html#unicode-strings
  • 是的,我也发现了。谢谢。
  • 似乎对于一个条目,slug 字段没有被写入,它破坏了整个事情。我从数据库中删除了错误的条目,它起作用了。

标签: django python-2.7


【解决方案1】:

试试这个: {{ 文章标题 }}

【讨论】:

    猜你喜欢
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 2012-10-21
    • 2017-04-25
    相关资源
    最近更新 更多