3

What I want to do is to pass the dates inside the parameter of a function, then process the input. Here's the function for it

@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'))

Now, my problem was I can't pass the values in my function. The reason was my input dates had slashes (/) in it. Let me show you:

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 %}

Now, when I try to click confirm button, the response that I get is Error 404 not found. The url of the 404 error is: http://localhost:5000/confirm_sickLeave/02/01/2015%3B02/02/2015/seer

Is there any alternatives I can do? Thank you for you contribution. :)

2
  • 1
    Format the date for the url so it's 2015-02-02. Commented Feb 10, 2015 at 11:45
  • Not Sure how it works in python , but if you need to read values you can encode the / to %09 and then send . see url Encoding Commented Feb 10, 2015 at 11:54

1 Answer 1

7

Slashes carry meaning in a URL path, so the default converter for a path section explicitly excludes slashes.

You have two options:

  • Explicitly match the separate parts of the date and re-constitute:

    @HR.route('/confirm_sickLeave/<year>/<month>/<day>/<user>', methods=['GET', 'POST'])
    def confirm_sickLeave(user, year, month, day):
        date = '/'.join([year, month, day])
    
  • Format your dates to use a different separator, like a -.

You can match slashes in paths, with the path converter (so /confirm_sickLeave/<path:date>/<user>), but that means you now match an arbitrary number of slashes in the path, making validation harder.

Sign up to request clarification or add additional context in comments.

5 Comments

Hey @Martijn Pieters , for example how about a user having value as 'test me' ? and how to ignore replace it with a hyphen just in for the URL?
@Codenewbie Just URL encode the value; spaces become %20, Flask handles that transparently.
How can I replace %20 to a hyphen instead mate ? for e.g.: app.route('/<variable>/') and variable has value 'new base' , how to show in URL like 'new-base' instead of 'new%20base' ?
@Codenewbie Don’t. Because you can’t then remove the dash again to get a space and still keep original dashes, if you have two users called test super user and test super-user you’d get test-super-user in both cases and you can’t reverse the process. Also don’t call people “mate”, please. I’m a volunteer, like everyone else here, not service personnel to get impatient with.
@Codenewbie if you don’t care about reversal, just use manual string replacement (e.g. in Python: username.replace(' ', '-')). If you don’t care about how people react to using mate in a sentence like that, I’ll point you to our help center and the fact that this has strayed way into secondary discussion territory. You should really be using Stack Overflow Chat for inquiries like these.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.