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
Related
I'm trying to run project using Django-cms 3.6.0. It was created using Django-cms 2.3.8, I'm trying to use the old code. In this new version the menu with subpages links doesn't appear. children variable in template doesn't seem to contain anything.
I expect it to show links to 4 subpages of a page. I've added pages manually in admin UI in new version of django-cms.
subbase.html:
{% extends "base.html" %}
{% load i18n %}
{% load menu_tags cms_tags %}
...
{% block left_menu %}
<nav id="lMenu">
{% show_menu 1 1 0 1 "menu/sub_menu.html" %}
{% block left_content %}{% endblock left_content %}
</nav>
{% endblock left_menu %}
sub_menu.html:
{% load menu_tags %}
<ul class="subMenu">
{% for child in children %}
<li>{{ child.get_menu_title }}</li>
{% endfor %}
</ul>
I've checked in database, using manage.py shell, that those pages has child pages:
from cms.models.pagemodel import Page
pages = Page.object.all()
children = pages[2].get_descendant_pages()
And now, e.g., calling pages[2].get_menu_title(), children[0].get_menu_title() returns expected proper names of the pages, as added through UI.
I haven't found much about this children variable in docs. Should this still work this way in 3.6? What is the proper way to refer to child pages in template?
I've found, that I had something wrong with language settings. Menu with subpages links started to appear after I:
used i18n_patterns in main project urls.py file
added CMS_LANGUAGES in settings.py according to docs http://docs.django-cms.org/en/latest/reference/configuration.html#cms-languages
added pages in both languages through cms admin page. Before I had page in only one language.
Can't remember exactly which one fixed issue with submenu.
Also maybe something were wrong with urls handling, I used django apps urls from urls.py instead of cms.urls.
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 %}
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
I'm building a website using django with a header on top of every page, which basically is a menu with a few links, constant throughout the pages.
However, depending on the page you're on I'd like to highlight the corresponding link on the menu by adding the class "active". To do so, I am currently doing as follow: each page has a full menu block that integrates within a general layout, which does NOT contain the menu. For exemple, page2 would look like this:
{% extends "layout.html" %}
{% block menu %}
<li>Home</li>
<li>page1</li>
<li class="active">page2</li>
<li>page3</li>
{% endblock %}
The problem is that, beside from that solution being not so pretty, every time I want to add a link to the header menu I have to modify each and every page I have. Since this is far from optimal, I was wondering if any of you would know about a better way of doing so.
Thanks in advance!
You can create a custom templatetag:
from django import template
from django.core.urlresolvers import reverse, NoReverseMatch, resolve
register = template.Library()
#register.simple_tag
def active(request, view_name):
url = resolve(request.path)
if url.view_name == view_name:
return 'active'
try:
uri = reverse(view_name)
except NoReverseMatch:
uri = view_name
if request.path.startswith(uri):
return 'active'
return ''
And use it in the template to recognize which page is loaded by URL
<li class="{% active request 'car_edit' %}">Edit</li>
If you have a "page" object at every view, you could compare a navigation item's slug to the object's slug
navigation.html
<ul>
{% for page in navigation %}
<li{% ifequal object.slug page.slug %} class="active"{% endifequal %}>
{{ page.title }}
</li>
{% endfor %}
</ul>
base.html
<html>
<head />
<body>
{% include "navigation.html" %}
{% block content %}
Welcome Earthling.
{% endblock %}
</body>
</html>
page.html
{% extends "base.html" %}
{% block content %}
{{ object }}
{% endblock %}
Where navigation is perhaps a context_processor variable holding all the pages, and object is the current PageDetailView object variable
Disclaimer
There are many solutions for your problem as noted by Paulo. Of course this solution assumes that every view holds a page object, a concept usually implemented by a CMS. If you have views that do not derive from the Page app you would have to inject page pretenders within the navigation (atleast holding a get_absolute_url and title attribute).
This might be a very nice learning experience, but you'll probably save loads time installing feinCMS or django-cms which both define an ApplicationContent principle also.
You may use the include tag and pass it a value which is the current page.
For example, this may be a separate file for declaring the menu template only:
menu.html
{% if active = "a" %}
<li>Home</li>
{% if active = "b" %}
<li>page1</li>
{% if active = "c" %}
<li class="active">page2</li>
{% if active = "d" %}
<li>page3</li>
And call this from within your template like this:
{% include 'path/to/menu.html' with active="b"%} # or a or c or d.
Hope it helps!
I'm in the process of completing the official Django tutorial and I'm stuck on part 3. Since templates are also used in the last part of part2, I will describe what I did:
Part 2 told me to "copy the template admin/base_site.html from within the default Django admin template directory in the source code of Django itself (django/contrib/admin/templates) into an admin subdirectory of whichever directory you're using in TEMPLATE_DIRS."
So I created a new directory "admin" that has the following relative path (note that where Django uses the directory name 'mysite', I use 'django_test' : /django_test/polls/templates/admin. I copied the base_site.html file into this directory.
When I render the file in my local browser, it says: {% extends "admin/base.html" %} {% load i18n %} {% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %} {% block branding %}{% trans 'Django administration' %}{% endblock %} {% block nav-global %}{% endblock %}
Part 3 has me create an index.html file in a new subdirectory polls/index.html. But when I load this file in my web browser (using localhost server), I simply see the html code instead of a bulleted list (see below).
Note that I also edited TEMPLATE_DIRS in my settings.py file to tell Django that it can find index.html under /Users/myname/Sites/django_test/django_test/templates
Below I will paste the code that my local server renders (instead of the bulleted list, which is what I want). Do you know why this code is being rendered, instead of the bulleted list?
<html>
<head><title>Test</title></head>
<body>
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li>{{ poll.question }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
</body>
</html>
I don't know whether I'm making a mistake in how I'm organizing the files. Might someone have an idea about what I'm doing wrong?
As you say in your comment, you're putting the file path into your browser. Naturally, then, you're going to see the text of the template, because you are bypassing Django completely and getting the browser to load the unrendered template from disk.
As the tutorial describes, you need to ask Django to serve the template and render it, via its normal URL mechanism. In the earlier part of that section, you went to localhost:8000/admin/ to see the admin site - this hasn't changed just because you've replaced a template. Go back to that address and you'll see your updated - and rendered - template.
The django admin site is easy once you get the hang of it.
The steps to take are:
-Uncomment the django admin site in your urls.py
-Make the css available to the admin site by either copying the admin folder (inside django package) into the folder specified in STATIC_ROOT in your settings.py or making the diectory available on your PYTHONPATH
In other words, you dont need to create a template for the admin site. You will, however, need to create templates to access the views that you create in your project