簡體   English   中英

如何在網址中傳遞帶有*的參數?

[英]How to pass a parameter with *slashes* in the 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

我有其他選擇嗎? 謝謝您的貢獻。 :)

斜杠在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> )來匹配路徑中的斜杠 ,但這意味着您現在可以匹配路徑中任意數量的斜杠 ,從而使驗證更加困難。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM