如何获取烧瓶中get请求的参数值?

时间:2014-03-13 15:31:24

标签: python web flask get-request

我在网络上找到的答案是使用request.args.get。但是,我无法管理它。我有以下简单的例子:

from flask import Flask
app = Flask(__name__)

@app.route("/hello")
def hello():
    print request.args['x']
    return "Hello World!"

if __name__ == "__main__":
    app.run()

我在浏览器中转到127.0.0.1:5000/hello?x=2,结果我得到了:

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

我做错了什么?

2 个答案:

答案 0 :(得分:74)

简单的答案是你没有从flask包中导入request全局对象。

from flask import Flask, request

通过执行

,可以通过在调试模式下运行开发服务器来轻松确定自己
app.run(debug=True)

这将为您提供一个包含以下内容的堆栈跟踪:

print request.args['x']
NameError: global name 'request' is not defined

答案 1 :(得分:6)

java.util.hashmap ?n = 5

类似于之前的情况

public class AddressControl extends AddressControlTask {

private View anchorView;

// You must send a context
public static void run(Context context, View anchorView, String zipCode, String houseNumber) {
    new AddressControl(context, anchorView).execute(zipCode, houseNumber);
}

private AddressControl(Context context, View anchorView) {
    super(context);
    this.anchorView = anchorView;
}

@Override
public Class<? extends AbstractTask> getTaskClass() {
    return getClass();
}

@Override
protected void onPostExecute(final List<Address> result) {
    if (result.size() == 0) {
        result.add(null);


        destroyDialog();

        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View view = inflater.inflate( R.layout.control_address_fragment, null );

        ImageView okaddress = (ImageView)view.findViewById(R.id.address_ok);

        okaddress.setImageResource(R.drawable.clear);



        final CustomAlertDialog alertDialog = new CustomAlertDialog(getContext());
        alertDialog.setPositiveButton("Ok", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });
        alertDialog.setTitle("Wrong");

        alertDialog.setText("Address is not correct");
        alertDialog.show();
        super.onPostExecute(result);
    } if(result.size() == 1) {
        super.onPostExecute(result);
    } else {
        destroyDialog();
        Context context = getContext();
        CustomSelectDialog dialog = new CustomSelectDialog(context, anchorView);
        dialog.setOnItemSelectListener(new CustomSelectDialog.OnItemSelectedListener() {
            @Override
            public void onItemSelected(String text, Object value) {
                List<Address> addressList = new ArrayList<>();
                addressList.add((Address) value);
                onPostExecute(addressList);
            }
        });
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialogInterface) {
                onPostExecute(new ArrayList<Address>());
            }
        });
        for(Address address : result) {
            dialog.addItem(StringUtils.getAddressText(address), address);
        }
        dialog.setTitle("Enter true address, please");
        dialog.show();
    }
}
}

可以做到这一点