/'start_date'

时间:2019-09-16 15:48:41

标签: python json django

实际上,当我想在Django模板中更改日历日期时,它必须运行第一个代码,如果不运行,则必须等价,但我会遇到多值错误

def homepage(request):
    if request.method == 'GET':
        start_date = request.GET['start_date']
        end_date = request.GET['end_date']

        start = parser.parse(start_date)
        end = parser.parse(end_date)

        _start = json.dumps({"year":start.year, "month":start.month, "day":start.day})
        _end = json.dumps({"year":end.year, "month":end.month, "day":end.day})
        url="https://backend.makerobos.com/admin_apis/?key=g223p25HE23fdg2hHJBhU545HuhI&start={}&end={}".format(_start, _end)
        response=requests.get(url).json()
        return render(request,'robosapiapp/robos.html',{'data':response})
    else:
        url="https://backend.makerobos.com/admin_apis/?key=g223p25HE23fdg2hHJBhU545HuhI&start={%22year%22:2019,%22month%22:2,%22day%22:11}&end={%22year%22:2019,%22month%22:3,%22day%22:1}"
        response=requests.get(url).json()
        return render(request,'robosapiapp/robos.html',{'data':response})

这是我的表单页面

<form method="GET">
     <label>From:<input id="start_date" type="date" name="start_date"></label> &nbsp;
     <label>To:<input id="end_date" type="date" name="end_date"></label> &nbsp;
     <input type="submit" name="search" id="search" value="Submit">
   </form>

1 个答案:

答案 0 :(得分:1)

即使您第一次加载页面,请求也始终是GET。您只想在实际提交表单时运行代码;因此,您应该更改条件以检查提交内容。一种方法是查找提交按钮本身。

def homepage(request):
    if 'search' in request.GET: