Home > OS >  Unable to access context object in HTML page rendered in Django
Unable to access context object in HTML page rendered in Django

Time:01-01

The below view function send news object to HTML page but HTML page gives no result/blank page.

def Newsdetail(request,slug):
    news = get_object_or_404(News,slug=slug)
    return render(request, 'newsfront/news_detail.html', {news:news})

below is the model

class News(models.Model):
    title=models.TextField()
    ..........

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

HTML PAGE

 <h5>{{news.title}}</h5>

CodePudding user response:

i suppose the return statement of your Newsdetail view should look like this:

def Newsdetail(request, slug):
    news = get_object_or_404(News, slug=slug)
    return render(request, 'newsfront/news_detail.html', {'news':news})
  • Related