首页 > 解决方案 > I need that when I click on an article <> it’s called the name of the article

问题描述

I need that when I click on an article <> it’s called the name of the article

For example, now, when I click on an article, I go to url address news / 1, but I need instead of 1, there was a name similar to this picture. Or here’s another premiere, I call the article “How to Skip School” and url the address will be like this news/how-to-skip-school

enter image description here

views.py

class ArticleIndex(ListView):
    model = Articles
    template_name = 'news/posts.html'

def ArticleDetailView(request, pk):

      tag=None
      Articles.objects.filter(pk=pk).update(view=F('view') + 1)
      Articles.objects.all()
      article_details = Articles.objects.filter(pk=pk).first()

      if request.method == 'POST':
          comment_form = Comments(request.POST)
          comment_form.save()
      else:
          comment_form = Comments()

      commentss = CommentModel.objects.all()

      return render(request, 'news/post.html', {'article_details': article_details,
                                                'comment_form': comment_form, 'comments': commentss,
                                                'tag': tag
                                                })

urls.py

path('', ArticleIndex.as_view(), name='articles_list'),
path('<int:pk>/', views.ArticleDetailView, name='article_detail'),

models.py

class Articles(models.Model):
    title = models.CharField(max_length= 200)
    post = models.TextField()
    date = models.DateTimeField()
    img = models.ImageField(upload_to='', default="default_value",verbose_name='Каритинка 260х180')
    tags = TaggableManager()
    article_like = models.IntegerField(default='0')
    article_dislike = models.IntegerField(default='0')
    view = models.IntegerField(default='0')
    datesArticle = models.DateTimeField(auto_now=True)


    class Meta:
        ordering = ['-datesArticle']

    def __str__(self):
        return self.title

标签: pythondjangourl

解决方案


这称为蛞蝓。您可以将 slug 添加到模型中,例如使用SlugField[Django-doc],但最好安装django-autoslugpackageAutoSlugField改用:

from django.db import models
from autoslug import AutoSlugField

class Articles(models.Model):
    title = models.CharField(max_length= 200)
    slug = AutoSlugField(populate_from='title')
    post = models.TextField()
    date = models.DateTimeField()
    img = models.ImageField(upload_to='', default='default_value', verbose_name='Каритинка 260х180')
    tags = TaggableManager()
    article_like = models.IntegerField(default='0')
    article_dislike = models.IntegerField(default='0')
    view = models.IntegerField(default='0')
    datesArticle = models.DateTimeField(auto_now=True)


    class Meta:
        ordering = ['-datesArticle']

    def __str__(self):
        return self.title

你可以改变你的 URL 模式来解析这样的 slug:

path('', ArticleIndex.as_view(), name='articles_list'),
path('<slug:slug>/', views.article_detail_view, name='article_detail'),

在您看来,您可以使用以下方法处理 slug:

def ArticleDetailView(request, slug):
    tag=None
    Articles.objects.filter(slug=slug).update(view=F('view') + 1)
    article_details = Articles.objects.filter(slug=slug).first()
    if request.method == 'POST':
        comment_form = Comments(request.POST)
        if comment_form.is_valid():
            comment_form.save()
            return redirect('some-view-name')
    else:
        comment_form = Comments()
    comments = CommentModel.objects.all()
    return render(
        request,
        'news/post.html',
        {
           'article_details': article_details,
           'comment_form': comment_form,
           'comments': comments,
            'tag': tag
        }
    )

请注意,您应该检查表单是否有效,如果成功,您最好重定向到另一个视图,以实现Post/Redirect/Get模式 [wiki]


推荐阅读