2015-02-10 61 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>),但是這意味着你現在匹配斜槓的路徑中包含任意數量,使得驗證更難。

相關問題