473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

question about endpoint in flask

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>
Jun 27 '19 #1
0 1323

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

Similar topics

0
by: Richard L Rosenheim | last post by:
I'm playing around with setting up a multicast socket. I can create a socket for sending, and I can one that receives. Is it possible to create one socket that can do both? The code I've been...
4
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I...
0
by: ronscottlangham | last post by:
I have a WCF Web Service that I develop using the ASP.NET Development Server in Visual Studio. In release, the web service will support both HTTP and HTTPS. Initially I had only HTTP configured in...
0
by: rkprasad | last post by:
I am able to create BASIC-CLEAR-INTEGRATED sql http endpoint and consume it on a LAN. But i am not able to consume the created endpoint on inter domain network. If i create endpoint on a server...
3
by: Lance Wynn | last post by:
Hello, I am receiving this error when trying to instantiate a webservice component. I have 2 development machines, both are XP sp2 with VS 2008 installed. On one machine, the code works fine. On...
8
by: Frank Hauptlorenz | last post by:
Hello out there, I changed an existing and good working webservice from an wsHttpBinding to an NetTcpBinding. This is working (after trying some time) and has real a better performance! But...
0
by: keelo | last post by:
I have a small c++ .net program that is intended to be a simple UDP server; however, I just want to start blasting out packets when a connection is made. After setup, Socket^ socket = gcnew...
0
maylortaylor
by: maylortaylor | last post by:
I have a pretty simple TimeClock application written in VB (.net4) that I'm trying to add to. The addition will allow the user to change where the service is coming from. In short, I want to be...
1
by: vento | last post by:
I have struggled to get this code to work for some days now. This should be just simple web form where text is added to 2 fields and posted to the Sqlite-database. I am able to display data from...
0
by: nishank05 | last post by:
I want to integration CC Avenue payment gateway with Flask. Is there a ready made library for that or is there a way I can do it. Seems CC Avenue officially supports ASP, ASP.net JSP and PHP only. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.