Django - 单个帖子视图,上一个/下一个链接

时间:2016-08-04 08:24:39

标签: python django

我已经构建了一个博客应用程序作为django教程的一部分,我可以使用djangoproject - https://docs.djangoproject.com/en/1.9/topics/pagination/#using-paginator-in-a-view中的代码对博客列表视图进行分页。我只是在根据当前页面发布视图检索Prev / Next url slug链接时遇到问题。

model.py

class Film(Timestamp):
title = models.CharField(max_length=255)
slug = models.SlugField(unique=True)
image = ImageField(upload_to='thumb')
video = EmbedVideoField(blank=True)
director = models.CharField(max_length=255,blank=True)
cinematographer = models.CharField(max_length=255,blank=True)
producer = models.CharField(max_length=255,blank=True)
publish = models.BooleanField(default=False)
date_published = models.DateTimeField()

# override the admin name
class Meta:
    verbose_name_plural = "Film Projects"

def __unicode__(self):
    return self.title

# helper method
def get_absolute_url(self):
    return "/film/%s/" % self.slug

def save(self, *args, **kwargs):
        super(Film, self).save(*args, **kwargs)

views.py

# film single
def film_detail(request, slug):

film = Film.objects.get(slug=slug)

def get_next(self):
    next_post = Film.get_next_by_date_published()
    if next:
        return next.first()
    return False

def get_next(self):
    prev_post = Film.get_previous_by_date_published()
    if prev:
       return prev.first()
    return False

return render(request, 'film/film_detail.html', {
    'film': film,
})

urls.py

url(r'^film/$', views.film_list, name='film_list'),
url(r'^films/(?P<slug>[-\w]+)/$', views.film_detail, name='film_detail'),

film_detail.html

<a href="{{ film.get_next_by_date_published }}">Next</a><br>
<a href="{{ film.get_previous_by_date_published }}">Previous</a>

以上链接返回下一个和上一个帖子标题,而不是slug并包括当前帖子slug,例如 - http://127.0.0.1:8000/films/sea-chair/Can主席。

对于这么简单的事情(虽然我是django和python的新手),我花了几天时间研究没有运气,我希望有人可以提供帮助!

1 个答案:

答案 0 :(得分:2)

{{ film.get_next_by_date_published }}会返回film个对象。要将其转换为网址,您需要访问film.get_next_by_date_published.slug

您可以对模板中的网址进行硬编码

<a href="/films/{{ film.get_next_by_date_published }}">Next</a>

然而,使用{% url %}标记会更好。

<a href="{% url 'film_detail' film.get_previous_by_date_published.slug %}">Next</a>

下一个问题是get_next_by_date_publishedget_previous_by_date_published可能会引发DoesNotExist例外,如果您已分别在最后一部或第一部电影中。

我建议在视图中提取下一部和之前的电影,而不是尝试在模板中进行。请注意,我已使用get_object_or_404快捷键来处理不存在带有该slug的电影的情况。

from django.shortcuts import get_object_or_404

def film_detail(request, slug):

    film = get_object_or_404(Film, slug=slug)
    try:
        next_film = film.get_next_by_date_published()
    except Film.DoesNotExist:
        next_film = None

    try:
        previous_film = film.get_previous_by_date_published()
    except Film.DoesNotExist:
        previous_film = None

    return render(request, 'film/film_detail.html', {
        'film': film,
        'next_film': next_film,
        'previous_film': previous_film
    })

然后在您的模板中,在显示链接之前检查next_film是否存在:

{% if next_film %}
<a href="{% url 'film_detail' next_film.slug %}">Next</a>
{% else %}
This is the last film!
{% endif %}