gpt4 book ai didi

python - jinja2蓝图路线错误

转载 作者:行者123 更新时间:2023-11-30 22:45:10 25 4
gpt4 key购买 nike

我正在开发一个 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()

编辑我的项目树:

enter image description here

我做了metmirr建议的更改,现在我的项目结构是这样的:project/home/__init__.py

from flask import Blueprint

home_blueprint = Blueprint(
'home', __name__,
template_folder='templates'
)

from . import views

URL 当我搜索时:

http://localhost:5000/home?query=new

何时应该:

http://localhost:5000/search?query=new

不过,它并没有进入搜索功能。我不知道我做错了什么。

任何帮助都很棒!谢谢。

最佳答案

如果您在项目中打开 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com