L
L
lisaG2021-11-26 11:49:42
Django
lisaG, 2021-11-26 11:49:42

Can't go to a single post page from the news list?

I'm trying to move from the all news page to one post, but I stay on the all news page.
Models.py

class News(models.Model):
    title = models.CharField(max_length=1000, verbose_name='Название')
    slug = models.SlugField(max_length=200, db_index=True)
    image = models.ImageField(upload_to='articles/', verbose_name='Фото')
    publish = models.DateTimeField(default=timezone.now, verbose_name='Дата публикации')
    author = models.ForeignKey(User, related_name='news', on_delete=models.CASCADE, verbose_name='Автор', null=True)
    text = models.TextField(verbose_name='Текст')
    tags = models.ManyToManyField(Tag, related_name='news', verbose_name='Тэг')
    created = models.DateTimeField(auto_now_add=True, verbose_name='Дата создания')
    updated = models.DateTimeField(auto_now=True, verbose_name='Дата обнавления')
    status = models.CharField(max_length=30, choices=STATUS_CHOICES, default='опубликован', verbose_name='Статус')


class Meta:
    ordering = ('title',)
    verbose_name = 'Новости'
    verbose_name_plural = 'Новости'

def __str__(self):
    return self.title

def get_absolute_url(self):
    return reverse('post', kwargs={'post_slug': self.slug})


Views.py

def list_news(request):
    news = News.objects.all()
    return render(request,
                  'diplom/news/post.html',
                   {'news': news})

   def single_news(request, post_slug):
        post = get_object_or_404(News, slug=post_slug)
        return render(request,
                      'diplom/news/blog-single.html',
                      {'post': post})


urls.py
urlpatterns = [
   path('news/', views.list_news, name='News'),
   path('post/<slug:post_slug>/', views.single_news, name='post')
]


templates
<div class="col-lg-4 col-md-6 grid-item">
        {% for news in news %}
           <div class="blog-item large-item set-bg">{{ news.image }}
               <a href="{{post.get_absolute_url}}" methods="post" >
                     <div class="categories">{{ news.title }}</div>
                        <h5>{{ news.text| linebreaks|truncatechars:200 }}</h5>
               </a>
                    <div>
                        {{ news.publish }}
                        {{ news.tag }}
                    </div>
            </div>
         {%endfor%}
      </div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robert Goodman, 2022-03-01
@Robertgoodman

<a href="{% url "post" news.id %}"  >
                     <div class="categories">{{ news.title }}</div>
                        <h5>{{ news.text| linebreaks|truncatechars:200 }}</h5>
               </a>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question