由于初学者 刚学 有些知识没学到位
花了一天时间找错误 才发现是路径跳转的问题 汗!!
项目结构:
子结构及代码:
# -*- coding:utf-8 -*-
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from flask_nav import Nav
from 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_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "D:/python/flask4/flask4.py", line 25, in swe2
return render_template('jingjia.html',title='welcome')
File "C:\Python27\lib\site-packages\flask\templating.py", line 134, in render_template
context, ctx.app)
File "C:\Python27\lib\site-packages\flask\templating.py", line 116, in _render
rv = template.render(context)
File "C:\Python27\lib\site-packages\jinja2\environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "C:\Python27\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
reraise(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 render
self))
File "C:\Python27\lib\site-packages\visitor\__init__.py", line 48, in visit
return meth(node)
File "C:\Python27\lib\site-packages\flask_bootstrap\nav.py", line 53, in visit_Navbar
bar_list.add(self.visit(item))
File "C:\Python27\lib\site-packages\visitor\__init__.py", line 48, in visit
return meth(node)
File "C:\Python27\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 "C:\Python27\lib\site-packages\flask_nav\elements.py", line 77, in get_url
return url_for(self.endpoint, **self.url_for_kwargs)
File "C:\Python27\lib\site-packages\flask\helpers.py", line 333, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "C:\Python27\lib\site-packages\flask\app.py", line 1805, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\helpers.py", line 323, in url_for
force_external=external)
File "C:\Python27\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 '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'
初学者在使用Python NAV框架时遇到一天的困扰,最终发现问题是由于路径跳转不正确导致。通过调整和学习,成功解决问题。
3309

被折叠的 条评论
为什么被折叠?



