0

I have bulit a Flask app and it has a package called projects. I have an issue when I try to render a template - it does not show in the HTML page. The queried database table is named “project”.

Here is the routes.py file content:

from flask import render_template, redirect, request, url_for, flash
from flask_login import (
    current_user,
    login_required,
    login_user,
    logout_user
)

from app import db, login_manager
from app.projects import blueprint
from app.projects.models import Project

@blueprint.route("/projects_list")
@login_required
def projects_list():

    projects = Project.query.all()

    return render_template('projects_list.html', projects=projects)

projects_list.html:

{% extends "base_site.html" %}

{% block title %} Projects {% endblock title %}

{% block stylesheets %}
  {{ super() }}
{% endblock stylesheets %}

{% block content %}

    <div class="right_col">
      <div>Project Name:
        {% for project in projects %}
        {{ project.project_name }}
        {% endfor %}
      </div>
    </div>

{% endblock content %}

models.py:

from datetime import datetime
#from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from flask import current_app
from flask_login import current_user, login_required
from app import db, login_manager
from flask_login import UserMixin

class Project(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    project_name = db.Column(db.String(100), nullable=False)
    date_created = db.Column(db.DateTime, nullable=True, default=datetime.utcnow)
    keywords = db.Column(db.Text, nullable=False)

    def __repr__(self):
        return f"Project('{self.project_name}')"
amsr
  • 19
  • 4
  • Were I in your position, I'd also pass `nprojects=len(projects)` into the template as a quick check that database access was wired up correctly. – Dave W. Smith Jul 15 '18 at 23:08
  • I tried it and nothing appears! also, I have changed "projects = Project.query.all()" to "projects = [1, 2, 3, 4, 5]" and still the passed list doesn't show up in the template. It seems that I have an issue with routing! – amsr Jul 16 '18 at 09:11
  • Just fixed it :) the issue was in the passed URL as I was only passing the direct route address instead of package name +route. – amsr Jul 16 '18 at 09:40

0 Answers0