Advertisement
Guest User

Untitled

a guest
Apr 14th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. from flask import *
  2. from functools import wraps
  3. app = Flask(__name__)
  4. app.secret_key = "my precious"
  5.  
  6. @app.route('/')
  7. def home():
  8. return render_template('home.html')
  9.  
  10. @app.route('/welcome')
  11. def welcome():
  12. return render_template('welcome.html')
  13.  
  14. @app.route('/logout')
  15. def logout():
  16. session.pop('logged_in',None)
  17. return redirect (url_for('home'))
  18.  
  19. @app.route('/hello')
  20. def hello():
  21. return render_template('hello.html')
  22.  
  23.  
  24. @app.route('/log', methods=['GET','POST'])
  25. def log():
  26. error = None
  27. if request.method == "POST":
  28. if request.form['username'] != 'admin' or request.form['password'] != 'admin':
  29. error = "Invalid credentials"
  30. else:
  31. session['logged_in'] = True
  32. return redirect (url_for('hello'))
  33. return render_template('log.html', error=error)
  34.  
  35.  
  36. if __name__ == '__main__':
  37. app.run(debug=True)
  38.  
  39. {% extends "templates.html" %}
  40. {% block content %}
  41. <h1>Login</h1>
  42. {% If error %}
  43. <p class=error> <strong> Error: </strong> {{ error }}
  44. {% endif %}
  45. <form action="" method="POST">
  46. <dl>
  47. <dt>Username:
  48. <dt><input type="text" name="username" value="{{
  49. request.form.username }}">
  50. <dt>Password:
  51. <dd><input type="password" name="passowrd">
  52. </dl>
  53. <p><input type="submit" value="Login">
  54. </form>
  55. {% endblock %}
  56.  
  57. <html>
  58. <head>
  59. <title>Flask tutorial (Part 1)</title>
  60. </head>
  61. <header>
  62. <div class="navbar navbar-inverse">
  63. <div class="navbar-inner">
  64. <a class="brand" href="/">Real python (for the web!)</a>
  65. <ul>
  66. <li><a href="/welcome">Welcome</a></li>
  67. <li><a href="/log">login</a></li>
  68. </ul>
  69. </div>
  70. </div>
  71. </header>
  72. <body>
  73. <div class="container">
  74. {% block content %}
  75. {% endblock %}
  76. </div>
  77. </body>
  78. </html>
  79.  
  80. {% extends "templates.html"%}
  81. {% block content %}
  82. <div class="jumbo">
  83. <h2>Welcome to Flask</h2>
  84. <br/>
  85. <p>click <a href="/welcome">here</a> to go to welcome page</p>
  86. </div>
  87. {% endblock %}
  88.  
  89. {% extends "templates.html" %}
  90. {% block content %}
  91. <h2>Welcome! You are logged in.</h2>
  92. {% endblock %}
  93.  
  94. {% extends "templates.html"%}
  95. {% block content %}
  96. <h2>Sample</h2>
  97. <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
  98. <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
  99. {% endblock %}
  100.  
  101. @app.route('/log', methods=['GET','POST'])
  102. def log():
  103. error = None
  104. if request.method == "POST":
  105. if request.form['username'] != 'admin' or request.form['password'] != 'admin':
  106. error = "Invalid credentials"
  107. else:
  108. session['logged_in'] = True
  109. return redirect (url_for('hello'))
  110. return render_template('log.html', error=error)
  111.  
  112. @app.route('/welcome')
  113. def welcome():
  114. return render_template("welcome.html")
  115.  
  116. @app.route('/welcome')
  117. def welcome():
  118. if request.method == "POST":
  119. return render_template("welcome.html")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement