错误的jinja2蓝图路线 [英] Wrong jinja2 blueprint route

查看:175
本文介绍了错误的jinja2蓝图路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Python-Flask应用程序,并试图实现一个简单的搜索功能:

主页/views.py

 自。 import home_blueprint 
################################# SEARCH ########## #######################


@ home_blueprint.route(/ search)
@ ():
def search():$ b $查询).all()
为结果中的p:
print p.publicName
打印结果
返回render_template('search_results.html',
results = results)

非常简单,在将项目蓝图纳入之前,当我在我的视图中调用这个函数时,我什么也没有得到。

已更新!我的 base.html 文件:

 < form class =form-inlinemethod =GETaction ={{url_for('home.search')}}> 
< div class =form-group>
< label for =query> Poltician< / label>
< input type =textclass =form-controlname =queryid =query>
< / div>
< button type =submitclass =btn btn-primary>搜寻< / button>
< / form>

由于url_for寻找一个函数,我通过了我的home_blueprint和我想调用的函数(搜索)。



是否有任何理由不进入搜索功能?我在 .html 文件中做错了什么?



最好的问候

编辑: init .py)

  ##### ############ 
#### imports ####
#################

################
#### config ####
############# ###
从瓶子进口烧瓶,g
app =烧瓶(__ name__)

从project.models进口数据库,用户,政治,组织
import datetime
从表单导入SearchForm
导入os $ b $ from flask_login导入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)
$ b $ login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view =用户.login


from project.users.views import users_blueprint
#from project.home.views从project.home导入home_blueprint
import home_blueprint
来自project.politicians.views从project.organizations.views导入politicians_blueprint
导入organizational_blueprint
从project.proposals.views导入proposal_blueprint
从project.flag.views导入flag_blueprint
#register我们的蓝图
app.register_blueprint(users_blueprint)
app.register_blueprint(home_blueprint)
app.register_blueprint(politicians_blueprint)
app.register_blueprint(organizations_blueprint)
app.register_blueprint( ():
app.register_blueprint(flag_blueprint)


@ app.before_request
def before_request():
g.user = current_user
如果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):
给定* user_id *,返回关联的User对象。

:param unicode user_id:user_id(email)用户检索

返回User.query.filter_by(email = email).first()

编辑我的项目树:


什么时候应该是:


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


Still ,它没有进入搜索功能。
我不知道我在做什么错。



任何帮助都很棒!如果你在你的项目中打开python解释器,(确保在你使用的时候激活你的虚拟env一)您可以检查应用程序上注册的端点和他们指向哪些功能。 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>])


I'm developing a Python-Flask application and I'm trying to implement a simple search function:

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)

Pretty simple and I got it working before I included blueprint on my project. When I'm calling this function on my view, I don't get anything.

updated! my base.html file:

<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>

Since url_for looks for a function I passed my home_blueprint and the function I want to call (search).

Is there any reason why I'm not getting the into the search function? Am I doing something wrong in my .html file?

Best regards

EDIT:(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()

EDIT My project tree:

I did the changes metmirr suggested and now my project structure is this: project/home/__init__.py

from flask import Blueprint

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

from . import views

URL when I search:

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

when it should be:

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

Still, it doesn't get into the search function. I got no idea what I'm doing wrong.

Any help is great! Thanks.

解决方案

If you open the python interpreter in your project, (Make sure to activate your virtual env if you're using one) you can check the registered endpoints on the app and to which functions they point.

>>> 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>])

这篇关于错误的jinja2蓝图路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