问题描述
根据我对Python编程的有限经验,我在网上搜索了很长时间.但是没用.请提供帮助或尝试提供一些想法,以实现这一目标.
In my limited experience with Python programing and I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.
错误消息:
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?
这是我的项目的目录布局:
This is the directory layout of my project:
/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中,但找不到.
I think my problem is in /main/views.py but I can't find it .
/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)
/main/ init .py:
/main/init.py:
from flask import Blueprint
main = Blueprint('main', __name__)
from . import views
/app/ init .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
推荐答案
问题在于调用 url_for('index')
.如您的错误所述,您必须使用 url_for('main.index')
.如果此调用位于同一蓝图中,您甚至可以使其相对 url_for('.index')
.
The problem is with a call to url_for('index')
. As your error said, you must use url_for('main.index')
. If this call is in the same blueprint, you can even make it relative url_for('.index')
.
这是一个简单的修复程序,但是在您粘贴到此处的所有代码中,您并没有粘贴有问题的代码.
It is an easy fix, but in all code you pasted here, you didn't pasted the code with the problem.
这篇关于关于Python Flask,"BuildError:无法为端点'index'建立url.您是说'main.index'吗?"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!