我在Python编程方面的经验有限,而且我在网上搜索了很长时间。但没用。请帮助或尝试提供一些如何实现这一目标的想法。
错误消息:
Traceback (most recent call last):
File "G:\Flask\Sample\venv\lib\site-packages\tornado\web.py", line 1488, in _execute
result = self.prepare()
File "G:\Flask\Sample\venv\lib\site-packages\tornado\web.py", line 2774, in prepare
self.fallback(self.request)
File "G:\Flask\Sample\venv\lib\site-packages\livereload\server.py", line 89, in __call__
WSGIContainer.environ(request), start_response)
File "G:\Flask\Sample\venv\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "G:\Flask\Sample\venv\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "G:\Flask\Sample\venv\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "G:\Flask\Sample\venv\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "G:\Flask\Sample\venv\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "G:\Flask\Sample\venv\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "G:\Flask\Sample\venv\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "G:\Flask\Sample\app\main\views.py", line 7, in index
return render_template('index.html', title='Welcome')
File "G:\Flask\Sample\venv\lib\site-packages\flask\templating.py", line 134, in render_template
context, ctx.app)
File "G:\Flask\Sample\venv\lib\site-packages\flask\templating.py", line 116, in _render
rv = template.render(context)
File "G:\Flask\Sample\venv\lib\site-packages\jinja2\environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "G:\Flask\Sample\venv\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "G:\Flask\Sample\app\templates\index.html", line 2, in top-level template code
{% import '_macro.html' as ma %}
File "G:\Flask\Sample\app\templates\base.html", line 1, in top-level template code
{% extends 'bootstrap/base.html' %}
File "G:\Flask\Sample\venv\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 1, in top-level template code
{% block doc -%}
File "G:\Flask\Sample\venv\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 4, in block "doc"
{%- block html %}
File "G:\Flask\Sample\venv\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 20, in block "html"
{% block body -%}
File "G:\Flask\Sample\venv\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 21, in block "body"
{% block navbar %}
File "G:\Flask\Sample\app\templates\base.html", line 13, in block "navbar"
{{ nav.top.render() }}
File "G:\Flask\Sample\venv\lib\site-packages\flask_nav\elements.py", line 24, in render
self))
File "G:\Flask\Sample\venv\lib\site-packages\visitor\__init__.py", line 48, in visit
return meth(node)
File "G:\Flask\Sample\venv\lib\site-packages\flask_bootstrap\nav.py", line 53, in visit_Navbar
bar_list.add(self.visit(item))
File "G:\Flask\Sample\venv\lib\site-packages\visitor\__init__.py", line 48, in visit
return meth(node)
File "G:\Flask\Sample\venv\lib\site-packages\flask_bootstrap\nav.py", line 98, in visit_View
item.add(tags.a(node.text, href=node.get_url(), title=node.text))
File "G:\Flask\Sample\venv\lib\site-packages\flask_nav\elements.py", line 77, in get_url
return url_for(self.endpoint, **self.url_for_kwargs)
File "G:\Flask\Sample\venv\lib\site-packages\flask\helpers.py", line 333, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "G:\Flask\Sample\venv\lib\site-packages\flask\app.py", line 1805, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "G:\Flask\Sample\venv\lib\site-packages\flask\helpers.py", line 323, in url_for
force_external=external)
File "G:\Flask\Sample\venv\lib\site-packages\werkzeug\routing.py", line 1768, in build
raise BuildError(endpoint, values, method, self)
BuildError: Could not build url for endpoint 'index'. Did you mean 'main.index' instead?
这是我项目的目录布局:
/Sample
/app
/auth
/__init__.py
/forms.py
/views.py
/main
/__init__.py
/views.py
/templates
/index.html
/base.html
/login.html
/__init__.py
/models.py
/manager.py
/venv
我认为我的问题出现在/main/views.py中,但我找不到它。
/main/views.py:
from flask import render_template, request, redirect, url_for, make_response, flash
from . import main
@main.route('/')
def index():
return render_template('index.html', title='Welcome')
@main.route('/services')
def services():
return render_template('index.html', title='services')
@main.route('/about')
def about():
return render_template('index.html', title='about')
@main.route('/user/<regex("[a-z]{3}"):user_id>')
def user(user_id):
return 'User %s' % user_id
@main.route('/work/')
def projects():
return render_template('index.html', title='projects')
@main.errorhandler(404)
def page_not_found(error):
return render_template('404.html'), 404
# @main.template_test('current_link')
# def current_link(link):
# return link == request.path
def read_md(filename):
with open(filename) as md_file:
content = reduce(lambda x, y: x + y, md_file.readline())
return content.decode('utf-8')
@main.context_processor
def inject_methods():
return dict(read_md=read_md)
/主/的初始化的.py:
from flask import Blueprint
main = Blueprint('main', __name__)
from . import views
/应用/的初始化的.py:
from os import path
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_nav import Nav
from flask_nav.elements import *
from flask_sqlalchemy import SQLAlchemy
from werkzeug.routing import BaseConverter
class RegexConverter(BaseConverter):
def __init__(self, url_map, *items):
super(RegexConverter, self).__init__(url_map)
self.regex = items[0]
basedir = path.abspath(path.dirname(__file__))
bootstrap = Bootstrap()
#app.config['BOOTSTRAP_SERVE_LOCAL'] = True
db = SQLAlchemy()
nav = Nav()
def create_app():
app = Flask(__name__)
app.url_map.converters['regex'] = RegexConverter
app.config.from_pyfile('config')
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = \
'sqlite:///' + path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
nav.register_element('top', Navbar('Flask',
View('Index', 'index'),
View('About', 'about'),
View('Services', 'services'),
View('Projects', 'projects')))
nav.init_app(app)
bootstrap.init_app(app)
db.init_app(app)
from .auth import auth as auth_blueprint
from .main import main as main_blueprint
app.register_blueprint(auth_blueprint)
app.register_blueprint(main_blueprint)
return app
答案 0 :(得分:1)
问题在于调用url_for('index')
。正如您的错误所述,您必须使用url_for('main.index')
。如果此调用位于同一蓝图中,您甚至可以将其设为相对url_for('.index')
。
这是一个简单的修复方法,但在您粘贴的所有代码中,您都没有将代码粘贴到问题中。