2014-06-13 60 views
-2

我的目标是让用户输入日期以从选定日期检索预测数据。我的网址的格式为wwww.mywebsite.com/forecasts/region/{{date}}(我正在使用Pyramid的URL调度)。我希望用户能够以YYYYMMDDHH格式输入日期,并在点击提交后将用户带到该日期的网址。有没有办法在HTML中做到这一点?或者,将JavaScript,或服务器端Python是一个更好的选择?由于单击提交后根据表单输入链接到URL

回答

0

您可以与服务器端的python这样做很容易:

@view_config(route_name='foo') 
def foo(request): 
    #get date like something like this: 
    if ('date' in request.params.keys()): 
     userInputDate = request.params.get('date') 

    #redirect the user to date specific view 
    return HTTPFound(location=request.route_url('bar', date = userInputDate) 

@view_config(route_name='bar', renderer='bar.mak') 
def bar(request): 
    #get that usergiven date 
    date = request.matchdict['date'] 
    #do stuff 

路由定义了吧:

config.add_route('bar','/bar/{date:\d+}') 

这将创建与改变的值,比如日期的网址。关于此的更多信息:http://docs.pylonsproject.org/docs/pyramid/en/1.5-branch/narr/urldispatch.html