How to avoid writing request.GET.get() twice in order to print it?

前端 未结 10 792
名媛妹妹
名媛妹妹 2020-12-04 21:34

I come from a PHP background and would like to know if there\'s a way to do this in Python.

In PHP you can kill 2 birds with one stone like this:

Instead of

10条回答
  •  余生分开走
    2020-12-04 22:21

    If get() throws an exception when it's not there, you could do

    try:
       q = request.GET.get('q')
       print q
    except :
       pass
    

提交回复
热议问题