我创建了一个应用程序,它使用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 →</a></p>
我一定是错过了一些很明显的东西。。。在
尝试使用
你可以试试
您将实际的完整URL从
scholarship.get_absolute_url
传递到{% url %}
标记。您应该使用其中一个或另一个,而不是两个都使用:要么{{ scholarship.get_absolute_url }}
或{% url 'single_scholarship' scholarship.slug %}
。在相关问题 更多 >
编程相关推荐