2017-10-20 39 views
0

L'erreur se produit lorsque j'essaie d'utiliser le script de gestion en haut du package thermos. Je veux vraiment savoir comment résoudre cette erreur. Je suppose qu'il y a une erreur quelque part et je ne peux pas comprendre cela. J'utilise Flask 0.12. J'apprends comment construire des applications simples à complexes en utilisant arborescence Flask.The:AssertionError: le mappage de fonction View remplace une fonction de point de terminaison existante: index

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 

Lors de l'exécution python manage.py runserver cette erreur a été lancée:

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 

C'est le 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() 

et .py initialisation:

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 

C'est le 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

Je suis le code mélangé. – rarevessell

Répondre

0

L'erreur arrêté après, je corrige quelques erreurs de typo.

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 

le 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() 

et 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()