我创建了一个应用,可以使用django-autoslug根据帖子的slug
自动生成title
。
我已成功显示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})
视图
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'),
)
相关模板
{% for scholarship in scholarship_list %}
<p><a href="{% url scholarship.get_absolute_url %}">Read more →</a></p>
我必须错过一些明显的东西......
答案 0 :(得分:1)
您已将实际完整网址从scholarship.get_absolute_url
传递到{% url %}
代码。您应该使用其中一个,而不是两个:{{ scholarship.get_absolute_url }}
或 {% url 'single_scholarship' scholarship.slug %}
。
答案 1 :(得分:0)
尝试使用
{{ scholarship.get_absolute_url }}
答案 2 :(得分:0)
你可以尝试
<a href="{{scholarship.get_absolute_url}}">Read more →</a>