【发布时间】:2020-05-30 14:55:53
【问题描述】:
我正在使用烧瓶,我想知道,如何将我的应用程序变量放入我的 html 模板中,而无需在调用模板时添加它,如本例所示:
render_template('home.html', app=app)
也许有一个我不知道的导入函数。
我想这样做,这样我就可以遍历我的所有端点(app.url_map.iter_rules())并将它们与当前端点(request.endpoint)进行比较。我当前的主要问题是将所有端点都放入我的 html 文件中。
我目前实现导航栏的方式是这样的:
<header>
<div class="head" align="center">
{% if request.endpoint!='users.login' and current_user.is_authenticated!=true %}
<a href="{{ url_for('users.login') }}">
<button class="tablecontent">Login</button>
</a>
{% endif %}
{% if request.endpoint!='users.register' %}
<a href="{{ url_for('users.register') }}">
<button class="tablecontent">Register</button>
</a>
{% endif %}
{% if request.endpoint!='core.home_page' %}
<a href="{{ url_for('core.home_page') }}">
<button class="tablecontent">Home</button>
</a>
{% endif %}
{% if current_user.is_authenticated %}
{% if request.endpoint!='users.logout' %}
<a href="{{ url_for('users.logout') }}">
<button class="tablecontent">Logout</button>
</a>
{% endif %}
{% endif %}
{% if current_user.is_authenticated %}
{% if request.endpoint!='products.new_product' %}
<a href="{{ url_for('products.add_product') }}">
<button class="tablecontent">Add Product</button>
</a>
{% endif %}
{% endif %}
</div>
</header>
当我尝试使用答案中的站点地图时出现导入错误:
File "C:\Users\ulman\PycharmProjects\database_website_optimised\source\database_website\application.py", line 20, in register_applications
from database_website.applications.core.urls import blueprint as core_blueprint
File "C:\Users\ulman\PycharmProjects\database_website_optimised\source\database_website\applications\core\urls.py", line 3, in <module>
from database_website.applications.core import views
File "C:\Users\ulman\PycharmProjects\database_website_optimised\source\database_website\applications\core\views.py", line 7, in <module>
from database_website.properties import sitemap
File "C:\Users\ulman\PycharmProjects\database_website_optimised\source\database_website\properties.py", line 4, in <module>
from database_website.application import application
File "C:\Users\ulman\PycharmProjects\database_website_optimised\source\database_website\application.py", line 39, in <module>
application = Application.create()
File "C:\Users\ulman\PycharmProjects\database_website_optimised\source\database_website\application.py", line 35, in create
instance.register_applications()
File "C:\Users\ulman\PycharmProjects\database_website_optimised\source\database_website\application.py", line 20, in register_applications
from database_website.applications.core.urls import blueprint as core_blueprint
ImportError: cannot import name 'blueprint' from 'database_website.applications.core.urls' (C:\Users\ulman\PycharmProjects\database_website_optimised\source\database_website\applications\core\urls.py)
我的视图文件:
from flask import Flask, render_template, url_for, flash, redirect, request
from flask.views import MethodView
from database_website.applications.views import FormViewMixin
from database_website.applications.products.models import Product
from database_website.applications.core import forms
from database_website.application import application
def has_no_empty_params(rule):
defaults = rule.defaults if rule.defaults is not None else ()
arguments = rule.arguments if rule.arguments is not None else ()
return len(defaults) >= len(arguments)
def sitemap():
links = []
for rule in application.url_map.iter_rules():
# Filter out rules we can't navigate to in a browser
# and rules that require parameters
if "GET" in rule.methods and has_no_empty_params(rule):
url = url_for(rule.endpoint, **(rule.defaults or {}))
links.append((url, rule.endpoint))
return links
class HomePageView(MethodView, FormViewMixin):
def get(self):
form = forms.ProductSearchForm()
products = Product.query.all()
return render_template('core/home.html', title='Home', products=products, form=form)
def post(self):
product_search = request.form.get('search_name')
return redirect(url_for('products.search_product', search_name=product_search))
这是当它硬编码站点地图时,它仍然显示完全相同的错误,因为当时导入应用程序对象时它还不存在。我可以展示我初始化应用的方式。
【问题讨论】:
-
据我所知,这是最简单最常见的方法。我相信你可能会做一些令人费解的黑客攻击。但是您上面的示例通常是如何完成的。你有什么特别的原因想要以不同的方式做这件事吗?您能否为您的问题添加更多信息和上下文?
-
我想制作一个导航栏,我很清楚如何在没有很多 if 循环的情况下制作它,它涉及使用 app.url_map。因为它会显示在每个页面中,所以我需要在渲染模板的每个地方都包含这个变量。我知道有 Flask-Nav,但我只是想尝试在不安装另一个库的情况下制作它。
-
这能回答你的问题吗?或者这是否有助于您实现您想要实现的目标? stackoverflow.com/questions/62094647/…
-
不,因为我已经知道如何让 html 作为标题显示在每个页面上,我只需要找到一种方法将我的应用程序导入基本 html 模板,所以我在我的模板中使用了 app.url_map,但也许我只是错过了那篇文章中的那部分。编辑:也许包含可以与 .py 文件一起使用。
-
让我看看我是否正确理解了您的问题。 (1) 你想创建一个导航栏。 (2)您希望导航栏出现在每个页面上?
标签: python html python-3.x flask