Akshat
Akshat

Reputation: 41

How to manage slug in views.py django

I'm having trouble dealing with slugs: I have defined different slugs for every article in admin panel but on my website, if I type any random slug(e.g- fvrryybr,34,d4g5tg5) on any page of my app it takes me to the webpage of article function of views.py
I don't know how to deal with as I a beginner in Django

____view.py___

 def article(request,slug):
    allarticles = Article.objects.all()
    context = {'allarticles':allarticles}
    return render(request,"news/Home/tnews/blog-single.html",context)

models.py

class Post(models.Model):
    no = models.CharField(max_length=1000,primary_key=True)
    title = models.CharField(max_length=100)
    content = models.TextField()
    cover = models.ImageField(upload_to='images/')
    timeStamp = models.DateTimeField(blank=True)
    slug = models.SlugField(null=False,unique=True)

    def __str__(self):
        return self.title

class Article(models.Model):
   heading = models.CharField(max_length=1000)
   player = models.CharField(max_length=100)
   article = models.TextField()
   source = models.CharField(max_length=50)
   date = models.DateTimeField(blank=True)
   image = models.ImageField(upload_to='images/')

   def __str__(self):
    return 'Article on '+ self.player

urls.py

   path("<slug:slug>",views.article,name='article')

Upvotes: 2

Views: 3946

Answers (2)

Sheraram_Prajapat
Sheraram_Prajapat

Reputation: 597

Add slug to article model

Class Article(models.Model):
    slug = models.SlugField(unique=True)
from django.shortcuts import get_object_or_404

def article_detail(request, article):
    article = get_object_or_404(Model_Name, slug=article)
    context = {'article ': article}
    return render(request,"news/Home/tnews/blog-single.html",context)

In urls.py path('<slug:article>/", views.article_detail, name='detail'),

get_object_or_404 is a function similar to get() function, it returns an object if it exists or 404 if it doesn't exist.

If you want to add link in template set href attribute of a tag href="{% url 'article' article.slug %}" Also I think you want to get details of an article so make a new function like I did article_detail and pass variables to template.

Upvotes: 2

SALAH EDDINE ELGHARBI
SALAH EDDINE ELGHARBI

Reputation: 305

from django.shortcuts import get_object_or_404

def article(request,slug):
    allarticles= get_object_or_404(Article, slug=slug)
    context = {'allarticles':allarticles}
    return render(request,"news/Home/tnews/blog-single.html",context)

and urls.py

   path("<str:slug>",views.article,name='article')

and

class Article(models.Model):
   heading = models.CharField(max_length=1000)
   player = models.CharField(max_length=100)
   article = models.TextField()
   source = models.CharField(max_length=50)
   date = models.DateTimeField(blank=True)
   image = models.ImageField(upload_to='images/')


  slug = models.SlugField(unique=True)


   def __str__(self):
    return 'Article on '+ self.player

Upvotes: 0

Related Questions