Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python A Social Network with Flask How to Win Friends Suggestions

Andy Hughes
Andy Hughes
8,479 Points

Nesting templates

I've kind of asked this question already but didn't really get a usable answer. So I thought I'd try again.

In Kenneth's social app we build various templates to view information. But what about if we want to show templates, within templates? How do we do that?

My thinking came from considering backend administration. For example, we have a check in the social app to see if someone is an admin. If they are........then what? At the moment, this check is irrelevant because we can't do anything with it.

In reality, as an admin, we should be able to see and do certain things normal users can't. Yet given how common a requirement this is, it has not been covered at all.

So a simple question would be:

If I have a form in a template, that allows admins to add/delete users; and I want this to be visible on the admin page. How would I do it?

Let's assume the user is logged in and is taken immediately to an admin.html page that extends layout.html. How do I show the add/delete user template, inside of the admin template.

Just so you know, I've tried this a bunch of times and always get errors. Can one of the staff or moderators please explain how to go about doing this please?

1 Answer

Andy Hughes
Andy Hughes
8,479 Points

For anyone elses benefit. I finally managed to figure this out.

As an example, if I have an admin template view and I want to include static (non-dynamic) pages in it, I can do this using:

{% include 'filename.html' %}

which should work just fine (or at least it does when I try it). However, if you want to include a template with dynamic content, such as in my case (a form), it won't work without declaring any variables in the parent template. You'll recieve an 'undefined' error.

In short, this is telling you that you're trying to include something in the parent template, but that template's never heard of the thing you're trying to include. So you must reference any variables or functions properly so that the parent template can use them.

As an example, my parent admin.html template looked like this originally (which didn't work):

@app.route('/admin')
@login_required
def admin():
    if (models.User.is_admin == True):
        return render_template('admin.html')
    else:
        return redirect(url_for('index'))

But if I change it to include the form variable I want to pass to the template and reference this variable at the end of the render_template statement, it works as expected.

@app.route('/admin')
@login_required
def admin():
    form = forms.StandardForm()
    if (models.User.is_admin == True):
        return render_template('admin.html', form=form)
    else:
        return redirect(url_for('index'))

Took me over a month to figure this out!! Hope it helps someone else. :)