django navbar template - tags - django

I have created 2 navbars one for blog and one for the main app. I use include tag to introduce navbar into templates. "extends base.html" is also used however, navbar is not included in the base file and as mentioned above is introduced as include tag.
In the main app which is supposed to render same navbar(contents) for all pages of the app is not doing so, and I only receive the desired result on the homepage.
The context variables used on the navbar are from the homepage view function and when homepage page is rendered it works perfectly however when I open any internal pages of the same app the navbar does not show any of the content.
I presume navbar would try and load context variables of the view function of the active(currently opened) html page only and so homepage context variables do not work on any other page, even though navbar is a separate html file.
If this is correct I wanted to know how to get around it as all the pages on the main app should show the same navbar values.
home.html - This is how base and navbar are introduced in the template.
{% extends 'base.html' %}
{% block title %}Title{% endblock title %}
{% block navbar %}{% include 'navbar.html' %}{% endblock navbar%}
Similarly rest of the pages introduce both base and navbar.
All context variables used in navbar {% for country in countries %} for example are from view function for the home page.
When I use the same navbar for say "about us" page the navbar does not show the required information.
Does that help?

simple way you can do
in base.html
{% block content %}
{% endblock %}
in navbar2.html file
all html code
in navbar2.html file
all html code
other1.html file
{% extends 'base.html'%}
{% blck content %}
{% include 'navbar1.html' %}
All html code here
{% endblock %}
other2.html file
{% extends 'base.html'%}
{% blck content %}
{% include 'navbar1.html' %}
All html code here
{% endblock %}
if you want send the context variables to all files then create view which will render to base.html and pass that context variables to base.html. As base.html is extended in each templates, so you will get access for that context variables in all templates

Related

How load template from another App through Main App

I just started learn Django and got a problem. I have a Main App, that must include another apps. My Main app has url localhost: port/main/. I have main template, which consist header, content and footer. In header I load Menu. How load News template in content block, when url will localhost: port/main/news. I use CBV.
Your main template has surely a content block, something like
{% block content %}
{% endblock content %}
The news template should use the same block like:
{% extends 'main.html' %}
{% block content %}
here the content of your news.html template
{% endblock content %}

How to add a contact form template into another template?

I'm very new to this and I'm trying to create a simple website for a company using django 2.
I'm using this template that is a single page: https://html5up.net/astral
I want to make the contact form work but I can't manage.
I've tried putting {% block %} {% endblock %} in the HTML file but it won't render the form, {% include %} renders the html file I created but not the form. I was wondering if it is possible to make the form that is already rendered work.
Thanks!
You can use Template Inheritance to do this.
You have your "base.html" parent template which has a placeholder for the form:
<html>
...
<div ... >
{% block contact-form %}
{% endblock %}
</div>
...
</html>
And your form is in "contact.html" child template:
{% extends "base.html" %}
{% block contact-form %}
<!-- contact form content -->
{% endblock %}
Then in your url patterns direct peeps to the view that renders the child "contact.html" template.
The {% extends %} tag lets the template engine know that it must first load the parent "base.html" and then fill in the appropriate block with the child "contact.html" template's content.

Render block only in django with google app engine

I'm using google app engine and django for running a simple web-application. Everything works fine, but when I'm clicking in my navigation on a link the whole page is rendering again, including the navigation. So I tried to except the main content with a block like that:
# main.html
<html>
<title>Dummytitle</title>
<body>
...Navigation menu...
{% block maincontent %}
{% endblock %}
</body>
</html>
And here the content:
# index.html
{% extends "main.html" %}
{% block maincontent %}
<div id="main-container">
Amazing content
</div>
{% endblock %}
In the backend I'm calling the template like that:
def render_template(self, view_filename = 'index.html', params=None):
path = os.path.join(os.path.dirname(__file__), 'views', view_filename)
self.response.out.write(template.render(path, params))
But it still doesn't work. When I want to change the content from index.html to another page also the navigation itself is rendering again.
So how is it possible to change the content of the block without rendering the whole page?
The full page (index with main, or another with main) is always rendered. Except when Jinja has cached page data.
You can load a main and use ajax to load and change the page. See this question

Django-CMS show_placeholder not working as expected

