EsseTi
EsseTi

Reputation: 4271

Python template include a page if condition

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.

Upvotes: 0

Views: 524

Answers (1)

yedpodtrzitko
yedpodtrzitko

Reputation: 9359

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

Upvotes: 2

Related Questions