Я пытаюсь перейти со страницы со всеми новостями на один пост, но я остаюсь на странице со всеми новостями.
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>