I'm working on a site where the footer content is shared across all pages. What is the best way to do in Django-CMS?
I tried using show_placeholder tag, but it somehow didn't work. A little more details on what I did:
First, I have a {% placeholder footer_info %} in base.html. Then I add a page called "Home" (template homepage.html) in django admin and put some text under footer_info as a Text plugin. As the accepted answer in this question suggested (http://stackoverflow.com/questions/3616745/how-to-render-django-cms-plugin-in-every-page),
I add
{% placeholder footer_info or %}
{% show_placeholder footer_info "Home" %}
{% endplaceholder %}
In a template called services.html which I used as the template for page Services.
However, the content in home page is not showing up in services page. I also tried adding an id home_cms_page to home page in the Advanced option area, so that I can reference it in services.html like this:
{% placeholder footer_info or %}
{% show_placeholder footer_info "home_cms_page" %}
{% endplaceholder %}
But the content is still not showing up.
Could anyone tell me what I am doing wrong? And is this the best way of getting some content from a page across all other pages (and I have to add show_placeholder in every other page)?
Thank you
EDIT:
It is not a multilingual site. I commented out 'cms.middleware.multilingual.MultilingualURLMiddleware', because the only language I use on the site is English.
I have this in my base.html:
{% load cms_tags sekizai_tags %}
<!-- all the rest of the HTML markups -->
<div class="span4">
{% placeholder footer_info %}
</div>
Then I added a page in the admin called "Home" with a Text plugin and an id of "home_cms_page".
The following is in my services.html:
{% extends "base.html" %}
{% load cms_tags %}
{% block base_content %}
{% placeholder services_info %}
{% endblock base_content %}
{% block page_content %}
Home page
{% endblock page_content %}
{% placeholder "footer_info" or %}
{% show_placeholder "footer_info" "home_cms_page" %}
{% endplaceholder %}
Read the documentation:
If you know the exact page you are referring to, it is a good idea to
use a reverse_id (a string used to uniquely name a page) rather than a
hard-coded numeric ID in your template. For example, you might have a
help page that you want to link to or display parts of on all pages.
To do this, you would first open the help page in the admin interface
and enter an ID (such as help) under the ‘Advanced’ tab of the form.
Then you could use that reverse_id with the appropriate templatetags:
{% show_placeholder "right-column" "help" %}
I added "index" in the advanced options of the index page, and added {% show_placeholder "banner" "index" %} in the base template. It all works.

Django: How to include a block site wide?

Apologies, this is pretty basic. I have abstracted my static html navigation bar to a block so that it can be dynamically rendered from a model. I have created a new block marker in base.html with the following syntax
{% block navigation %}{% endblock %}
How can I ensure this is rendered on every page? Do I need to create some sort of middle layer for this? Everything I have done so far has simply used the primary block.
EDIT Sunday, 14 August 2011 11:25 AM
I didn't explain this very well. The content of navigation block is
{% extends 'base.html' %}
{% block navigation %}
<nav>
<ul>
{% for item in items %}
<li>{{ item.name }}</li>
{% endfor %}
</ul>
</nav>
{% endblock %}
I want to render this on every page without having to go through child templates of base.html individually and add it to them, if that is possible.
You have got static navigation bar, so you can just write code in your base.html page and then use it with {% extends 'base.html' %} tag on every new page. Your markup will be in every page. Also, if you use only extends tag in your child page and then render it - you will see base.html without any edition.
If you have got code, which need to appear on few pages, but not on everyone - create 'includes' directory, save code there and extend your base template with {% include %} tag. It won't avoid repating, but make your code shorter.
And last thing you'll need in future, maybe with dynamic code - caching. With {% cache %} tag you can cache block for some time.
Your block is not doing anything here, but if you want something to appear in all the templates, you have to define it in a base template (base.html) and make the others extend from it:
base.html
Yor menu and the stuff you want to appear everywhere
{% block content %}{% endblock %}
Another template
{% extends "base.html" %}
{% block content %}
The actual content of the page
{% endblock %}
{% block FOO %}{% endblock %} reserves a space to be overwritten in sub templates.
{% include "foo.html" %} pulls content from another file into the current file.
To get {% block navigation %}{% endblock %} to display on every page, you need to add content
{% block navigation %}SHOW ME{% endblock %}