Guest User

Untitled

a guest
Oct 4th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3. <body>
  4.  
  5. <h1>Login</h1>
  6.  
  7. {% if error %}
  8. <p><strong>Error:</strong> {{ error }}
  9. {% endif %}
  10.  
  11. <form action = "" method = post>
  12. <dl>
  13. <dt>Username:</dt>
  14.  
  15. <dd>
  16. <input type = text name = username
  17. value = "{{request.form.username }}">
  18. </dd>
  19.  
  20. <dt>Password:</dt>
  21. <dd><input type = password name = password></dd>
  22. </dl>
  23. <p><input type = submit value = Login></p>
  24. </form>
  25.  
  26. </body>
  27. </html>
  28.  
  29. <!doctype html>
  30. <html>
  31.  
  32. <head>
  33. <title>Flask Message flashing</title>
  34. </head>
  35. <body>
  36.  
  37. {% with messages = get_flashed_messages() %}
  38. {% if messages %}
  39. <ul>
  40. {% for message in messages %}
  41. <li<{{ message }}</li>
  42. {% endfor %}
  43. </ul>
  44. {% endif %}
  45. {% endwith %}
  46.  
  47. <h1>Flask Message Flashing Example</h1>
  48. <p>Do you want to <a href = "{{ url_for('login') }}">
  49. <b>log in?</b></a></p>
  50.  
  51. </body>
  52. </html>
  53.  
  54. from flask import Flask, flash, redirect, render_template, request, url_for
  55. app = Flask(__name__)
  56. app.secret_key = 'random string'
  57.  
  58. @app.route('/')
  59. def index():
  60. return render_template('index.html')
  61.  
  62. @app.route('/login', methods = ['GET', 'POST'])
  63. def login():
  64. error = None
  65.  
  66. if request.method == 'POST':
  67. if request.form['username'] != 'admin' or
  68. request.form['password'] != 'admin':
  69. error = 'Invalid username or password. Please try again!'
  70. else:
  71. flash('You were successfully logged in')
  72. return redirect(url_for('index'))
  73.  
  74. return render_template('login.html', error = error)
  75.  
  76. if __name__ == "__main__":
  77. app.run(debug = True)
Add Comment
Please, Sign In to add comment