def login(): # Here we use a class of some kind to represent and validate our # client-side form data. For example, WTForms is a library that will # handle this for us, and we use a custom LoginForm to validate. form = LoginForm() if form.validate_on_submit(): # Login and validate the user. # user should be an instance of your `User` class login_user(user) Flask.flash('Logged in successfully.') next = Flask.request.args.get('next') # next_is_valid should check if the user has valid # permission to access the `next` url if not next_is_valid(next): return Flask.abort(400) return Flask.redirect(next or Flask.url_for('index')) return Flask.render_template('login.html', form=form)
def login(): # Here we use a class of some kind to represent and validate our # client-side form data. For example, WTForms is a library that will # handle this for us, and we use a custom LoginForm to validate. form = LoginForm() if form.validate_on_submit(): # Login and validate the user. # user should be an instance of your `User` class login_user(user) Flask.flash('Logged in successfully.') next = Flask.request.args.get('next') # next_is_valid should check if the user has valid # permission to access the `next` url if not next_is_valid(next): return Flask.abort(400) return Flask.redirect(next or flask.url_for('index')) return Flask.render_template('login.html', form=form)
from flask import Flask, render_template, request app = Flask(__name__) @app.route('/') def student(): return render_template('student.html') @app.route('/result',methods = ['POST', 'GET']) def result(): if request.method == 'POST': result = request.form return render_template("result.html",result = result) if __name__ == '__main__': app.run(debug = True) Flask.redirect(location, statuscode, response)
def success(name): return Flask.redirect("test/cont.html")
class CatFactsREST(object): def __init__(self, config): self.config = config self.apikeys = [s.strip() for s in self.config['apikeys'].split(',')] dburi = self.config['dburi'] self.db = Shove(dburi) self.app = Flask(__name__) self.api = TwilioRestClient( self.config['SID'], self.config['token']) if 'numbers' not in self.db: self.db['numbers'] = [] if 'facts' not in self.db: print "No catfacts found, run catfacts load" exit() self.db.sync() self.routes = { "/api/numbers": (self.add_number, {"methods": ['POST']}), "/api/numbers/<num>": (self.remove_number, {"methods": ['DELETE']}), "/api/callback": (self.twilio_callback, {"methods": ['GET']}), "/api/facts": (self.add_facts, {"methods": ['POST']}), "/": (self.view_home, {"methods": ['GET']}), "/subscribe": (self.subscribe, {"methods": ['POST']}), "/submit": (self.submit, {"methods": ['POST']}), } map( lambda route: self.app.route(route, **self.routes[route][1])(self.routes[route][0]), self.routes) def view_home(self): """ View the CatFacts homepage, where you can submit CatFacts! """ return render_template('index.html') def subscribe(self): """ Add a phone number to the CatFacts database. """ number = request.values['number'] data = json.dumps(dict( number=number, apikey='submitkey', )) payload = dict(json=data) urllib2.urlopen('http://localhost:{0}/api/numbers'.format( self.config['port'], data=urllib.urlencode(payload))) self.app.redirect('/') # TODO: Add success message def submit(self): """ Submit a cat fact to the CatFacts database. """ fact = request.values['fact'] data = json.dumps(dict( fact=fact, apikey='submitkey', )) payload = dict(json=data) urllib2.urlopen('http://localhost:{0}/api/facts'.format( self.config['port'], data=urllib.urlencode(payload))) self.app.redirect('/') # TODO: Add success message def add_number(self): """ POST: /api/numbers """ print "Adding numbers" try: j = request.values['json'] data = json.loads(j) except Exception as e: return json.dumps(dict( success=False, message="Invalid data recieved")) try: if data['apikey'] not in self.apikeys: raise Exception except: return json.dumps(dict( succes=False, message="Unauthorized")) try: number = data['number'] if number not in self.db['numbers']: self.db['numbers'].append(number) self.db.sync() self.api.sms.messages.create( to=number, from_="2037947419", body="Congrats, you have been signed up for catfacts, \ the Premire cat information service, you will \ receive hourly cat information") return json.dumps(dict( success=True, message="Added {0} to catfacts".format(number))) else: return json.dumps(dict( success=False, message="{0} is already signed up for catfacts".format( number))) except KeyError: return json.dumps(dict( success=False, message="Not Enough paramters")) def remove_number(self, num): """ DELETE: /api/numbers/<number> """ if num in self.db: self.db['numbers'].remove(num) self.db.sync() return json.dumps(dict( success=True, message="Removed {0} from catfacts".format(num))) else: return json.dumps(dict( success=False, message="{0} is not signed up for catfacts".format(num))) def twilio_callback(self): """ POST: /api/callback """ response = twilio.twiml.Response() response.sms(choice(self.db['facts'])) return str(response) def add_facts(self): """ POST: /api/facts """ try: data = json.loads(request.values['json']) except: return json.dumps(dict( success=False, message="Invalid data recieved")) try: if data['apikey'] not in self.apikeys: raise Exception except: return json.dumps(dict( succes=False, message="Unauthorized")) try: self.db['facts'].extend(data['facts']) self.db.sync() return json.dumps(dict( success=True, message='Added more cat facts')) except KeyError: return json.dumps(dict( success=False, message="not enough parameters")) def start(self): self.app.run( host=self.config['host'], port=self.config['port'], debug=True)
return redirect("/edit/<user_id>") @app.route("/delete/<bright_idea_id>") def delete_bright_idea(bright_idea_id): mysql = connectToMySQL(database) query = "DELETE FROM bright_ideas WHERE bright_ideas.id = %(bright_idea_id)s" data = {'bright_idea_id': bright_idea_id} mysql.query_db(query, data) return redirect("/landing") if __name__ == "__main__": app.run(debug=True) '''<<<<< Flask.redirect(location, statuscode, response) "location" = URL where response should be directed. "statuscode" = statuscode sent to browser's header. "response" = response parametwer used to instantiate response. "Statuscodes" STATUS CODES HTTP_300_MULTIPLR_CHOICES HTTP_301_MOVED_PERMANENTLY HTTP_302_FOUND
def success(name): return Flask.redirect("test/pinsert.html")