2017-10-20 40 views
0

當我嘗試在熱保護程序包的頂部使用管理腳本時出現錯誤。我真的很想知道如何解決此錯誤。我想有錯誤某處,我無法弄清楚。我使用Flask 0.12。 我正在學習如何建立簡單的使用Flask.The目錄樹複雜的應用程序:AssertionError:視圖函數映射正在覆蓋現有的端點函數:索引

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 

當運行蟒蛇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='[email protected]')) 
    db.session.add(User(username='ayobami',email='[email protected]')) 
    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) 
+0

我搞亂了代碼。 – rarevessell

回答

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='[email protected]')) 
    db.session.add(User(username='ayobami',email='[email protected]')) 
    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()