Django navbar content links are disappear
Im stuck my django blog project.
I create a standart blog but i have a problem now. When i add in navbar some categorys in all post i see this post index page. But when i go another page (like contact form) navbar post links are disappear.
let me explain;
i create three article and i add this articles in "Music" category
after then i use this code in my navbar;
{% for category in category %}
{% if category.name == 'Music' %}
{% for article in category.get_article %}
<li class="nav-item" > <a class="nav-link" title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a></li>
{% endfor %}
{% endif %}
{% endfor %}
So I want to do here is to bring the titles of the articles in the music category to the menu. I'm not sure if this is the right method but it works on the index page.
After this if i go contact page this links are disappear. (Also Contact form is a separate app https://github.com/maru/django-contact-form-recaptcha.)
same time, I use another method but these links are also disappear;
{% for article in articles %}
{% if article.slug == 'about_us' %}
<a href="{% url 'article:detail' slug=article.slug %}">
{{ article.title }}</a>
{% endif %}
{% endfor %}
Why disappear this links? How can i solve this? Can i ask help ?
my navbar.html
{% load i18n %}
<!-- Menu -->
<div class="menu-wrapper center-relative">
<nav id="header-main-menu">
<div class="mob-menu">Menu</div>
<ul class="main-menu sm sm-clean">
<li><a href="{% url "index" %}">{% trans "HomePage" %}</a></li>
<li><a href="#services">{% trans "Services" %}</a></li>
<li>
{% for article in articles %}
{% if article.slug == 'about_us' %}
<a href="{% url 'article:detail' slug=article.slug %}">
{{ article.title }}</a>
{% endif %}
{% endfor %}
</li>
<li><a href="#video">{% trans "HELLO WORLD" %}</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-1643 dropdown">
<a title="" href="">{% trans "Producs" %}</a>
<ul role="menu" class=" dropdown-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-1644 dropdown">
<a title="Level 2" href="">{% trans "Consult" %}</a>
<ul role="menu" class=" dropdown-menu">
<li >
{% for category in category %}
{% if category.name == 'music' %}
{% for article in category.get_article %}
<a title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a>
{% endfor %}
{% endif %}
{% endfor %}</li>
</ul>
</li>
<li>
{% for category in category %}
{% if category.name == 'header' %}
{% for article in category.get_article %}
<a title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a>
{% endfor %}
{% endif %}
{% endfor %}
</li>
</ul>
</li>
<li><a href="{% url 'contact_form' %}">{% trans "İletişim" %}</a></li>
</ul>
</nav>
</div>
article/views.py
from django.shortcuts import render, get_object_or_404
from .models import Article, Category
from django.core.paginator import Paginator
from django.utils.translation import gettext as _
# Create your views here.
def index(request):
articles = Article.objects.all()
category = Category.objects.all()
context = {
"articles": articles,
"category": category,
}
return render(request, 'index.html', context)
def detail(request,slug):
# article = Article.objects.filter (id = id).first()
article = get_object_or_404(Article, slug = slug)
category = Category.objects.all()
return render(request, "detail.html", {"article":article, "category":category,})
def category_detail(request,slug):
template = "category_detail.html"
category=get_object_or_404(Category,slug=slug)
article=Article.objects.filter(category=category)
context = {
'category' : category,
'article' : article,
}
return render(request,template,context)
def category_page(request):
object_list = Category.objects.all()
context = {'object_list': object_list,}
return render(request, 'detail.html', context)
contact_form/views.py
from django.views.generic.edit import FormView
from .forms import ContactForm
try:
from django.urls import reverse
except ImportError: # pragma: no cover
from django.core.urlresolvers import reverse # pragma: no cover
class ContactFormView(FormView):
form_class = ContactForm
recipient_list = None
template_name = 'contact_form/contact_form.html'
def form_valid(self, form):
form.save()
return super(ContactFormView, self).form_valid(form)
def get_form_kwargs(self):
if self.recipient_list is not None:
kwargs.update({'recipient_list': self.recipient_list})
return kwargs
def get_success_url(self):
return reverse('contact_form_sent')
Thanks for help...
django python-3.x
add a comment |
Im stuck my django blog project.
I create a standart blog but i have a problem now. When i add in navbar some categorys in all post i see this post index page. But when i go another page (like contact form) navbar post links are disappear.
let me explain;
i create three article and i add this articles in "Music" category
after then i use this code in my navbar;
{% for category in category %}
{% if category.name == 'Music' %}
{% for article in category.get_article %}
<li class="nav-item" > <a class="nav-link" title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a></li>
{% endfor %}
{% endif %}
{% endfor %}
So I want to do here is to bring the titles of the articles in the music category to the menu. I'm not sure if this is the right method but it works on the index page.
After this if i go contact page this links are disappear. (Also Contact form is a separate app https://github.com/maru/django-contact-form-recaptcha.)
same time, I use another method but these links are also disappear;
{% for article in articles %}
{% if article.slug == 'about_us' %}
<a href="{% url 'article:detail' slug=article.slug %}">
{{ article.title }}</a>
{% endif %}
{% endfor %}
Why disappear this links? How can i solve this? Can i ask help ?
my navbar.html
{% load i18n %}
<!-- Menu -->
<div class="menu-wrapper center-relative">
<nav id="header-main-menu">
<div class="mob-menu">Menu</div>
<ul class="main-menu sm sm-clean">
<li><a href="{% url "index" %}">{% trans "HomePage" %}</a></li>
<li><a href="#services">{% trans "Services" %}</a></li>
<li>
{% for article in articles %}
{% if article.slug == 'about_us' %}
<a href="{% url 'article:detail' slug=article.slug %}">
{{ article.title }}</a>
{% endif %}
{% endfor %}
</li>
<li><a href="#video">{% trans "HELLO WORLD" %}</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-1643 dropdown">
<a title="" href="">{% trans "Producs" %}</a>
<ul role="menu" class=" dropdown-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-1644 dropdown">
<a title="Level 2" href="">{% trans "Consult" %}</a>
<ul role="menu" class=" dropdown-menu">
<li >
{% for category in category %}
{% if category.name == 'music' %}
{% for article in category.get_article %}
<a title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a>
{% endfor %}
{% endif %}
{% endfor %}</li>
</ul>
</li>
<li>
{% for category in category %}
{% if category.name == 'header' %}
{% for article in category.get_article %}
<a title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a>
{% endfor %}
{% endif %}
{% endfor %}
</li>
</ul>
</li>
<li><a href="{% url 'contact_form' %}">{% trans "İletişim" %}</a></li>
</ul>
</nav>
</div>
article/views.py
from django.shortcuts import render, get_object_or_404
from .models import Article, Category
from django.core.paginator import Paginator
from django.utils.translation import gettext as _
# Create your views here.
def index(request):
articles = Article.objects.all()
category = Category.objects.all()
context = {
"articles": articles,
"category": category,
}
return render(request, 'index.html', context)
def detail(request,slug):
# article = Article.objects.filter (id = id).first()
article = get_object_or_404(Article, slug = slug)
category = Category.objects.all()
return render(request, "detail.html", {"article":article, "category":category,})
def category_detail(request,slug):
template = "category_detail.html"
category=get_object_or_404(Category,slug=slug)
article=Article.objects.filter(category=category)
context = {
'category' : category,
'article' : article,
}
return render(request,template,context)
def category_page(request):
object_list = Category.objects.all()
context = {'object_list': object_list,}
return render(request, 'detail.html', context)
contact_form/views.py
from django.views.generic.edit import FormView
from .forms import ContactForm
try:
from django.urls import reverse
except ImportError: # pragma: no cover
from django.core.urlresolvers import reverse # pragma: no cover
class ContactFormView(FormView):
form_class = ContactForm
recipient_list = None
template_name = 'contact_form/contact_form.html'
def form_valid(self, form):
form.save()
return super(ContactFormView, self).form_valid(form)
def get_form_kwargs(self):
if self.recipient_list is not None:
kwargs.update({'recipient_list': self.recipient_list})
return kwargs
def get_success_url(self):
return reverse('contact_form_sent')
Thanks for help...
django python-3.x
1
You've only addedarticles
andcategory
(btw I would rename that tocategories
) to the context used to render theindex
view, not the context of the other views. So for the other views, such as theContactFormView
,articles
andcategory
are undefined and won't render in your template.
– dirkgroten
Nov 14 '18 at 10:48
@dirkgroten thanks for answer? Is there an easy way to do this? Can I use it all in one place?
– Aytek
Nov 14 '18 at 11:47
add a comment |
Im stuck my django blog project.
I create a standart blog but i have a problem now. When i add in navbar some categorys in all post i see this post index page. But when i go another page (like contact form) navbar post links are disappear.
let me explain;
i create three article and i add this articles in "Music" category
after then i use this code in my navbar;
{% for category in category %}
{% if category.name == 'Music' %}
{% for article in category.get_article %}
<li class="nav-item" > <a class="nav-link" title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a></li>
{% endfor %}
{% endif %}
{% endfor %}
So I want to do here is to bring the titles of the articles in the music category to the menu. I'm not sure if this is the right method but it works on the index page.
After this if i go contact page this links are disappear. (Also Contact form is a separate app https://github.com/maru/django-contact-form-recaptcha.)
same time, I use another method but these links are also disappear;
{% for article in articles %}
{% if article.slug == 'about_us' %}
<a href="{% url 'article:detail' slug=article.slug %}">
{{ article.title }}</a>
{% endif %}
{% endfor %}
Why disappear this links? How can i solve this? Can i ask help ?
my navbar.html
{% load i18n %}
<!-- Menu -->
<div class="menu-wrapper center-relative">
<nav id="header-main-menu">
<div class="mob-menu">Menu</div>
<ul class="main-menu sm sm-clean">
<li><a href="{% url "index" %}">{% trans "HomePage" %}</a></li>
<li><a href="#services">{% trans "Services" %}</a></li>
<li>
{% for article in articles %}
{% if article.slug == 'about_us' %}
<a href="{% url 'article:detail' slug=article.slug %}">
{{ article.title }}</a>
{% endif %}
{% endfor %}
</li>
<li><a href="#video">{% trans "HELLO WORLD" %}</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-1643 dropdown">
<a title="" href="">{% trans "Producs" %}</a>
<ul role="menu" class=" dropdown-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-1644 dropdown">
<a title="Level 2" href="">{% trans "Consult" %}</a>
<ul role="menu" class=" dropdown-menu">
<li >
{% for category in category %}
{% if category.name == 'music' %}
{% for article in category.get_article %}
<a title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a>
{% endfor %}
{% endif %}
{% endfor %}</li>
</ul>
</li>
<li>
{% for category in category %}
{% if category.name == 'header' %}
{% for article in category.get_article %}
<a title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a>
{% endfor %}
{% endif %}
{% endfor %}
</li>
</ul>
</li>
<li><a href="{% url 'contact_form' %}">{% trans "İletişim" %}</a></li>
</ul>
</nav>
</div>
article/views.py
from django.shortcuts import render, get_object_or_404
from .models import Article, Category
from django.core.paginator import Paginator
from django.utils.translation import gettext as _
# Create your views here.
def index(request):
articles = Article.objects.all()
category = Category.objects.all()
context = {
"articles": articles,
"category": category,
}
return render(request, 'index.html', context)
def detail(request,slug):
# article = Article.objects.filter (id = id).first()
article = get_object_or_404(Article, slug = slug)
category = Category.objects.all()
return render(request, "detail.html", {"article":article, "category":category,})
def category_detail(request,slug):
template = "category_detail.html"
category=get_object_or_404(Category,slug=slug)
article=Article.objects.filter(category=category)
context = {
'category' : category,
'article' : article,
}
return render(request,template,context)
def category_page(request):
object_list = Category.objects.all()
context = {'object_list': object_list,}
return render(request, 'detail.html', context)
contact_form/views.py
from django.views.generic.edit import FormView
from .forms import ContactForm
try:
from django.urls import reverse
except ImportError: # pragma: no cover
from django.core.urlresolvers import reverse # pragma: no cover
class ContactFormView(FormView):
form_class = ContactForm
recipient_list = None
template_name = 'contact_form/contact_form.html'
def form_valid(self, form):
form.save()
return super(ContactFormView, self).form_valid(form)
def get_form_kwargs(self):
if self.recipient_list is not None:
kwargs.update({'recipient_list': self.recipient_list})
return kwargs
def get_success_url(self):
return reverse('contact_form_sent')
Thanks for help...
django python-3.x
Im stuck my django blog project.
I create a standart blog but i have a problem now. When i add in navbar some categorys in all post i see this post index page. But when i go another page (like contact form) navbar post links are disappear.
let me explain;
i create three article and i add this articles in "Music" category
after then i use this code in my navbar;
{% for category in category %}
{% if category.name == 'Music' %}
{% for article in category.get_article %}
<li class="nav-item" > <a class="nav-link" title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a></li>
{% endfor %}
{% endif %}
{% endfor %}
So I want to do here is to bring the titles of the articles in the music category to the menu. I'm not sure if this is the right method but it works on the index page.
After this if i go contact page this links are disappear. (Also Contact form is a separate app https://github.com/maru/django-contact-form-recaptcha.)
same time, I use another method but these links are also disappear;
{% for article in articles %}
{% if article.slug == 'about_us' %}
<a href="{% url 'article:detail' slug=article.slug %}">
{{ article.title }}</a>
{% endif %}
{% endfor %}
Why disappear this links? How can i solve this? Can i ask help ?
my navbar.html
{% load i18n %}
<!-- Menu -->
<div class="menu-wrapper center-relative">
<nav id="header-main-menu">
<div class="mob-menu">Menu</div>
<ul class="main-menu sm sm-clean">
<li><a href="{% url "index" %}">{% trans "HomePage" %}</a></li>
<li><a href="#services">{% trans "Services" %}</a></li>
<li>
{% for article in articles %}
{% if article.slug == 'about_us' %}
<a href="{% url 'article:detail' slug=article.slug %}">
{{ article.title }}</a>
{% endif %}
{% endfor %}
</li>
<li><a href="#video">{% trans "HELLO WORLD" %}</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-1643 dropdown">
<a title="" href="">{% trans "Producs" %}</a>
<ul role="menu" class=" dropdown-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-1644 dropdown">
<a title="Level 2" href="">{% trans "Consult" %}</a>
<ul role="menu" class=" dropdown-menu">
<li >
{% for category in category %}
{% if category.name == 'music' %}
{% for article in category.get_article %}
<a title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a>
{% endfor %}
{% endif %}
{% endfor %}</li>
</ul>
</li>
<li>
{% for category in category %}
{% if category.name == 'header' %}
{% for article in category.get_article %}
<a title="{{ article.title }}" href="{% url 'article:detail' slug=article.slug %}"> <p> {{ article.title }}</p></a>
{% endfor %}
{% endif %}
{% endfor %}
</li>
</ul>
</li>
<li><a href="{% url 'contact_form' %}">{% trans "İletişim" %}</a></li>
</ul>
</nav>
</div>
article/views.py
from django.shortcuts import render, get_object_or_404
from .models import Article, Category
from django.core.paginator import Paginator
from django.utils.translation import gettext as _
# Create your views here.
def index(request):
articles = Article.objects.all()
category = Category.objects.all()
context = {
"articles": articles,
"category": category,
}
return render(request, 'index.html', context)
def detail(request,slug):
# article = Article.objects.filter (id = id).first()
article = get_object_or_404(Article, slug = slug)
category = Category.objects.all()
return render(request, "detail.html", {"article":article, "category":category,})
def category_detail(request,slug):
template = "category_detail.html"
category=get_object_or_404(Category,slug=slug)
article=Article.objects.filter(category=category)
context = {
'category' : category,
'article' : article,
}
return render(request,template,context)
def category_page(request):
object_list = Category.objects.all()
context = {'object_list': object_list,}
return render(request, 'detail.html', context)
contact_form/views.py
from django.views.generic.edit import FormView
from .forms import ContactForm
try:
from django.urls import reverse
except ImportError: # pragma: no cover
from django.core.urlresolvers import reverse # pragma: no cover
class ContactFormView(FormView):
form_class = ContactForm
recipient_list = None
template_name = 'contact_form/contact_form.html'
def form_valid(self, form):
form.save()
return super(ContactFormView, self).form_valid(form)
def get_form_kwargs(self):
if self.recipient_list is not None:
kwargs.update({'recipient_list': self.recipient_list})
return kwargs
def get_success_url(self):
return reverse('contact_form_sent')
Thanks for help...
django python-3.x
django python-3.x
edited Nov 14 '18 at 9:41
Aytek
asked Nov 14 '18 at 9:23
AytekAytek
246
246
1
You've only addedarticles
andcategory
(btw I would rename that tocategories
) to the context used to render theindex
view, not the context of the other views. So for the other views, such as theContactFormView
,articles
andcategory
are undefined and won't render in your template.
– dirkgroten
Nov 14 '18 at 10:48
@dirkgroten thanks for answer? Is there an easy way to do this? Can I use it all in one place?
– Aytek
Nov 14 '18 at 11:47
add a comment |
1
You've only addedarticles
andcategory
(btw I would rename that tocategories
) to the context used to render theindex
view, not the context of the other views. So for the other views, such as theContactFormView
,articles
andcategory
are undefined and won't render in your template.
– dirkgroten
Nov 14 '18 at 10:48
@dirkgroten thanks for answer? Is there an easy way to do this? Can I use it all in one place?
– Aytek
Nov 14 '18 at 11:47
1
1
You've only added
articles
and category
(btw I would rename that to categories
) to the context used to render the index
view, not the context of the other views. So for the other views, such as the ContactFormView
, articles
and category
are undefined and won't render in your template.– dirkgroten
Nov 14 '18 at 10:48
You've only added
articles
and category
(btw I would rename that to categories
) to the context used to render the index
view, not the context of the other views. So for the other views, such as the ContactFormView
, articles
and category
are undefined and won't render in your template.– dirkgroten
Nov 14 '18 at 10:48
@dirkgroten thanks for answer? Is there an easy way to do this? Can I use it all in one place?
– Aytek
Nov 14 '18 at 11:47
@dirkgroten thanks for answer? Is there an easy way to do this? Can I use it all in one place?
– Aytek
Nov 14 '18 at 11:47
add a comment |
1 Answer
1
active
oldest
votes
As @dirkgroten has mentioned, views manage context only on pages served by that view.
To have the same items everywhere across pages served by different views, you could use a context_processor.
You can basically take the code you have in index
, make it return context
instead of render and put its path to context_processors
of TEMPLATES
in settings.py
.
Context processors are loaded with every request and the items from their returned dictionaries are automatically included in the context.
As a best practice, it's better to keep your context processors in a separate file.
Example context_processors.py
which can be placed in the same directory as your views.py
and models.py
:
from .models import Article, Category
def navigation(request):
articles = Article.objects.all()
category = Category.objects.all()
return {
"articles": articles,
"category": category,
}
In settings.py
find TEMPLATES
option, which probably looks something like
```
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Add 'APP_NAME.context_processors.navigation'
, after 'django.contrib.messages.context_processors.messages',
Replace APP_NAME
with the name of the directory where your files are located.
thank you so much for answer. Can you explain to me by writing the code? where do I need to add code? I'm so new django, so sorry for this.
– Aytek
Nov 14 '18 at 13:54
1
I've added an example
– 4140tm
Nov 14 '18 at 14:52
1
you are really best. Thanks man. I am thankful to you.
– Aytek
Nov 14 '18 at 14:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296759%2fdjango-navbar-content-links-are-disappear%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As @dirkgroten has mentioned, views manage context only on pages served by that view.
To have the same items everywhere across pages served by different views, you could use a context_processor.
You can basically take the code you have in index
, make it return context
instead of render and put its path to context_processors
of TEMPLATES
in settings.py
.
Context processors are loaded with every request and the items from their returned dictionaries are automatically included in the context.
As a best practice, it's better to keep your context processors in a separate file.
Example context_processors.py
which can be placed in the same directory as your views.py
and models.py
:
from .models import Article, Category
def navigation(request):
articles = Article.objects.all()
category = Category.objects.all()
return {
"articles": articles,
"category": category,
}
In settings.py
find TEMPLATES
option, which probably looks something like
```
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Add 'APP_NAME.context_processors.navigation'
, after 'django.contrib.messages.context_processors.messages',
Replace APP_NAME
with the name of the directory where your files are located.
thank you so much for answer. Can you explain to me by writing the code? where do I need to add code? I'm so new django, so sorry for this.
– Aytek
Nov 14 '18 at 13:54
1
I've added an example
– 4140tm
Nov 14 '18 at 14:52
1
you are really best. Thanks man. I am thankful to you.
– Aytek
Nov 14 '18 at 14:58
add a comment |
As @dirkgroten has mentioned, views manage context only on pages served by that view.
To have the same items everywhere across pages served by different views, you could use a context_processor.
You can basically take the code you have in index
, make it return context
instead of render and put its path to context_processors
of TEMPLATES
in settings.py
.
Context processors are loaded with every request and the items from their returned dictionaries are automatically included in the context.
As a best practice, it's better to keep your context processors in a separate file.
Example context_processors.py
which can be placed in the same directory as your views.py
and models.py
:
from .models import Article, Category
def navigation(request):
articles = Article.objects.all()
category = Category.objects.all()
return {
"articles": articles,
"category": category,
}
In settings.py
find TEMPLATES
option, which probably looks something like
```
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Add 'APP_NAME.context_processors.navigation'
, after 'django.contrib.messages.context_processors.messages',
Replace APP_NAME
with the name of the directory where your files are located.
thank you so much for answer. Can you explain to me by writing the code? where do I need to add code? I'm so new django, so sorry for this.
– Aytek
Nov 14 '18 at 13:54
1
I've added an example
– 4140tm
Nov 14 '18 at 14:52
1
you are really best. Thanks man. I am thankful to you.
– Aytek
Nov 14 '18 at 14:58
add a comment |
As @dirkgroten has mentioned, views manage context only on pages served by that view.
To have the same items everywhere across pages served by different views, you could use a context_processor.
You can basically take the code you have in index
, make it return context
instead of render and put its path to context_processors
of TEMPLATES
in settings.py
.
Context processors are loaded with every request and the items from their returned dictionaries are automatically included in the context.
As a best practice, it's better to keep your context processors in a separate file.
Example context_processors.py
which can be placed in the same directory as your views.py
and models.py
:
from .models import Article, Category
def navigation(request):
articles = Article.objects.all()
category = Category.objects.all()
return {
"articles": articles,
"category": category,
}
In settings.py
find TEMPLATES
option, which probably looks something like
```
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Add 'APP_NAME.context_processors.navigation'
, after 'django.contrib.messages.context_processors.messages',
Replace APP_NAME
with the name of the directory where your files are located.
As @dirkgroten has mentioned, views manage context only on pages served by that view.
To have the same items everywhere across pages served by different views, you could use a context_processor.
You can basically take the code you have in index
, make it return context
instead of render and put its path to context_processors
of TEMPLATES
in settings.py
.
Context processors are loaded with every request and the items from their returned dictionaries are automatically included in the context.
As a best practice, it's better to keep your context processors in a separate file.
Example context_processors.py
which can be placed in the same directory as your views.py
and models.py
:
from .models import Article, Category
def navigation(request):
articles = Article.objects.all()
category = Category.objects.all()
return {
"articles": articles,
"category": category,
}
In settings.py
find TEMPLATES
option, which probably looks something like
```
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Add 'APP_NAME.context_processors.navigation'
, after 'django.contrib.messages.context_processors.messages',
Replace APP_NAME
with the name of the directory where your files are located.
edited Nov 14 '18 at 14:50
answered Nov 14 '18 at 13:20
4140tm4140tm
1,136714
1,136714
thank you so much for answer. Can you explain to me by writing the code? where do I need to add code? I'm so new django, so sorry for this.
– Aytek
Nov 14 '18 at 13:54
1
I've added an example
– 4140tm
Nov 14 '18 at 14:52
1
you are really best. Thanks man. I am thankful to you.
– Aytek
Nov 14 '18 at 14:58
add a comment |
thank you so much for answer. Can you explain to me by writing the code? where do I need to add code? I'm so new django, so sorry for this.
– Aytek
Nov 14 '18 at 13:54
1
I've added an example
– 4140tm
Nov 14 '18 at 14:52
1
you are really best. Thanks man. I am thankful to you.
– Aytek
Nov 14 '18 at 14:58
thank you so much for answer. Can you explain to me by writing the code? where do I need to add code? I'm so new django, so sorry for this.
– Aytek
Nov 14 '18 at 13:54
thank you so much for answer. Can you explain to me by writing the code? where do I need to add code? I'm so new django, so sorry for this.
– Aytek
Nov 14 '18 at 13:54
1
1
I've added an example
– 4140tm
Nov 14 '18 at 14:52
I've added an example
– 4140tm
Nov 14 '18 at 14:52
1
1
you are really best. Thanks man. I am thankful to you.
– Aytek
Nov 14 '18 at 14:58
you are really best. Thanks man. I am thankful to you.
– Aytek
Nov 14 '18 at 14:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296759%2fdjango-navbar-content-links-are-disappear%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You've only added
articles
andcategory
(btw I would rename that tocategories
) to the context used to render theindex
view, not the context of the other views. So for the other views, such as theContactFormView
,articles
andcategory
are undefined and won't render in your template.– dirkgroten
Nov 14 '18 at 10:48
@dirkgroten thanks for answer? Is there an easy way to do this? Can I use it all in one place?
– Aytek
Nov 14 '18 at 11:47