Adding Context to Navbar HTML Django Project

Viewed 72

I am trying to add context to navbar.html but it is not rendering/ showing the required information.

Here is the models.py:

class Information(models.Model):
    github = models.URLField(blank=True, null=True)
    linkedin = models.URLField(blank=True, null=True)
    facebook = models.URLField(blank=True, null=True)
    twitter = models.URLField(blank=True, null=True)

here is the view.py:

#Trial 1
def home(request):
    template_name = 'blog/navbar.html'
    info = Information.objects.first()
    context = {
        'info': info,
    }
    return render(request, template_name, context)

#Trial 2
class Home(DetailView):
    model = Information
    template_name = 'blog/navbar.html'  # <app>/<model>_<viewtype>.html
    context_object_name = 'info'

Here is the HTML:

          <a href="" class="nav-link waves-effect">{{info.facebook}}
            <i class="fab fa-facebook-f"></i>
          </a>

My Qustion: Why is the {{info.facebook}} not printing what am I missing? How should I fix it? In the URLs I did not add anything because I am sending the context to navbar which will be appearing on every page not a specific page

1 Answers

Because no query was executed, try

info = Information.objects.all().first()
Related