错误的jinja2蓝图路线

时间:2016-12-22 02:49:54

标签: python flask jinja2

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

主/ views.py

public class Edit_Fragment : Fragment
{
    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        return inflater.Inflate(Resource.Layout.edit, container, false);
    }
}

非常简单,在我将蓝图纳入项目之前,我已经开始工作了。当我在我的观点上调用此功能时,我什么也得不到。

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

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)

由于url_for查找函数,我传递了home_blueprint和我要调用的函数(搜索)。

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

祝你好运

修改(适用的初始化的.py)

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

编辑我的项目树:

enter image description here

我做了metmirr建议的更改,现在我的项目结构如下: 项目的/ home / __ __初始化。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()
我搜索时

网址

  

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

应该是:

  

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

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

任何帮助都很棒!感谢。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

我想我遇到了你的问题。您可能正在同一位置创建BlueprintFlask应用程序。那么蓝图与应用程序类似,并且烧瓶具有蓝图,因此您可以拥有大型应用程序结构。我将向您展示两个示例,第一个不起作用,另一个示例:

from flask import Flask, Blueprint

app = Flask(__name__)

main = Blueprint('main', __name__)
app.register_blueprint(main)

@main.route('/')
def index():
    return "Hello Flask"
app/
   app.py
   main/
      __init__.py
      views.py
      forms

#app/app.py

from flask import Flask

from main import main as main_blueprint

app = Flask(__name__)
app.register_blueprint(main_blueprint)

if __name__ == '__main__':
    app.run()

#main/__init__.py
from flask import Blueprint

main = Blueprint('main', __name__)

# importing this module causes the routes to be associated with the blueprint
from . import views, forms

#main/views.py
from . import main

@main.route('/')
def index():
    return "Hello Flask"

所以现在写action="{{url_for('main.index')}}"时工作正常。