【问题标题】:Flask HTML Module access Views.py function's attributeFlask HTML Module 访问 Views.py 函数的属性
【发布时间】:2017-09-14 00:35:51
【问题描述】:

我正在创建一个仪表板,当时我正在根据数据库中的表名创建一个导航栏(这部分没问题)。

在views.py中我有(编辑:已经添加了views.py的其余部分):

    from flask import render_template
from app import app

import sqlite3, json

@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html',title='Homepage',)
def nav_bar():
    connection = sqlite3.connect('C:\\SQLite\\Databases\\testPython.db')
    cursor = connection.cursor()

    cursor.execute('SELECT NAME FROM sqlite_master WHERE TYPE = \'table\';')
        #ORDER BY NAME ASC
        #tableNames = cursor.fetchall()

    connection.close()

    tableNames = json.dumps(cursor.fetchall()).replace("\"], [\"", " ").replace("[[\"","").replace("\"]]","").replace("access_","").split()

    return tableNames

我在代码所在的navigation.html中创建了导航-

{% for names in navBar.tableNames %}
    <h1>Test</h1>
{% endfor %}

编辑:这是base.html

<html>
    <head>
        <title>{{ title }} - Dashboard</title>
  </head>
  <body style="margin: 0; padding: 0;">
        <div style="height:7%; background-color: #171819;"><a href="/" style="color: white; vertical-align: middle; text-decoration: none; font-family: Sans-serif; font-size: 20px; position: relative; transform: translateY(-50%); top: 35%; padding-left: 5%">Access Log Dashboard >>></a></div>
        <div class="container">
          <div class="column-left" style="float: left; width: 15%; background-color: #3c3f42; height: 93%">
            {% include 'navigation.html' %}
          </div>
          <div class="column-centre" style="display: inline-block; width: 80%; color: #3c3f42;">
            {% block content %}
            {% endblock %}
          </div>
        </div>
  </body>
</html>

如何从 navigation.html 访问 views.py 中的属性?抱歉,如果这是一个新问题,我认为是!

【问题讨论】:

  • 你能发给你请添加你的views.py问题吗??
  • @EspoirMurhabazi 添加了额外的views.py,如果你问的是这个?

标签: python html flask


【解决方案1】:

首先,您需要将 nav_bar 函数返回的表名传递给您的渲染模板,如下所示:

from flask import render_template
from app import app

import sqlite3, json

@app.route('/')
@app.route('/index')
def index():
    table_names = nav_bar()
    return render_template('base.html',title='Homepage',table_names = table_name)
def nav_bar():
    connection = sqlite3.connect('C:\\SQLite\\Databases\\testPython.db')
    cursor = connection.cursor()

    cursor.execute('SELECT NAME FROM sqlite_master WHERE TYPE = \'table\';')
        #ORDER BY NAME ASC
        #tableNames = cursor.fetchall()

    connection.close()

    table_names = json.dumps(cursor.fetchall()).replace("\"], [\"", " ").replace("[[\"","").replace("\"]]","").replace("access_","").split()

    return table_names

然后像这样将其调用到您的模板中:

{% for names in table_names %}
    <h1>names</h1>
{% endfor %}

【讨论】:

  • 我可以把它作为一个模块而不是一个模板来做吗?因为我想基本上将navigation.html 连接到base.html,而不是索引,所以我知道我可以在每个页面上都有这个,但我宁愿把它放在base.html 上。对不起,我很痛苦!
  • 请具体告诉我你想要什么?如果答案对您有帮助,请点赞,我将对其进行编辑
  • 将编辑我的问题以添加 base.hml。但是我有 base.html 来包含 navigation.html 并且希望基本上将其作为模块而不是模板。从模块中,它将能够访问 nav_bar() 函数来获取 table_names 变量。然后在模块中创建一个 列表(我还没有添加它,但它是我将来要做的)然后将在 base.html 的每个页面中找到。
  • 好的!做我会帮你的
  • 谢谢,非常感谢:)
猜你喜欢
  • 1970-01-01
  • 2018-06-03
  • 2012-07-27
  • 1970-01-01
  • 2017-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-19
相关资源
最近更新 更多