- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个 Python-Flask 应用程序,并尝试实现一个简单的搜索功能:
home/views.py
from . import home_blueprint
################################# SEARCH #################################
@home_blueprint.route("/search")
@login_required
def search():
results = Politic.query.whoosh_search(request.args.get('query')).all()
#results = Politic.query.whoosh_search(query).all()
for p in results:
print p.publicName
print results
return render_template('search_results.html',
results=results)
非常简单,在我将蓝图纳入我的项目之前,我就让它工作了。当我在我的 View 中调用这个函数时,我什么也得不到。
已更新!我的base.html文件:
<form class="form-inline" method="GET" action="{{url_for('home.search')}}">
<div class="form-group">
<label for="query">Poltician</label>
<input type="text" class="form-control" name="query" id="query">
</div>
<button type="submit" class="btn btn-primary">Search</button>
</form>
由于 url_for 查找函数,因此我传递了 home_blueprint 和我想要调用的函数(搜索)。
有什么原因导致我无法进入搜索功能吗?我在 .html 文件中做错了什么吗?
致以诚挚的问候
编辑:(init.py)
#################
#### imports ####
#################
################
#### config ####
################
from flask import Flask, g
app = Flask(__name__)
from project.models import db, User, Politic, Organization
import datetime
from forms import SearchForm
import os
from flask_login import LoginManager, \
current_user
print(os.environ['APP_SETTINGS'])
app.config.from_object(os.environ['APP_SETTINGS'])
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['WHOOSH_BASE'] = 'whoosh'
db.init_app(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "users.login"
from project.users.views import users_blueprint
#from project.home.views import home_blueprint
from project.home import home_blueprint
from project.politicians.views import politicians_blueprint
from project.organizations.views import organizations_blueprint
from project.proposals.views import proposals_blueprint
from project.flag.views import flag_blueprint
# register our blueprints
app.register_blueprint(users_blueprint)
app.register_blueprint(home_blueprint)
app.register_blueprint(politicians_blueprint)
app.register_blueprint(organizations_blueprint)
app.register_blueprint(proposals_blueprint)
app.register_blueprint(flag_blueprint)
@app.before_request
def before_request():
g.user = current_user
if g.user.is_authenticated:
g.user.last_seen = datetime.datetime.utcnow()
db.session.add(g.user)
db.session.commit()
g.search_form = SearchForm()
@login_manager.user_loader
def user_loader(email):
"""Given *user_id*, return the associated User object.
:param unicode user_id: user_id (email) user to retrieve
"""
return User.query.filter_by(email=email).first()
编辑我的项目树:
我做了metmirr建议的更改,现在我的项目结构是这样的:project/home/__init__.py
from flask import Blueprint
home_blueprint = Blueprint(
'home', __name__,
template_folder='templates'
)
from . import views
URL 当我搜索时:
何时应该:
不过,它并没有进入搜索功能。我不知道我做错了什么。
任何帮助都很棒!谢谢。
最佳答案
如果您在项目中打开 python 解释器(如果您使用的是虚拟环境,请确保激活虚拟环境),您可以检查应用程序上注册的端点以及它们指向的函数。
>>> import app
>>> app = app.create_app()
>>> app.url_map
Map([<Rule '/search' (OPTIONS, POST) -> **folder_where_your_search_views_are_located**.search>,
<Rule '/search_results/<query>' (OPTIONS, GET) -> **folder_where_your_search_views_are_located**.search_results>])
关于python - jinja2蓝图路线错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41275073/
我有两种类型的路由 Public 和 Private。 只有用户登录后才能访问所有私有(private)路由: return tokenService.token ? ( <>
我已按照 Laravel 5.5 文档在我们的应用程序上要求、安装和配置 Laravel Passport。我们仅使用密码授予功能,因为我们不打算将其用作社交登录工具。但是,按照所有说明操作后,我在尝
我想设置事件菜单项的样式,为此我需要将当前 url 与路由进行比较。我知道我可以在 javascript 中做到这一点,但我想知道其他人是如何解决这个问题的。 有什么建议么? 伪代码: My Page
我正在尝试在浏览器上以图形方式显示路径/路线以供客户查看。例如,基于 txt 或 XML 文件,包含说明。 4 90 5 90 2 或 F4,L90,F5,L90,F2 相当于
我创建了一个中间件来阻止我的 laravel 应用程序中的某些路由,但不起作用,无法弄清楚我做错了什么,这是我的代码: ps:我使用的是 laravel 5.2 路线: Route::get('sec
我正在使用 Java 工作。给定一个矩阵 NxM,我需要找到通过该数组的所有可能路径。只允许斜向上或斜向下,或向右斜行。 4x4 矩阵示例: 3 5 7 9 2 4 6 8 9 3 7
我是 Marionette 新手,只是找不到上类路线。 我正在使用 Marionette 的 2.4.1 版本,并尝试以最简单的方式进行操作,以便它能够正常工作。 此代码适用于旧版本的 Marione
我是 AngularJS 的新手。我正在尝试从这个网站( https://docs.angularjs.org/tutorial/step_07 )学习 AngularJS。我的代码如下 index.
我在 yandexmapkit-android 项目上工作。图书馆链接是 https://github.com/yandexmobile/yandexmapkit-android 文档非常薄弱,git
我正在阅读有关 Angular 路由的文档并创建了一个简单的测试: const routes: Route[] = [ { path: '', redirectTo: '/home', pat
我正在开发一项服务 (spring-boot),它获取一个 ID 列表,一个一个地从数据库中获取对象,将这些对象聚合成批处理,然后将它们保存在其他地方。目前,聚合后的批量大小约为 50 个对象,大约每
我正在制作一个网站,在用户登录后,用户将被重定向到主页。网站的主页和所有其他页面只能由登录用户访问,但即使在用户登录后(firebase auth),网站的其余部分( protected 路由)仍然无
我有一个惰性模块,我希望在桌面和移动设备上有不同的体验。基本上我想要我的桌面布局如下: Component1 显示一个列表,用户在列表中选择一个项目,component2 将显示详细信息。我创建了名为
我是 Angular 的新手,我正在尝试让我的路由器工作。基本上我在 / 有一个主页,其中有一个到 /courses 的路由器链接,它运行良好,但是当我重新加载 /courses 时(或输入地址in)
完整的 Mojolicious 应用程序有 routes将转储应用程序路由的命令: script/my_app.pl routes 我如何从 Lite 的测试脚本中做同样的事情应用? use Mojo
我有一个 Camel 2.13.1 应用程序,它使用我通过 CXF 组件访问的外部 Web 服务。我使用 Spring XML 路由元素的 startupOrder 属性来确保在我设置为在启动时调用一
我们有一个在 Karaf 2.4.3 和 Camel 2.15.3 上运行的数据处理应用程序。 在这个应用程序中,我们有一堆导入数据的路由。我们有一个管理 View ,其中列出了这些路由以及每条路由的
我正在尝试组合一个应用程序,我可以在其中查询谷歌路线服务,存储结果以建立缓存,然后根据需要呈现路线。 我可以取回方向数据并将其存储在数据库中就好了,这一切都很好,现在当我在 map 上渲染方向时,我的
我根据 Ryan Bates 的 railscast 使用设计登录创建了一个新项目. 它没有注册路线(与我之前制作的项目不同,步骤完全相同) This image显示了两个“rake 路由”命令。顶
我发现 Google Maps API 通过以下方式支持路线: var map; var directionsPanel; var directions; function initialize()
我是一名优秀的程序员,十分优秀!