AssertionError:视图函数映射覆盖现有端点函数:index

时间:2017-10-20 10:11:01

标签: python flask

当我尝试在热水瓶包的顶部使用管理脚本时出现的错误。我真的想知道如何解决这个错误。我想在某个地方有错误,我无法解决这个问题。我和#39; m使用Flask 0.12。 我正在学习如何使用Flask构建简单到复杂的应用程序。目录树:

README.rst manage.py thermos

./thermos:
__init__.py forms.py models.py static thermos.db views.py
__init__.pyc forms.pyc models.pyc templates thermos.pyc views.pyc

./thermos/static:
css img js

./thermos/static/css:
main.css normalize.css normalize.min.css

./thermos/static/img:

./thermos/static/js:
main.js vendor

./thermos/static/js/vendor:
jquery-1.11.2.min.js modernizr-2.8.3-respond-1.4.2.min.js

./thermos/templates:
404.html add.html base.html form_macros.html index.html

运行python manage.py runserver时抛出了这个错误:

Traceback (most recent call last):
  File "manage.py", line 4, in <module>
    from thermos import app,db
  File "/Users/tunji/dev/thermos/thermos/__init__.py", line 16, in <module>
    import views
  File "/Users/tunji/dev/thermos/thermos/views.py", line 10, in <module>
    @app.route('/index')
  File "/Users/tunji/.envs/thermos/lib/python2.7/site-packages/flask/app.py", line 1080, in decorator
    self.add_url_rule(rule, endpoint, f, **options)
  File "/Users/tunji/.envs/thermos/lib/python2.7/site-packages/flask/app.py", line 64, in wrapper_func
    return f(self, *args, **kwargs)
  File "/Users/tunji/.envs/thermos/lib/python2.7/site-packages/flask/app.py", line 1051, in add_url_rule
    'existing endpoint function: %s' % endpoint)
AssertionError: View function mapping is overwriting an existing endpoint function: index

这是manage.py:

#! /usr/bin/env python

from flask_script import Manager,prompt_bool
from thermos import app,db

import thermos.models 


manager = Manager(app)

@manager.command
def initdb():
    db.create_all()
    db.session.add(User(username='olatunji',email='ayo19602003@yahoo.com'))
    db.session.add(User(username='ayobami',email='bambamaks37@gmail.com'))
    db.session.commit()
    print "Initialized the database"

@manager.command
def dropdb():
    if prompt_bool(
        "Are you sure you want to lose all your data"):
        db.drop_all()
        print 'Dropped the database'

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

init .py:

import os

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

basedir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI']= 'sqlite:///' + os.path.join(basedir,'thermos.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['DEBUG'] = True
db = SQLAlchemy(app)

import models
import views

这是views.py:

from flask import render_template,flash,redirect,url_for

from thermos import app,db
from forms import BookmarkForm
from models import User,Bookmark

def logged_in_user():
    return User.query.filter_by(username='olatunji').first()

@app.route('/index')
def index():
    return render_template('index.html',new_bookmarks=Bookmark.newest(5))

@app.route('/add',methods=['GET','POST'])
def add():
    form = BookmarkForm()
    if form.validate_on_submit():
        url = form.url.data
        description =form.description.data
        bm = Bookmark(user=logged_in_user(),url=url,description=description)
        db.session.add(bm)
        db.session.commit()
        flash("Stored '{}'".format(description))
        return redirect(url_for('index'))
    return render_template('add.html',form=form)

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'),404

@app.errorhandler(500)
def server_error(e):
    return render_template('500.html'),500


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

1 个答案:

答案 0 :(得分:0)

错误停止后,我纠正了一些拼写错误。

The __init__.py

import os

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

basedir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)
app.config['SECRET_KEY'] = '\x9a\xf1\x9d\xc7\xe9t\x0f\x1a\xcb\xe1\xba\xc0\x1dK\xf6\xfb:\x88Y\xedEH\\S'
app.config['SQLALCHEMY_DATABASE_URI']= 'sqlite:///' + os.path.join(basedir,'thermos.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['DEBUG'] = True
db = SQLAlchemy(app)

import models
import views

manage.py:

#! /usr/bin/env python

from thermos import app,db
from thermos.models import User
from flask_script import Manager,prompt_bool

manager = Manager(app)

@manager.command
def initdb():
    db.create_all()
    db.session.add(User(username='olatunji',email='ayo19602003@yahoo.com'))
    db.session.add(User(username='ayobami',email='bambamaks37@gmail.com'))
    db.session.commit()
    print "Initialized the database"

@manager.command
def dropdb():
    if prompt_bool(
        "Are you sure you want to lose all your data"):
        db.drop_all()
        print 'Dropped the database'

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

和views.py:

from flask import render_template,flash,redirect,url_for

from thermos import app,db
from forms import BookmarkForm
from models import User,Bookmark

def logged_in_user():
    return User.query.filter_by(username='olatunji').first()


@app.route('/index')
def index():
    return render_template('index.html',new_bookmarks=Bookmark.newest(5))

@app.route("/add",methods=['GET','POST'])
def add():
    form = BookmarkForm()
    if form.validate_on_submit():
        url = form.url.data
        description =form.description.data
        bm = Bookmark(user=logged_in_user(),url=url,description=description)
        db.session.add(bm)
        db.session.commit()
        flash("Stored '{}'".format(description))
        return redirect(url_for('index'))
    return render_template("add.html",form=form)

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'),404

@app.errorhandler(500)
def server_error(e):
    return render_template('500.html'),500


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