4 Commits 91510cab35 ... 6b97582ff1

Author SHA1 Message Date
  felix 6b97582ff1 frog-3.5 3 years ago
  felix f87ea3577e frog-3.4 3 years ago
  felix 5b5428c34d frog-3.3 3 years ago
  felix 00dd296ffd frog-3.2 4 years ago

+ 38 - 8
frog.py

@@ -1,14 +1,44 @@
-from flask import Flask
-from flask import request
-from flask import make_response
-from flask import redirect
-from flask import render_template
+from flask import Flask,request,make_response,redirect,render_template,url_for,abort,flash
 app=Flask(__name__)
+app.config['SECRET_KEY']='132456'
+
+@app.errorhandler(404)
+def page_not_found(e):
+    return render_template('404.html'),404
+
+@app.errorhandler(500)
+def internal_server_error(e):
+    return render_template('500.html'),500
 
 @app.route('/')
 def index():
     return render_template('index.html')
 
-@app.route('/hello/<user>')
-def hello(user):
-    return render_template('hello.html',user=user)
+@app.route('/post/')
+def posts():
+    allposts=[{'id':1,'title':'title1'},{'id':2,'title':'title2'}]
+    return render_template('posts.html',posts=allposts)
+
+@app.route('/post/<int:id>')
+def post(id):
+    return render_template('post.html',id=id)
+
+@app.route('/article/')
+def article():
+    return redirect(url_for('posts'))
+
+from flask import abort
+
+@app.route('/i-love/<obj>')
+def ilove(obj):
+    if obj=='shit':
+        abort(404)
+    return '<h1>I love %s</h1>' % obj
+
+@app.route('/<int:num1>/plus/<int:num2>/eq/<int:num3>')
+def count(num1,num2,num3):
+    if num1+num2==num3:
+        flash('计算正确')
+    else:
+        flash('计算错误')
+    return redirect(url_for('index'))

+ 0 - 0
static/.gitignore


+ 3 - 0
static/myfunction.js

@@ -0,0 +1,3 @@
+function abc(){
+    console('hello abc');
+}

BIN
static/pictures/a.png


+ 1 - 0
static/style.css

@@ -0,0 +1 @@
+.classname{color:black;}

+ 7 - 0
templates/404.html

@@ -0,0 +1,7 @@
+{% extends 'base.html' %}
+
+{% block title %}404 not found{% endblock %}
+
+{% block content %}
+<h1>404 page not found</h1>
+{% endblock %}

+ 7 - 0
templates/500.html

@@ -0,0 +1,7 @@
+{% extends 'base.html' %}
+
+{% block title %}500 internal server error{% endblock %}
+
+{% block content %}
+<h1>500 internal server error</h1>
+{% endblock %}

+ 4 - 0
templates/base.html

@@ -4,10 +4,14 @@
         <link rel="stylesheet" href="//cdnjs.loli.net/ajax/libs/mdui/0.4.3/css/mdui.min.css">
         <script src="//cdnjs.loli.net/ajax/libs/mdui/0.4.3/js/mdui.min.js"> var SS=mdui.JQ;</script>
         <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
+        <link rel='stylesheet' href="{{ url_for('static',filename='style.css') }}">
+        <script src="{{ url_for('static',filename='myfunction.js') }}"></script>
     </head>
     <body class='mdui-theme-primary-light-green'>
         <div class="mdui-toolbar mdui-color-theme">
             <a href='/' class='mdui-typo-headline mdui-text-color-white' style='font-weight:bold'>Frog</a>
+            <a href='/' class='mdui-text-color-white'>首页</a>
+            <a href="{{ url_for('posts') }}" class='mdui-text-color-white'>文章</a>
         </div>
         <div class='mdui-container'>
             {% block content %}{% endblock %}

+ 0 - 5
templates/hello.html

@@ -1,5 +0,0 @@
-{% extends 'base.html' %}
-
-{% block title %}{{ user }}-Frog{% endblock %}
-
-{% block content %}<h1>hello {{ user }}!</h1>{% endblock %}

+ 12 - 1
templates/index.html

@@ -2,4 +2,15 @@
 
 {% block title %}Frog{% endblock %}
 
-{% block content %}<h1>hello world!</h1>{% endblock %}
+{% block content %}
+
+    {% with messages=get_flashed_messages() %}
+            {% if messages %}
+                {% for message in messages %}
+                <div style='color:green;background-color:lightgreen;padding:10px;'>{{ message }}</div>
+                {% endfor %}
+        {% endif %}
+    {% endwith %}
+    
+<h1>hello world!</h1>
+{% endblock %}

+ 11 - 0
templates/post.html

@@ -0,0 +1,11 @@
+{% extends 'base.html' %}
+
+{% block title %}文章-{{ id }}{% endblock %}
+
+{% block content %}
+
+
+<h1>文章-{{ id }}</h1>
+<hr>
+<p>这是文章-{{ id }}的内容</p>
+{% endblock %}

+ 14 - 0
templates/posts.html

@@ -0,0 +1,14 @@
+{% extends 'base.html' %}
+
+{% block title %}文章列表{% endblock %}
+
+{% block content %}
+
+
+<h1>文章列表</h1>
+<div class='mdui-list'>
+{% for post in posts %}
+<a href="{{ url_for('post',id=post['id']) }}" class="mdui-list-item mdui-ripple">{{ post['title'] }}</a>
+{% endfor %}
+</div>
+{% endblock %}