我有一个用于获取最新条目的模板标签,但我似乎无法访问“get_absolute_url”函数。
我的错误是
No module named app2 (not the name of the app I'm trying to use)
我的新闻模型是这样的:
STATUS_CHOICES = (
(DRAFT, _('Draft')),
(HIDDEN, _('Hidden')),
(PUBLISHED, _('Published'))
)
TYPE_CHOICES = (
('normal', _('Normal')),
('special', _('Special')),
)
class Entry(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255)
body = RichTextField(null=True, blank=True)
start_publication = models.DateTimeField(blank=True, null=True)
end_publication = models.DateTimeField(blank=True, null=True)
status = models.IntegerField(choices=STATUS_CHOICES, default=DRAFT)
arttype = models.CharField(max_length=10, choices=TYPE_CHOICES, default='normal', )
objects = models.Manager()
published = EntryPublishedManager()
def __unicode__(self):
return self.title
class Meta:
ordering = ['-start_publication']
get_latest_by = 'creation_date'
@models.permalink
def get_absolute_url(self):
creation_date = timezone.localtime(self.start_publication)
return ('entry_detail', (), {
'year': creation_date.strftime('%Y'),
'month': creation_date.strftime('%b').lower(),
'day': creation_date.strftime('%d'),
'slug': self.slug})
我的 urls.py 是这样的:
urlpatterns = patterns('',
url(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$', DateDetailView.as_view(allow_future=True, date_field='start_publication', queryset=Entry.objects.all()), name='entry_detail'),
)
我的模板标签 latest_news.py:
from django import template
from apps.news.models import Entry
register = template.Library()
def show_news():
entry = Entry.published.filter(arttype='normal').order_by('-start_publication')[:4]
return entry
register.assignment_tag(show_news, name='latest_news')
我的 frontpage.html 模板:
{% latest_news as latest_news %}
{% for entry in latest_news %}
<h2>{{ entry.title }}</h2>
<p><a href="{{ entry.get_absolute_url }}">Read more</a></p>
{% endfor %}
{{ entry.title }} 工作正常。但不是 .get_absolute_url。为什么要尝试导入另一个应用程序?