Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to access context object in HTML page rendered in Django

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading