E
E
Eugene2016-02-25 14:32:24
Django
Eugene, 2016-02-25 14:32:24

How to implement in django the output of the previous article in the body of the current one?

Good afternoon. I'm trying to implement the output of the previous article, i.e. the one that was published before the one in which the filtering of the call to the previous article occurs , inside the current one and something, somehow there is not enough understanding of how to write the filter in the view.
Here is such a twist

from django.shortcuts import render, get_object_or_404
from project.models import Project

def show_project(request, slug):
    project_content = Project.objects.all()
    project_back = Project.objects.filter(id__gt=project_content.id).order_by('-id')[0]
    project = get_object_or_404(Project, slug=slug)
    return render(request, 'project_tpl.html', {'project':project, 'project_back':project_back, 'project_content':project_content})

Swears 'QuerySet' object has no attribute 'id'
UP
model
from django.db import models
from datetime import datetime

class Project(models.Model):
    title = models.CharField(max_length=200, verbose_name='Заголовок')
    slug = models.SlugField('URL', blank='true')
    text = models.TextField(verbose_name='Описание')
    work = models.TextField(verbose_name='Что делали')
    work_list = models.CharField(max_length=200, verbose_name='Перечень работ')
    content = models.TextField(verbose_name='Основное контент')
    create_date = models.DateTimeField('Дата создания', default=datetime.now)
    images = models.ImageField(upload_to='static/img/portfolio', verbose_name='Изображение')
    images_back = models.ImageField(upload_to='static/img/portfolio', verbose_name='Предыдущий проект')
    seo_title = models.CharField(max_length=72, verbose_name='СЕО Заголовок', blank='true')
    seo_description = models.CharField(max_length=124, verbose_name='СЕО Описание', blank='true')
    seo_keywords = models.TextField(max_length=1000,verbose_name='СЕО Ключи', blank='true')

    class Meta:
        verbose_name = 'Проект'
        verbose_name_plural = 'Портфолио'

    def __str__(self):
        return self.title

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Voronkov, 2016-02-25
@dragonesis

def show_project(request, slug):
    # Это список объектов, а не объект, вот и ошибка
    project_content = Project.objects.all()

# может так лучше:
    project = get_object_or_404(Project, slug=slug) 
    project_back = Project.objects.filter(id__gt=project.id).order_by('-id')[:1]
    return render(request, 'project_tpl.html', {'project':project, 'project_back':project_back, 'project_content':project_content})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question