Django ListVi中链接的NoReverseMatch异常

2024-05-16 13:34:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个应用程序,它使用django-autoslug根据帖子的title自动生成slug。在

我已经成功地在ListView中显示了每个帖子,但是链接到DetailView时遇到了问题。思想?在

我收到以下错误:

Reverse for '/scholarships/test-scholarship-1/' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

型号

from django.db import models
from django.core.urlresolvers import reverse
from autoslug import AutoSlugField


class Scholarship(models.Model):
    title = models.CharField(max_length=255)
    slug = AutoSlugField(populate_from='title')

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('single_scholarship', kwargs={'slug': self.slug})

视图

^{pr2}$

网址

from django.conf.urls import patterns, url

from .views import ScholarshipDirectoryView, SingleScholarshipView

urlpatterns = patterns('',
    url(r'^$', ScholarshipDirectoryView.as_view(), name='scholarship_directory'),
    url(r'^(?P<slug>[A-Za-z0-9_\-]+)/$', SingleScholarshipView.as_view(), name='single_scholarship'),
)

相关模板

{% for scholarship in scholarship_list %}
<p><a href="{% url scholarship.get_absolute_url %}">Read more &rarr;</a></p>

我一定是错过了一些很明显的东西。。。在


Tags: djangofromimportselfurlfortitlemodels
3条回答

尝试使用

{{ scholarship.get_absolute_url }}

你可以试试

<a href="{{scholarship.get_absolute_url}}">Read more &rarr;</a>

您将实际的完整URL从scholarship.get_absolute_url传递到{% url %}标记。您应该使用其中一个或另一个,而不是两个都使用:要么{{ scholarship.get_absolute_url }}{% url 'single_scholarship' scholarship.slug %}。在

相关问题 更多 >