关于python Nav框架的问题

来源:互联网 发布:unity3d语言 编辑:程序博客网 时间:2024/06/06 03:47

由于初学者 刚学 有些知识没学到位

花了一天时间找错误 才发现是路径跳转的问题 汗!!

项目结构:

子结构及代码:
# -*- coding:utf-8 -*-from flask import Flask, render_templatefrom flask_bootstrap import Bootstrapfrom flask_nav import Navfrom flask_nav.elements import *app = Flask(__name__)Bootstrap(app)nav = Nav()nav.register_element('top', Navbar(u'Flask入门',View(u'主页', 'index'),View(u'关于', 'about'),View(u'服务', 'Services'),View(u'项目', 'projects'), ))#第二个projects是根据 路由函数名称来查找的nav.init_app(app)@app.route('/')def hello_world():return 'Hello sdas!'@app.route('/jinjia')def swe2():return render_template('jingjia.html',title='welcome')if __name__ == '__main__':app.run()
{% extends 'base.html' %}{% import '_marcos.html' as ui %}{% block title %}{{ title }}{% endblock %}{% block content %}<div class="page-header"><div class="container"><h1>{{ self.title() }}</h1></div></div><div class="container">{{ ui.input('name') }}{{ ui.input('password',type='password') }}</div>{% endblock content %}

{% extends 'bootstrap/base.html' %}{% block head %}{{ super() }}{% endblock %}{% block navbar %}{{ nav.top.render() }}{% endblock %}{% block content %}<header>{% block header %}{% endblock %}</header>{% endblock %}{% block footer %}<footer>Copyright 2015 by<a href="ray.dotnetage.com">Ray</a></footer>{% endblock %}

{#定义宏#}{% macro input(name,value='',type='text',size=20) %}<input type="{{ type }}"name="{{ name }}"value="{{ value }}"size="{{ size }}"/>{% endmacro %}
运行flask4.py后 执行http://127.0.0.1:5000/jingjia
出现问题:
Traceback (most recent call last):File "C:\Python27\lib\site-packages\flask\app.py", line 1982, in wsgi_appresponse = self.full_dispatch_request()File "C:\Python27\lib\site-packages\flask\app.py", line 1614, in full_dispatch_requestrv = self.handle_user_exception(e)File "C:\Python27\lib\site-packages\flask\app.py", line 1517, in handle_user_exceptionreraise(exc_type, exc_value, tb)File "C:\Python27\lib\site-packages\flask\app.py", line 1612, in full_dispatch_requestrv = self.dispatch_request()File "C:\Python27\lib\site-packages\flask\app.py", line 1598, in dispatch_requestreturn self.view_functions[rule.endpoint](**req.view_args)File "D:/python/flask4/flask4.py", line 25, in swe2return render_template('jingjia.html',title='welcome')File "C:\Python27\lib\site-packages\flask\templating.py", line 134, in render_templatecontext, ctx.app)File "C:\Python27\lib\site-packages\flask\templating.py", line 116, in _renderrv = template.render(context)File "C:\Python27\lib\site-packages\jinja2\environment.py", line 1008, in renderreturn self.environment.handle_exception(exc_info, True)File "C:\Python27\lib\site-packages\jinja2\environment.py", line 780, in handle_exceptionreraise(exc_type, exc_value, tb)File "D:\python\flask4\templates\jingjia.html",line 2, in top-level template code{% import '_marcos.html' as ui %}File "D:\python\flask4\templates\base.html", line 1, in top-level template code{% extends 'bootstrap/base.html' %}File "C:\Python27\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html",line 1, in top-level template code{% block doc -%}File "C:\Python27\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 4, in block "doc"{%- block html %}File "C:\Python27\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html",line 20, in block "html"{% block body -%}File "C:\Python27\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html",line 21, in block "body"{% block navbar %}File "D:\python\flask4\templates\base.html", line 8, in block "navbar"{{ nav.top.render() }}File "C:\Python27\lib\site-packages\flask_nav\elements.py", line 24, in renderself))File "C:\Python27\lib\site-packages\visitor\__init__.py", line 48, in visitreturn meth(node)File "C:\Python27\lib\site-packages\flask_bootstrap\nav.py", line 53, in visit_Navbarbar_list.add(self.visit(item))File "C:\Python27\lib\site-packages\visitor\__init__.py", line 48, in visitreturn meth(node)File "C:\Python27\lib\site-packages\flask_bootstrap\nav.py", line 98, in visit_Viewitem.add(tags.a(node.text, href=node.get_url(), title=node.text))File "C:\Python27\lib\site-packages\flask_nav\elements.py", line 77, in get_urlreturn url_for(self.endpoint, **self.url_for_kwargs)File "C:\Python27\lib\site-packages\flask\helpers.py", line 333, in url_forreturn appctx.app.handle_url_build_error(error, endpoint, values)File "C:\Python27\lib\site-packages\flask\app.py", line 1805, in handle_url_build_errorreraise(exc_type, exc_value, tb)File "C:\Python27\lib\site-packages\flask\helpers.py", line 323, in url_forforce_external=external)File "C:\Python27\lib\site-packages\werkzeug\routing.py", line 1768, in buildraise BuildError(endpoint, values, method, self)
BuildError: Could not build url for endpoint 'index'. Did you mean 'swe2' instead?
找了很久 原来问题出现在

index,about,Services,projects都是要跳转的指向因为找不到这些指向
由于我整理分类不小心删除了 忘记加跳转路由导致的错误 一定要注意 一天的教训!!!!
加上这些就可以了 上面的index,about,Services,projects对应跳转路由的函数名称~~!!而不是跳转的地址
@app.route('/Services')defServices():return 'Service'@app.route('/about')defabout():return 'about'@app.route('/Projects')def projects():return 'The projects page'@app.route('/Home')defindex():return 'index'



原创粉丝点击