2015-02-10 424 views
1

我想要做的是传递函数参数中的日期,然后处理输入。这里的功能它如何在URL中使用*斜杠*传递参数?

@HR.route('/confirm_sickLeave/<date>/<user>', methods=['GET', 'POST']) 
def confirm_sickLeave(user,date): 
    u = User.query.filter_by(username=user.username).first() 
    print u 
    us = UserStatistics.filter_by(userId=u.id).first() 
    temp = us.slDates 
    dates = temp.keys() 
    for a in dates: 
     if a == date: 
      temp['date'] = True 
      flash('Date Confirmed.') 
      return redirect(url_for('.approval_of_leaves')) 


    return redirect(url_for('.approval_of_leaves')) 

现在,我的问题是我无法通过我的函数的值。原因是我的输入dates在其中有斜线(/)。让我告诉你:

HTML:

{% for u in all_users %} 
## Returns all the dates applied by the user (it's in dict format) 
{% set user_info = u.return_sickLeaves(u.username) %} 
{% for us in user_info %} 
<tr> 

     <td>  {{ u.username }} </td> 
     <td>  {{ us|e }} </td> 
     {% if us.value|e == True %} 
     <td class="success">Confirmed</td> 
     {% else %} 
     <td class="warning">Pending..</td> 
     {% endif %} 
     <td><a href = "{{ url_for('HR.confirm_sickLeave', user=u.username, date= us|e) }}">Confirm</a> 
      <a href = "#">Deny</a> 
      </td> 
     {% endfor %} 
</tr> 
{% endfor %} 

现在,当我尝试点击确认按钮,我得到的回应是Error 404 not found。 404错误的网址是:http://localhost:5000/confirm_sickLeave/02/01/2015%3B02/02/2015/seer

有没有其他方法可以做?感谢您的贡献。 :)

+1

格式化网址的日期,因此它是'2015-02-02'。 – 2015-02-10 11:45:30

+0

不知道它如何在Python中工作,但如果你需要读取值,你可以编码/到%09然后发送。看网址编码 – 2015-02-10 11:54:02

回答

2

斜线在URL路径中携带含义,因此明确指定的路径部分的默认转换器不包括斜线。

你有两个选择:

  • 明确匹配日期的独立部分,并重新组成:

    @HR.route('/confirm_sickLeave/<year>/<month>/<day>/<user>', methods=['GET', 'POST']) 
    def confirm_sickLeave(user, year, month, day): 
        date = '/'.join([year, month, day]) 
    
  • 格式的日期,使用不同的分隔符,就像一个-

可以比赛斜线路径中,与path转换器(所以/confirm_sickLeave/<path:date>/<user>),但是这意味着你现在匹配斜杠的路径中包含任意数量,使得验证更难。