i'm developing a small app with Python and Google app engine. I'm using boilerplate (https://github.com/coto/gae-boilerplate) as front-end which follows gae direction and python templates, so nothing diffrent than plain stuff.
Now, what i would like to have is this.
When a user logged in, if the field of name and last name are not filled in i would like to have, in the home page, the profile editing.
The page for editing the profile is a template (which extend the base.html), called edit_profile.html which works well.
The homepage is a template as well (extend the base.html) called home.html.
Now, can i include the edit_profile.html in home.html? how can i do it?
this is what i've, i don't know what to put instead of ???? i tried with
{% block edit_profile.html %} {% endblock %}
but does not work
{% if user_info.name and user_info.last_name %}
..
{% else %}
????
{% endif %}
thanks.
So you want to include only some block of given template. There are two solutions:
1) Create template just for profile editing form and include it into edit_profile.html. Then include it also into home.html to if condition branch:
profile_form.html:
<form action="{% url some-action %}">
{{ form }}
<input type="submit" value="save"/>
</form
profile_edit.html
{% extends "base.html" %}
{% block main %}
{% include "profile_form.html" %}
{% endblock %}
home.html
{% if user_info.name and user_info.last_name %}
{% include "profile_form.html" %}
{% endif %}
2) use variable for extended template:
profile_form.html
{% extend BASE_TEMPLATE %}
and set it into context w/ different value as needed:
in home.html (let's say included_form.html is some basic template)
{% if user_info.name and user_info.last_name %}
{% with "included_form.html" as BASE_TEMPLATE %}
{% include "edit_profile.html" %}
{% endwith %}
{% endif %}
and if you want show form as a standalone page, set BASE_TEMPLATE to base.html
Related
I'm attempting to make a base template for my website. Ideally it would look like this
{% block navigationbar %}{% endblock %}
{% block content %}{% endblock %}
{% block footer %}{% endblock %}
This way I am able to have separate navigationbar.html and footer.html files. All views I would like to show would then just put this:
{% extends 'portfolio/base.html' %}
{% block content %}
// View Code here
{% endblock %}
Any way to achieve this. I have realized I can't do multiple extends and I've tried different combinations of nesting but nothing has worked for me so far.
So the ideal result would be 4 files. for the homepage:
NavigationBar.html - Holds the navigation bar code
Footer.html - Holds the footer code
Base.html - Holds the navigation bar, footer, and content
Home.html - Holds the content of the home page, but when it loads the user sees the navigationbar, and footer as well.
You can use include that loads a template and renders it with the current context. This is a way of “including” other templates within a template.
This example includes the contents of the template "NavigationBar.html" and "Footer.html" on your template:
{% extends 'portfolio/base.html' %}
{% block content %}
{% include "NavigationBar.html" %}
// View Code here
{% include "Footer.html" %}
{% endblock %}
You can also pass additional context to the template using arguments:
{% include "NavigationBar.html" with breadcrumb="home > wherever" %}
On my homepage, I would like to use other pages I have defined as sections. After my navigation, I have a portion for news and team members that run as stand alone pages on my site. It looks something like this:
Header
Nav
<div class="content">
{% include 'news.html' %}
{% include 'officers.html' %}
</div>
Footer
So my news has some basic html but in order not to clone my headers and nav I have to add this line:
{% if page_data.current_page == 'news' %} {% extends "base.html" %} {% endif %}
Is there a way to simplify this statement?
An alternative way to deal with the issue is to move the body of news.html into a partial, for example partials/news.html and then include it in both your home page and in the news page itself:
{# home.html #}
Header
Nav
<div class="content">
{% include 'partials/news.html' %}
{% include 'officers.html' %}
</div>
Footer
and then in news.html:
{% extends "base.html" %}
{% block where_news_belongs %}
{{ super() }} {# if we need to include the contents of the block #}
{% include 'partials/news.html %}
{% endblock where_news_belongs %}
I am currently in the process of adding a theme to the admin of django one issue I have found it adding styling to the forms is that it is very difficult and I can't find much useful documentation on it. The only real thing I need to do is add classes to the form elements so that they match the theme I am using is this possible and if so how would you go about doing it the code I am currently using is very basic and the basic code included in the standard theme does anybody know how to add classes to these standard bits of code bellow is what I have.
{% if is_popup %}
<input type="hidden" name="_popup" value="1" />
{% endif %}
{% if save_on_top %}
{% block submit_buttons_top %}
{% submit_row %}
{% endblock %}
{% endif %}
{% if errors %}
<p class="errornote">
{% blocktrans count counter=errors|length %}
Please correct the error below.
{% plural %}
Please correct the errors below.
{% endblocktrans %}
</p>
{{ adminform.form.non_field_errors }}
{% endif %}
{% block field_sets %}
{% for fieldset in adminform %}
{% include "admin/includes/fieldset.html" %}
{% endfor %}
{% endblock %}
{% block after_field_sets %}{% endblock %}
{% block inline_field_sets %}
{% for inline_admin_formset in inline_admin_formsets %}
{% include inline_admin_formset.opts.template %}
{% endfor %}
{% endblock %}
{% block after_related_objects %}{% endblock %}
{% block submit_buttons_bottom %}
{% submit_row %}
{% endblock %}
There are a lot of ways to do this, but certainly one way would be to overwrite all the widgets in your ModelAdmin rather than in the template. That could look something like this:
from django.db import models
from django.contrib import admin
from django.forms.extras.widgets import TextInput
class MyModelAdmin(admin.ModelAdmin):
formfield_overrides = {
models.TextField: {'widget': TextInput(attrs={'class':'my-widget-class'},)},
}
You'd have to go through and do that for each widget, but then they'd have the appropriate classes -- at least for that modelAdmin.
I have a site with some ajax pages. If user types in a browser /login/, he should get a full rendered template, extended from a base template. But if user clicks a login button, $('#content').ajax('/login/'); called, so i don't need to render a full template.
I.e. i have this (login_ajax.html):
{% load i18n %}
{% block title %}
{% trans "Login" %}
{% endblock %}
{% block content %}
{% include "social.html" %}
{% endblock %}
In login.html:
{% extends "base.html" %}
{% block ajax_content %}
{% include "login_ajax.html" %}
{% endblock %}
Simple login view:
def login(request):
c = Context({'user': request.user})
if request.is_ajax():
return render_to_response('login_ajax.html', c, context_instance=RequestContext(request))
return render_to_response('login.html', c, context_instance=RequestContext(request))
This problem refers to documentation of include tag:
The include tag should be considered as an implementation of “render
this subtemplate and include the HTML”, not as “parse this subtemplate
and include its contents as if it were part of the parent”. This means
that there is no shared state between included templates – each
include is a completely independent rendering process.
But i don't want to place title name in a view, or place it twice in login.html and login_ajax.html also.
I think you need to move {% block title %} back out into login.html then make two ajax calls. One to override {% block ajax_content %} and one to override {% block title %}. You could use the same pattern for overriding {% block title %} as you've used for overriding {% block ajax_content %}, but you'd probably manage without actually creating a new title.html template.
I can't see any other way round your problem.
Ok, i've found a simple solution. In fact, the problem is in question: "to extend from base, or not to extend".
In fact, i don't care a template from what login.html should be extended. So, for ajax request, the parent template will be empty.html, and for default request, it will be base.html. So, i'll point a parent template in the view:
def login(request):
c = Context({'user': request.user, 'extends': 'empty.html'})
if request.is_ajax():
return render_to_response('login.html', c, context_instance=RequestContext(request))
c['extends'] = 'base.html'
return render_to_response('login.html', c, context_instance=RequestContext(request))
The empty.html just contain a placeholder for a block:
{% block content %}{% endblock %}
And here is login.html:
{% extends extends %}
{% load i18n %}
{% if extends != 'empty.html' %}
{% block title %}{% trans "Login" %}{% endblock %}
{% else %}
<div style="display: none;" class="ajax-title">{% trans "Login" %}</div>
{% endif %}
{% block content %}
{% include "social.html" %}
{% endblock %}
Also, i suppose, there is a way to turn login.html into a snippet, that could be included using with. i.e. {% include 'snippet.html' with extends='base.html' %}
So i have a working layout _layout.html (using Jinja2 v2.6 and Flask) which is including my header with {% include 'header.html' %} and the body contents with {% block content %}{% endblock %} (in that order).
header.html
<ul>
<li><a href="/about" {% if active_page == 'about' %} class="selected" {% endif %}>ABOUT</a></li>
</ul>
about.html
{% extends "_layout.html" %}
{% set active_page = 'about' %}
{% block content %}
...
{% endblock %}
The problem is that as the child templates are global and executed before the layout template is evaluated so the class="selected" are not being added as the header.html template does not have the active_page in its context.
If i place the header.html contents in the main layout everything works fine, how can i get this to work using the include and structure i have?
EDIT:
I have also tried {% include 'header.html' with context %} and {% from 'header.html' import input with context %} both do not work.
The easiest work around could be to just use JavaScript (JQuery in this case):
JQuery:
var currentPage = window.location.pathname;
$('nav ul li a').each(function(){
if($(this).attr('href') == currentPage){
$(this).addClass('selected');
}
});
This function will add a selected class to the <a> tag that matches the current window location.
This is how I do it:
in my _mainlayout
{% block stylesheets %}
Links to stylesheets go here
{% endblock %}
{% block main_body_area %}
Replace with your html body
{% endblock %}
{% block scripts %}
Add js scripts here
{% endblock %}
Then in the child templates simply do the following:
{% extends "_mainlayout.html" %}
{% block stylesheets %}
<style sheets go here/>
{% endblock %}
{% block main_body_area %}
<your page content here/>
{% endblock %}
{% block scripts %}
add any js scripts here
{% endblock %}
You can then add your active_page in the head section which in the example I gave you is where I keep the stylesheets. I do it this way because in my _mainlayout file I have a standard css stylesheet which is used across all pages and then if I need additional page specific layouts I include it in that particular page if not just leave it blank.
The same goes with the js files I don't want to load scripts on pages that dont need them so it makes it easy to include them on pages that do require specific js files.
I just had the same issue in Jinja2 version 2.8. Upgrading it to version 2.9 solved the problem.