By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,247 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,247 IT Pros & Developers. It's quick & easy.

question about endpoint in flask

P: 3
hi there,when learning flask,i have confronted a question,which is "BuildError: Could not build url for endpoint 'login'. Did you mean 'index' instead?",my code is below.
Expand|Select|Wrap|Line Numbers
  1. __author__ = 'Administrator'
  2. from app import app,db,lm,oid
  3. from flask import render_template,flash,redirect,session,url_for,request,g
  4. from flask.ext.login import login_user,logout_user,current_user,login_required
  5. from models import User
  6. from form import LoginForm#,EditForm
  7. from datetime import datetime
  8.  
  9. @app.route('/')
  10. @app.route('/index')
  11. @login_required
  12. def index():
  13.     user = g.user
  14.     #user = { 'nickname': 'Miguel' } # fake user
  15.     posts = [ # fake array of posts
  16.         {
  17.             'author': { 'nickname': 'John' },
  18.             'body': 'Beautiful day in Portland!'
  19.         },
  20.         {
  21.             'author': { 'nickname': 'Susan' },
  22.             'body': 'The Avengers movie was so cool!'
  23.         }
  24.     ]
  25.     return render_template("index.html",
  26.         title = 'Home',
  27.         user = user,
  28.         posts = posts)
  29. @lm.user_loader
  30. def load_user(id):
  31.     return User.query.get(int(id))
  32. @app.route('/login',methods=['GET','POST'])
  33. @app.before_request
  34. def before_request():
  35.     g.user = current_user
  36.  
  37. def login():
  38.     if g.user is not None and g.user.is_authenticated:
  39.         return redirect(url_for('index'))
  40.     form = LoginForm()
  41.     print form.openid.data
  42.     if form.validate_on_submit():
  43.         session['remember_me'] = form.remember_me.data
  44.         a= User.query.get(2).nickname
  45.         print 'dataall   '+str(a)
  46.         user = User.query.filter_by(nickname=form.openid.data).first()
  47.         print 'query result  ' + str(user)
  48.                 if user :
  49.            print 'user start'
  50.                       login_user(user)
  51.            return redirect( url_for('index'))
  52.            print 'user done'
  53.     return render_template('login.html',title = 'Sign In',form = form,providers = app.config['OPENID_PROVIDERS'])
  54.  
  55.  
  56. @app.route('/user/<nickname>')
  57. @login_required
  58. def user(nickname):
  59.     user = User.query.filter_by(nickname = nickname).first()
  60.     if user == None:
  61.         flash('User' + nickname +"not found.")
  62.         return redirect(url_for('index'))
  63.     posts = [
  64.         {'author':'user','body':'Test post #1'},
  65.         {'author':'user','body':'Test post #2'}
  66.     ]
  67.     return render_template('user.html',user=user,posts =posts)
html file is below
Expand|Select|Wrap|Line Numbers
  1. <html> <head>
  2.     {% if title %}
  3.     <title>{{title}} - microblog</title>
  4.     {% else %}
  5.     <title>microblog</title>
  6.     {% endif %}
  7.   </head> <body> <div>Microblog:
  8.      <a href="{{ url_for('index') }}">Home</a> <a href="{{ url_for('user', nickname = g.user.nickname) }}">Your Profile</a> <a href="{{ url_for('logout') }}">Logout</a> </div> <hr>
  9.     {% with messages = get_flashed_messages() %}
  10.     {% if messages %}
  11.     <ul>
  12.     {% for messages in messages %}
  13.         <li>{{ message }}</li>
  14.     {% endfor %}
  15.     </ul>
  16.     {% endif %}
  17.     {% endwith %}
  18.     {% block content %}{% endblock %}
  19.   </body> </html>
3 Weeks Ago #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.