0

我有一个用于获取最新条目的模板标签,但我似乎无法访问“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。为什么要尝试导入另一个应用程序?

4

2 回答 2

1

get_absolute_url您忘记调用reverse网址时。您正在传递元组,这使其无效。你get_absolute_url应该是:

from django.core.urlresolvers import reverse

def get_absolute_url(self):
        creation_date = timezone.localtime(self.start_publication)
        return reverse('entry_detail', kwargs={
            'year': creation_date.strftime('%Y'),
            'month': creation_date.strftime('%b').lower(),
            'day': creation_date.strftime('%d'),
            'slug': self.slug})

permalink不推荐使用装饰器,reverse建议使用

于 2013-07-03T12:28:24.683 回答
0

You have an error somewhere else in your project, in an app that is referenced in urls.py.

The reverse URL lookup functionality (which you've implicitly used by using the @permalink decorator) has to import all views referenced in your urls.py file in order to calculate the correct value. If you have an error somewhere, it will fail.

You should find the reference to app2, wherever it is, and fix it.

于 2013-07-03T11:55:03.790 回答