NeIT
NeIT

Reputation: 155

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

Upvotes: 0

Views: 34

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477308

This is called a slug. You can add a slug to your model, for example with a SlugField [Django-doc], but it might be better to install the django-autoslug package, and use an AutoSlugField instead:

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

You can alter your URL patterns to parse a slug like:

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

In your view, you can then process the slug with:

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
        }
    )

Note that you should check if the form is valid, and if it is successful, you better redirect to another view, to implement the Post/Redirect/Get pattern [wiki].

Upvotes: 2

Related Questions