Flask: one template does not extend base correctly

One of my templates (offers_overview.html) does not inherit my base.html template correctly. Instead it just outputs the html given in offers_overview.html without the html from base.html.

I have another template (customer_overview.html) with the exact same html as offers_overview.html and it extends the base.html perfectly fine. That is what makes me confused. What am I doing wrong?

Here is what my views.py looks like for offers_overview:

@mod.route('/<username>/<int:inquiry_id>', methods=['GET', 'POST'])
@login_required
def show_offers_overview(username, inquiry_id):
 user = Customers.query.filter_by(cusername=username).first()
 inquiry = Inquiry.query.filter_by(iid=inquiry_id).first()

 if (user or inquiry) is None:
    flash('User %s or inquiry %s is not found.' % (username, inquiry_id))
    return redirect(url_for('user.user_overview', username=user.cusername))
 return render_template('offers_overview.html', inquiry=inquiry)

Here is what views.py looks like for customer_overview:

@mod.route('/<username>', methods=['GET', 'POST'])
@login_required
def user_overview(username):
 user = Customers.query.filter_by(cusername=username).first()
 if user is None:
    flash('User %s not found.' % username)
    return redirect('/user/login')
 return render_template('customer_overview.html', customer=user)

Here is offers_overview.html:

{% extends 'base.html' %}

{% block title %}Offers overview{% endblock title %}
{% block body %}
  <h2>Not important {{ inquiry.worktitle }}</h2>
  <br>
  <h3>Future</h3>
  <br>
  <h3>Present</h3>
  <br>
  <h3>Past</h3>
{% endblock body %}

Which is exactly the same code as in customer_overview.html, except from inquiry being changed to customer.

I tried changing which template to be rendered in show_offers_overview() to customer_overview. The result was that the child template did not inherit the base template.

UPDATE This is how the templates are structured:

|project
   |app
      |templates
         |--base.html
      |user
         |templates
            |--customer_overview.html
            |--offers_overview.html
         |--__init__.py
         |--views.py

Answer

I found out that in my base.html I had code that made deeper pages not inheritate the static file off-canvas.css. For example page

localhost:5000/user/myusername

would work, but not

localhost:5000/user/myusername/123

It looked like this in base.html where I refered to the css static file:

<link href="../static/off-canvas.css" rel="stylesheet">

I had to change to url_for in base.html, see here:

<link href="{{url_for('static', filename='off-canvas.css')}}" rel="stylesheet">

Leave a Reply

Your email address will not be published. Required fields are marked *