Pass parameters to base.html from every single views
I have such a top nav-bar in the base.html
<div class='section-topbar'>
<div class="row">
<nav class="col-md-12">
<ul class="nav nav-tabs nav-justified">
{% for sec in sections %} {% if sec == current_section %}
<li class="active">
<a href="/article/list/{{ current_section.id }}">{{ current_section.name }}</a>
</li>
{% else %}
<li>
<a href="/article/list/{{ sec.id }}">{{ sec.name }}</a>
</li>
{% endif %} {% endfor %}
<br class="cbt">
</ul>
</nav>
</div> <!--first row-->
</div>
It is designed to present on every single page, and retrieve two contextual parameter sections and current_section from the view,
context = {"page":page,
"current_section": section,
"sections": sections,}
return render(request, "article/article_list.html", context)
So I have to pass the extra parameters to templates from every views,
Is it possible to pass them in one go and enable them globally?
django
add a comment |
I have such a top nav-bar in the base.html
<div class='section-topbar'>
<div class="row">
<nav class="col-md-12">
<ul class="nav nav-tabs nav-justified">
{% for sec in sections %} {% if sec == current_section %}
<li class="active">
<a href="/article/list/{{ current_section.id }}">{{ current_section.name }}</a>
</li>
{% else %}
<li>
<a href="/article/list/{{ sec.id }}">{{ sec.name }}</a>
</li>
{% endif %} {% endfor %}
<br class="cbt">
</ul>
</nav>
</div> <!--first row-->
</div>
It is designed to present on every single page, and retrieve two contextual parameter sections and current_section from the view,
context = {"page":page,
"current_section": section,
"sections": sections,}
return render(request, "article/article_list.html", context)
So I have to pass the extra parameters to templates from every views,
Is it possible to pass them in one go and enable them globally?
django
1
Is the section related to for example the app where the view is stored?
– Willem Van Onsem
Jul 12 '18 at 11:31
the section is a model table in database @WillemVanOnsem
– JawSaw
Jul 12 '18 at 11:35
1
yes, but how do you determine what thecurrent_sectionis. Is there any (simple) logic behind this?
– Willem Van Onsem
Jul 12 '18 at 11:36
thanks for your inspiration, I got the idea, the current_section should be passed in to each template to determine which one is active. I am going to write a general function to retrieve data and update context. @WillemVanOnsem
– JawSaw
Jul 12 '18 at 11:49
add a comment |
I have such a top nav-bar in the base.html
<div class='section-topbar'>
<div class="row">
<nav class="col-md-12">
<ul class="nav nav-tabs nav-justified">
{% for sec in sections %} {% if sec == current_section %}
<li class="active">
<a href="/article/list/{{ current_section.id }}">{{ current_section.name }}</a>
</li>
{% else %}
<li>
<a href="/article/list/{{ sec.id }}">{{ sec.name }}</a>
</li>
{% endif %} {% endfor %}
<br class="cbt">
</ul>
</nav>
</div> <!--first row-->
</div>
It is designed to present on every single page, and retrieve two contextual parameter sections and current_section from the view,
context = {"page":page,
"current_section": section,
"sections": sections,}
return render(request, "article/article_list.html", context)
So I have to pass the extra parameters to templates from every views,
Is it possible to pass them in one go and enable them globally?
django
I have such a top nav-bar in the base.html
<div class='section-topbar'>
<div class="row">
<nav class="col-md-12">
<ul class="nav nav-tabs nav-justified">
{% for sec in sections %} {% if sec == current_section %}
<li class="active">
<a href="/article/list/{{ current_section.id }}">{{ current_section.name }}</a>
</li>
{% else %}
<li>
<a href="/article/list/{{ sec.id }}">{{ sec.name }}</a>
</li>
{% endif %} {% endfor %}
<br class="cbt">
</ul>
</nav>
</div> <!--first row-->
</div>
It is designed to present on every single page, and retrieve two contextual parameter sections and current_section from the view,
context = {"page":page,
"current_section": section,
"sections": sections,}
return render(request, "article/article_list.html", context)
So I have to pass the extra parameters to templates from every views,
Is it possible to pass them in one go and enable them globally?
django
django
asked Jul 12 '18 at 11:25
JawSawJawSaw
4,59811837
4,59811837
1
Is the section related to for example the app where the view is stored?
– Willem Van Onsem
Jul 12 '18 at 11:31
the section is a model table in database @WillemVanOnsem
– JawSaw
Jul 12 '18 at 11:35
1
yes, but how do you determine what thecurrent_sectionis. Is there any (simple) logic behind this?
– Willem Van Onsem
Jul 12 '18 at 11:36
thanks for your inspiration, I got the idea, the current_section should be passed in to each template to determine which one is active. I am going to write a general function to retrieve data and update context. @WillemVanOnsem
– JawSaw
Jul 12 '18 at 11:49
add a comment |
1
Is the section related to for example the app where the view is stored?
– Willem Van Onsem
Jul 12 '18 at 11:31
the section is a model table in database @WillemVanOnsem
– JawSaw
Jul 12 '18 at 11:35
1
yes, but how do you determine what thecurrent_sectionis. Is there any (simple) logic behind this?
– Willem Van Onsem
Jul 12 '18 at 11:36
thanks for your inspiration, I got the idea, the current_section should be passed in to each template to determine which one is active. I am going to write a general function to retrieve data and update context. @WillemVanOnsem
– JawSaw
Jul 12 '18 at 11:49
1
1
Is the section related to for example the app where the view is stored?
– Willem Van Onsem
Jul 12 '18 at 11:31
Is the section related to for example the app where the view is stored?
– Willem Van Onsem
Jul 12 '18 at 11:31
the section is a model table in database @WillemVanOnsem
– JawSaw
Jul 12 '18 at 11:35
the section is a model table in database @WillemVanOnsem
– JawSaw
Jul 12 '18 at 11:35
1
1
yes, but how do you determine what the
current_section is. Is there any (simple) logic behind this?– Willem Van Onsem
Jul 12 '18 at 11:36
yes, but how do you determine what the
current_section is. Is there any (simple) logic behind this?– Willem Van Onsem
Jul 12 '18 at 11:36
thanks for your inspiration, I got the idea, the current_section should be passed in to each template to determine which one is active. I am going to write a general function to retrieve data and update context. @WillemVanOnsem
– JawSaw
Jul 12 '18 at 11:49
thanks for your inspiration, I got the idea, the current_section should be passed in to each template to determine which one is active. I am going to write a general function to retrieve data and update context. @WillemVanOnsem
– JawSaw
Jul 12 '18 at 11:49
add a comment |
2 Answers
2
active
oldest
votes
Write your own context_processor which will inject the given variables in every view's context and they will be available in every template.
# myproject/myapp/context_processors.py
def sections_processor(request):
# do something ...
# then return your variables
return {'sections': sections, 'current_section': section}
You'll need to register this context processor in your settings file so that Django will run it:
# myproject/myproject/settings.py
TEMPLATES = [{
'OPTIONS': {
'context_processors': [
...
'myappp.context_processors.sections_processor',
]
}
}]
add a comment |
Yes it is, you can use context_processors, so every template yours will have by default this variable loaded... but keep in mind all your pages must be able to run the code inside your context_processor
https://docs.djangoproject.com/pt-br/2.0/_modules/django/template/context_processors/
EDIT: Here some code so you can try this out
settings.py
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
'youapp.context_processors.yourcontextname_context_processor',
],
},
}]
context_processors.py # Create it inside your app
def yourcontextname_context_processor(request):
... # Your logic
data = {
'something': "something",
'another_thing': "another_thing",
'array_of_thing': ["thing", "thing", "thing", ],
}
return data
in your html
{{ something }}
{{ another_thing }}
{% for thing in array_of_thing %}
{{ thing }}
{% endfor %}
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%2f51304437%2fpass-parameters-to-base-html-from-every-single-views%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Write your own context_processor which will inject the given variables in every view's context and they will be available in every template.
# myproject/myapp/context_processors.py
def sections_processor(request):
# do something ...
# then return your variables
return {'sections': sections, 'current_section': section}
You'll need to register this context processor in your settings file so that Django will run it:
# myproject/myproject/settings.py
TEMPLATES = [{
'OPTIONS': {
'context_processors': [
...
'myappp.context_processors.sections_processor',
]
}
}]
add a comment |
Write your own context_processor which will inject the given variables in every view's context and they will be available in every template.
# myproject/myapp/context_processors.py
def sections_processor(request):
# do something ...
# then return your variables
return {'sections': sections, 'current_section': section}
You'll need to register this context processor in your settings file so that Django will run it:
# myproject/myproject/settings.py
TEMPLATES = [{
'OPTIONS': {
'context_processors': [
...
'myappp.context_processors.sections_processor',
]
}
}]
add a comment |
Write your own context_processor which will inject the given variables in every view's context and they will be available in every template.
# myproject/myapp/context_processors.py
def sections_processor(request):
# do something ...
# then return your variables
return {'sections': sections, 'current_section': section}
You'll need to register this context processor in your settings file so that Django will run it:
# myproject/myproject/settings.py
TEMPLATES = [{
'OPTIONS': {
'context_processors': [
...
'myappp.context_processors.sections_processor',
]
}
}]
Write your own context_processor which will inject the given variables in every view's context and they will be available in every template.
# myproject/myapp/context_processors.py
def sections_processor(request):
# do something ...
# then return your variables
return {'sections': sections, 'current_section': section}
You'll need to register this context processor in your settings file so that Django will run it:
# myproject/myproject/settings.py
TEMPLATES = [{
'OPTIONS': {
'context_processors': [
...
'myappp.context_processors.sections_processor',
]
}
}]
edited Jan 1 at 21:02
answered Jul 12 '18 at 12:06
xyresxyres
9,76732445
9,76732445
add a comment |
add a comment |
Yes it is, you can use context_processors, so every template yours will have by default this variable loaded... but keep in mind all your pages must be able to run the code inside your context_processor
https://docs.djangoproject.com/pt-br/2.0/_modules/django/template/context_processors/
EDIT: Here some code so you can try this out
settings.py
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
'youapp.context_processors.yourcontextname_context_processor',
],
},
}]
context_processors.py # Create it inside your app
def yourcontextname_context_processor(request):
... # Your logic
data = {
'something': "something",
'another_thing': "another_thing",
'array_of_thing': ["thing", "thing", "thing", ],
}
return data
in your html
{{ something }}
{{ another_thing }}
{% for thing in array_of_thing %}
{{ thing }}
{% endfor %}
add a comment |
Yes it is, you can use context_processors, so every template yours will have by default this variable loaded... but keep in mind all your pages must be able to run the code inside your context_processor
https://docs.djangoproject.com/pt-br/2.0/_modules/django/template/context_processors/
EDIT: Here some code so you can try this out
settings.py
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
'youapp.context_processors.yourcontextname_context_processor',
],
},
}]
context_processors.py # Create it inside your app
def yourcontextname_context_processor(request):
... # Your logic
data = {
'something': "something",
'another_thing': "another_thing",
'array_of_thing': ["thing", "thing", "thing", ],
}
return data
in your html
{{ something }}
{{ another_thing }}
{% for thing in array_of_thing %}
{{ thing }}
{% endfor %}
add a comment |
Yes it is, you can use context_processors, so every template yours will have by default this variable loaded... but keep in mind all your pages must be able to run the code inside your context_processor
https://docs.djangoproject.com/pt-br/2.0/_modules/django/template/context_processors/
EDIT: Here some code so you can try this out
settings.py
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
'youapp.context_processors.yourcontextname_context_processor',
],
},
}]
context_processors.py # Create it inside your app
def yourcontextname_context_processor(request):
... # Your logic
data = {
'something': "something",
'another_thing': "another_thing",
'array_of_thing': ["thing", "thing", "thing", ],
}
return data
in your html
{{ something }}
{{ another_thing }}
{% for thing in array_of_thing %}
{{ thing }}
{% endfor %}
Yes it is, you can use context_processors, so every template yours will have by default this variable loaded... but keep in mind all your pages must be able to run the code inside your context_processor
https://docs.djangoproject.com/pt-br/2.0/_modules/django/template/context_processors/
EDIT: Here some code so you can try this out
settings.py
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
'youapp.context_processors.yourcontextname_context_processor',
],
},
}]
context_processors.py # Create it inside your app
def yourcontextname_context_processor(request):
... # Your logic
data = {
'something': "something",
'another_thing': "another_thing",
'array_of_thing': ["thing", "thing", "thing", ],
}
return data
in your html
{{ something }}
{{ another_thing }}
{% for thing in array_of_thing %}
{{ thing }}
{% endfor %}
edited Jul 12 '18 at 12:18
answered Jul 12 '18 at 12:02
Diego ViníciusDiego Vinícius
965415
965415
add a comment |
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%2f51304437%2fpass-parameters-to-base-html-from-every-single-views%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
Is the section related to for example the app where the view is stored?
– Willem Van Onsem
Jul 12 '18 at 11:31
the section is a model table in database @WillemVanOnsem
– JawSaw
Jul 12 '18 at 11:35
1
yes, but how do you determine what the
current_sectionis. Is there any (simple) logic behind this?– Willem Van Onsem
Jul 12 '18 at 11:36
thanks for your inspiration, I got the idea, the current_section should be passed in to each template to determine which one is active. I am going to write a general function to retrieve data and update context. @WillemVanOnsem
– JawSaw
Jul 12 '18 at 11:49