Python Django ListView中链接的NoReverseMatch异常
我已经创建了一个应用程序,可以根据帖子的Python Django ListView中链接的NoReverseMatch异常,python,django,Python,Django,我已经创建了一个应用程序,可以根据帖子的标题自动生成一个slug,使用 我已经成功地在列表视图中显示了每个帖子,但是链接到详细视图时遇到了问题。想法 我收到以下错误: 未找到参数为“()”且关键字参数为“{}”的“/scholarship/test-scholarship-1/”的反向。已尝试0个模式:[] 型号 from django.db import models from django.core.urlresolvers import reverse from autoslug impo
标题自动生成一个slug
,使用
我已经成功地在列表视图中显示了每个帖子,但是链接到详细视图时遇到了问题。想法
我收到以下错误:
未找到参数为“()”且关键字参数为“{}”的“/scholarship/test-scholarship-1/”的反向。已尝试0个模式:[]
型号
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})
from django.views.generic import ListView, DetailView
from .models import Scholarship
class ScholarshipDirectoryView(ListView):
model = Scholarship
template_name = 'scholarship-directory.html'
class SingleScholarshipView(DetailView):
model = Scholarship
template_name = 'single-scholarship.html'
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'),
)
视图
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})
from django.views.generic import ListView, DetailView
from .models import Scholarship
class ScholarshipDirectoryView(ListView):
model = Scholarship
template_name = 'scholarship-directory.html'
class SingleScholarshipView(DetailView):
model = Scholarship
template_name = 'single-scholarship.html'
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'),
)
URL
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})
from django.views.generic import ListView, DetailView
from .models import Scholarship
class ScholarshipDirectoryView(ListView):
model = Scholarship
template_name = 'scholarship-directory.html'
class SingleScholarshipView(DetailView):
model = Scholarship
template_name = 'single-scholarship.html'
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'),
)
来自django.conf.url导入模式,url
从.views导入ScholarshipDirectoryView,SingleScholarshipView
urlpatterns=模式(“”,
url(r'^$',ScholarshipDirectoryView.as_view(),name='scholarship_directory'),
url(r'^(?P[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>
{%用于奖学金中的奖学金\u列表%}
我必须错过一些非常明显的东西…您正在传递来自奖学金的实际完整URL。获取{%URL%}
标记的绝对URL。你应该使用其中一个,而不是两个:要么{{scholarship.get_absolute_url}
要么{%url'single_scholarship'scholarship.slug%}
尝试使用
{{ scholarship.get_absolute_url }}
你可以试试
<a href="{{scholarship.get_absolute_url}}">Read more →</a>