【问题标题】:I Want to access the variable of a if statement in the function .Python我想在函数 .Python 中访问 if 语句的变量
【发布时间】:2021-03-10 01:49:50
【问题描述】:
def Home(request):#django

    if request.method == 'POST':
        city = request.POST.get('search')
        print(city)
        
    URL = f'http://api.openweathermap.org/data/2.5/weather?q={city}

【问题讨论】:

  • 如果request.method 不是POST,city 应该有什么值?

标签: python django


【解决方案1】:

在 if 之前设置你的变量:

def Home(request):#django
    city = ''
    if request.method == 'POST':
        city = request.POST.get('search')
        print(city)
        
    URL = f'http://api.openweathermap.org/data/2.5/weather?q={city}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-19
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多