2013-09-21 17 views
0

我是新烧瓶(使用它与nginx),我想了解URL逻辑。我有2个python脚本.... /site/myapp.py和/site/bar.py。2个不同脚本和不同视图的Python烧瓶URL逻辑

我有三个问题,请:

  1. 如果,我想只运行myapp.py而不是/site/bar.py如何添加 一个URL规则与add_url_rule运行呢?
  2. 如果我想运行 /site/bar.py,我该怎么做?
  3. 如果我想跑myapp.py,并有两个 不同意见......根据xml.open("POST", "/site/myapp/view1", true)xml.open("POST", "/site/myapp/view2",真正的)......我将如何分配在每个 视图的URL中myapp.py与add_url_rule ?

python脚本/site/myapp.py:

[email protected]:/site# cat myapp.py 
import flask, flask.views 
app = flask.Flask(__name__) 

class View1(flask.views.MethodView): 
    def post(self): 
    pass 

app.add_url_rule('/site/myapp', view_func=View1.as_view('view1')) 

[email protected]:/site# 

JavaScript函数:

function foo() { 
     var xml = new XMLHttpRequest(); 
     xml.open("POST", "/site/myapp", true); 
     xml.send(form); 
     console.log("sent") 
     xml.onreadystatechange = function() { 
      console.log(xml.readyState); 
      console.log(xml.status); 
      if (xml.readyState == "4" && xml.status == "200"){ 
       console.log("yes"); 
       console.log(xml.responseText); 
      } 
     } 
    } 

nginx的配置:

server { 
    listen 10.33.113.55; 

    access_log /var/log/nginx/localhost.access_log main; 
    error_log /var/log/nginx/localhost.error_log info; 

location/{ 
root /var/www/dude; 
} 

location /site/ { 
     try_files $uri @uwsgi; 
} 

location @uwsgi { 
      include uwsgi_params; 
      uwsgi_pass 127.0.0.1:3031; 
    } 

} 
+2

Flask没有类似CGI或PHP的路由系统。 URL方案可能看起来与文件系统结构完全不同。请从[快速入门](http://flask.pocoo.org/docs/quickstart/)或[教程](http://flask.pocoo.org/docs/tutorial/)开始, –

回答

2

flask tutorial,你可以找到的东西如:

@app.route('/') 
def show_entries(): 
    cur = g.db.execute('select title, text from entries order by id desc') 
    entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()] 
    return render_template('show_entries.html', entries=entries) 

这意味着,任何人http://yourdomain.tld/访问您的网站将执行show_entries功能和render_template('show_entries.html', entries=entries)返回值将被发送给用户。

this page,你也可以发现:

@app.route('/') 
def index(): 
    pass 

相当于

def index(): 
    pass 
app.add_url_rule('/', 'index', index) 

你需要忘记你的PHP背景,想以另一种方式。人们不会使用类似http://yourdomain.com/index.py的网址访问您的网站。基本上,你告诉你的服务器烧瓶负责处理URL,并将URL映射到函数。就那么简单。