I'm trying to create a blogging website in Django. I'm pretty much a beginner so this question is probably silly but I don't know how to fix this.
I have a main navigation in my header. In the base.html template I created links in the menu for a couple of pages. In the same template I have a list of categories in the sidebar which are links to listing all posts within that category.
When I go to any other template (when I click any of those links), even though I extend the base.html template, the links disappear. How can I keep them in place regardless of the template? What's the common practice for things like this?
Thanks for reading! Cheers!
EDIT: The code:
<nav>
<ul>
<li><a href="{% url 'index' %}">HOME</a></li>
{% for link in links %}
<li><a href="{% url 'page' link.slug %}">{{ link.title }} </a></li>
{% endfor %}
</ul>
</nav>
This is in the base.html, but those links (except for the hardcoded Home link) disappear in other templates which just change the content block.
EDIT 2: Here's the view for the base:
def index(request):
posts = Post.objects.all()
links = Page.objects.filter(menu='Y')
categories = Category.objects.all()
return render(request, 'main/base.html', {'posts': posts,
'links': links,
'categories': categories,
})
SOLUTION: Thanks everyone for your help. Here's the solution I used:
from django.template import RequestContext
def base_links(request):
header_links = Page.objects.filter(menu='Y')
sidebar_categories = Category.objects.all()
return {
'links': header_links,
'categories': sidebar_categories,
}
And then I've added:
context_instance = RequestContext(request, processors=[base_links])
to all my views.
{% extends 'myapp/base.html' %}in the other templates? - Wtowerlinksbeing passed to the context? - rneviusRequestContext) - just add your custom context processor to yoursettings.CONTEXT_PROCESSORS. - bruno desthuilliers