librelist archives

« back to archive

Easy question - lost query strings on redirect

Easy question - lost query strings on redirect

From:
Rob Mela
Date:
2011-05-06 @ 16:01
Given a route for  '/mypath/foo' and a request to 
"http://myserver.com/mypath/foo?a=b"  then...

Flask redirects to "http://myserver.scom/mypath/foo/" ( path with '/' 
appended) but drops the query string.

Is there an easy config setting to preserve the query string, or disable 
the redirect?

thanks.

Re: [flask] Easy question - lost query strings on redirect

From:
Armin Ronacher
Date:
2011-05-06 @ 19:01
Hi,

On 2011-05-06 6:01 PM, Rob Mela wrote:
> Given a route for  '/mypath/foo' and a request to 
"http://myserver.com/mypath/foo?a=b"  then...
>
> Flask redirects to "http://myserver.scom/mypath/foo/" ( path with '/' 
appended) but drops the query string.
>
> Is there an easy config setting to preserve the query string, or disable
the redirect?
This was improved with the development version of Werkzeug.  I wanted to 
do a release last week but I was busy with a bunch of stuff.  You can 
however use the development version already if you want that improved 
behavior.


Regards,
Armin

Re: [flask] Easy question - lost query strings on redirect

From:
Rob Mela
Date:
2011-05-09 @ 14:02
Currently our product is the only client of the API we're creating, so I 
can dictate that the client that a trailing slash shall be appended to all
paths.    This is probably easier and safer than mixing versions of 
Werkzeug and Flask.

When we start  getting 3rd party users then the graceful behavior will be 
extremely helpful - but I'm sure that by then the Werkzeug fix will have 
long since made its way into Flask 0.8 :)


Den May 6, 2011 kl. 3:01 PM skrev Armin Ronacher:

> Hi,
> 
> On 2011-05-06 6:01 PM, Rob Mela wrote:
>> Given a route for  '/mypath/foo' and a request to 
"http://myserver.com/mypath/foo?a=b"  then...
>> 
>> Flask redirects to "http://myserver.scom/mypath/foo/" ( path with '/' 
appended) but drops the query string.
>> 
>> Is there an easy config setting to preserve the query string, or 
disable the redirect?
> This was improved with the development version of Werkzeug.  I wanted to 
> do a release last week but I was busy with a bunch of stuff.  You can 
> however use the development version already if you want that improved 
> behavior.
> 
> 
> Regards,
> Armin

Re: [flask] Easy question - lost query strings on redirect

From:
Drew Vogel
Date:
2011-05-06 @ 16:06
On Fri, May 6, 2011 at 11:01 AM, Rob Mela <rob@thinkingscreen.com> wrote:

> Given a route for  '/mypath/foo' and a request to "
> http://myserver.com/mypath/foo?a=b"  then...
>
> Flask redirects to "http://myserver.scom/mypath/foo/" ( path with '/'
> appended) but drops the query string.
>

This is not the behavior I observe and the documentation disagrees:

http://flask.pocoo.org/docs/quickstart/#routing

A request for /mypath/foo will only redirect to the /mypath/foo/ URI if the
route includes the trailing slash.

A question and views and templates

From:
Max Countryman
Date:
2011-05-11 @ 21:17
Hi all,

I have what is probably a silly question, so please forgive me if I'm 
missing something obvious. I've only been working with Flask for a few 
weeks and am still learning.

My problem is this, I have a view that looks something like this:

@app.route('/do/stuff', methods=['GET', 'POST'])
def do_stuff():
    url = request.args.get('url', '')

    form = DoStuffForm(request.form)
    if request.method == 'POST' and form.validate():
        if not url:
            url = form.url.data
            return url_for('do_stuff', url=url)
        else:
	    # I'm just going to use the user query as an example
            user = User.query.filter_by(
                    username=session.get('username')
                    ).first()
            user = User(
                name = form.name.data,  
                )
            db.session.add(user)
            db.session.commit()
            
    return render_template('do_stuff.html', form=form)

Now in my template, do_stuff.html, I have a form that will either show an 
input box for the URL (if there's no URL given in the form of 
/do/stuff?url=http://example.com/ or if it is provided show the user input
box instead. It looks something like this:

    {% if not request.args.get('url', '') %}
      {{ render_field(form.url) }}
    {% else %}
      {{ render_field(form.user) }}
    {% endif %}

If I manually specify /do/stuff?url=http://example.com/ it does what I 
would expect and renders just the user input box. However I can't seem to 
get the same results using url_for('do_stuff', url=url).

Am I going about this the wrong way? Essentially what I want is to send a 
URL to the view using that view and then render a different set of forms 
when the URL is provided. Hopefully that makes sense, I'm a little sleepy 
so my apologies if this makes no sense.

Thank you for reading,

Regards,


Max

Re: [flask] A question and views and templates

From:
Armin Ronacher
Date:
2011-05-11 @ 22:48
Hi,

On 2011-05-11 11:17 PM, Max Countryman wrote:
> If I manually specify /do/stuff?url=http://example.com/ it does what I 
would expect
In such a case, paste the view code :)  There is probably something very 
simple you're missing.


Regards,
Armin

Re: [flask] A question and views and templates

From:
Max Countryman
Date:
2011-05-11 @ 22:53
Sure, I actually pasted it in the first post:

@app.route('/do/stuff', methods=['GET', 'POST'])
def do_stuff():
   url = request.args.get('url', '')

   form = DoStuffForm(request.form)
   if request.method == 'POST' and form.validate():
       if not url:
           url = form.url.data
           return redirect(url_for('do_stuff', url=url))
       else:
	    # I'm just going to use the user query as an example
           user = User.query.filter_by(
                   username=session.get('username')
                   ).first()
           user = User(
               name = form.name.data,  
               )
           db.session.add(user)
           db.session.commit()

   return render_template('do_stuff.html', form=form)
On May 11, 2011, at 3:48 PM, Armin Ronacher wrote:

> Hi,
> 
> On 2011-05-11 11:17 PM, Max Countryman wrote:
>> If I manually specify /do/stuff?url=http://example.com/ it does what I 
would expect
> In such a case, paste the view code :)  There is probably something very 
> simple you're missing.
> 
> 
> Regards,
> Armin

Re: [flask] A question and views and templates

From:
Armin Ronacher
Date:
2011-05-11 @ 22:59
Hi,

Print the value of form.url.data.  In case it's None, that's your problem :)


Regards,
Armin

Re: [flask] A question and views and templates

From:
Max Countryman
Date:
2011-05-12 @ 00:00
Great, I think the form was causing the issue. Now it seems to be working.
Thanks for your time and patience all!

Regards,


Max

On May 11, 2011, at 3:59 PM, Armin Ronacher wrote:

> Hi,
> 
> Print the value of form.url.data.  In case it's None, that's your problem :)
> 
> 
> Regards,
> Armin

Re: [flask] A question and views and templates

From:
Max Countryman
Date:
2011-05-11 @ 23:04
All right, let me check really quickly :) btw where will I see the print? 
In the console where I'm running Flask from I assume?

On May 11, 2011, at 3:59 PM, Armin Ronacher wrote:

> Hi,
> 
> Print the value of form.url.data.  In case it's None, that's your problem :)
> 
> 
> Regards,
> Armin

Re: [flask] A question and views and templates

From:
Alistair Roche
Date:
2011-05-11 @ 22:00
Hi Max,

It's only a very small mistake you're making.

url_for's **values arguments are for the variable parts of a URL rule,
not for GET parameters. See:

http://flask.pocoo.org/docs/api/#flask.Flask.route
and
http://flask.pocoo.org/docs/api/#flask.url_for

As an example, if your rule is:

@app.route('/do/stuff/<url>/')
def do_stuff():
    #etc

then url_for('do_stuff', url='something') will return '/do/stuff/something/'.

Instead, you want to suffix url_for('do_stuff') with "?url=%s" % url.

Hope this helps.

Cheers,

Alistair

P.S. In the first return statement you probably meant to wrap that URL
with a 'flask.redirect'.


On 11 May 2011 22:17, Max Countryman <maxc@me.com> wrote:
> Hi all,
>
> I have what is probably a silly question, so please forgive me if I'm 
missing something obvious. I've only been working with Flask for a few 
weeks and am still learning.
>
> My problem is this, I have a view that looks something like this:
>
> @app.route('/do/stuff', methods=['GET', 'POST'])
> def do_stuff():
>    url = request.args.get('url', '')
>
>    form = DoStuffForm(request.form)
>    if request.method == 'POST' and form.validate():
>        if not url:
>            url = form.url.data
>            return url_for('do_stuff', url=url)
>        else:
>            # I'm just going to use the user query as an example
>            user = User.query.filter_by(
>                    username=session.get('username')
>                    ).first()
>            user = User(
>                name = form.name.data,
>                )
>            db.session.add(user)
>            db.session.commit()
>
>    return render_template('do_stuff.html', form=form)
>
> Now in my template, do_stuff.html, I have a form that will either show 
an input box for the URL (if there's no URL given in the form of 
/do/stuff?url=http://example.com/ or if it is provided show the user input
box instead. It looks something like this:
>
>    {% if not request.args.get('url', '') %}
>      {{ render_field(form.url) }}
>    {% else %}
>      {{ render_field(form.user) }}
>    {% endif %}
>
> If I manually specify /do/stuff?url=http://example.com/ it does what I 
would expect and renders just the user input box. However I can't seem to 
get the same results using url_for('do_stuff', url=url).
>
> Am I going about this the wrong way? Essentially what I want is to send 
a URL to the view using that view and then render a different set of forms
when the URL is provided. Hopefully that makes sense, I'm a little sleepy 
so my apologies if this makes no sense.
>
> Thank you for reading,
>
> Regards,
>
>
> Max
>

Re: [flask] A question and views and templates

From:
Max Countryman
Date:
2011-05-11 @ 22:16
So something like this?

return redirect(
                    url_for('do_stuff', '?url={0}'.format(from_url))
                    )

Still getting the same results unfortunately...

On May 11, 2011, at 3:00 PM, Alistair Roche wrote:

> Hi Max,
> 
> It's only a very small mistake you're making.
> 
> url_for's **values arguments are for the variable parts of a URL rule,
> not for GET parameters. See:
> 
> http://flask.pocoo.org/docs/api/#flask.Flask.route
> and
> http://flask.pocoo.org/docs/api/#flask.url_for
> 
> As an example, if your rule is:
> 
> @app.route('/do/stuff/<url>/')
> def do_stuff():
>    #etc
> 
> then url_for('do_stuff', url='something') will return '/do/stuff/something/'.
> 
> Instead, you want to suffix url_for('do_stuff') with "?url=%s" % url.
> 
> Hope this helps.
> 
> Cheers,
> 
> Alistair
> 
> P.S. In the first return statement you probably meant to wrap that URL
> with a 'flask.redirect'.
> 
> 
> On 11 May 2011 22:17, Max Countryman <maxc@me.com> wrote:
>> Hi all,
>> 
>> I have what is probably a silly question, so please forgive me if I'm 
missing something obvious. I've only been working with Flask for a few 
weeks and am still learning.
>> 
>> My problem is this, I have a view that looks something like this:
>> 
>> @app.route('/do/stuff', methods=['GET', 'POST'])
>> def do_stuff():
>>    url = request.args.get('url', '')
>> 
>>    form = DoStuffForm(request.form)
>>    if request.method == 'POST' and form.validate():
>>        if not url:
>>            url = form.url.data
>>            return url_for('do_stuff', url=url)
>>        else:
>>            # I'm just going to use the user query as an example
>>            user = User.query.filter_by(
>>                    username=session.get('username')
>>                    ).first()
>>            user = User(
>>                name = form.name.data,
>>                )
>>            db.session.add(user)
>>            db.session.commit()
>> 
>>    return render_template('do_stuff.html', form=form)
>> 
>> Now in my template, do_stuff.html, I have a form that will either show 
an input box for the URL (if there's no URL given in the form of 
/do/stuff?url=http://example.com/ or if it is provided show the user input
box instead. It looks something like this:
>> 
>>    {% if not request.args.get('url', '') %}
>>      {{ render_field(form.url) }}
>>    {% else %}
>>      {{ render_field(form.user) }}
>>    {% endif %}
>> 
>> If I manually specify /do/stuff?url=http://example.com/ it does what I 
would expect and renders just the user input box. However I can't seem to 
get the same results using url_for('do_stuff', url=url).
>> 
>> Am I going about this the wrong way? Essentially what I want is to send
a URL to the view using that view and then render a different set of forms
when the URL is provided. Hopefully that makes sense, I'm a little sleepy 
so my apologies if this makes no sense.
>> 
>> Thank you for reading,
>> 
>> Regards,
>> 
>> 
>> Max
>> 

Re: [flask] A question and views and templates

From:
Max Countryman
Date:
2011-05-11 @ 22:23
I'm wondering if request.args is the same when I do the redirect as when I
call it manually via /do/stuff?url=http://example.com?

What I'm getting at is the template seems to render correctly only when I 
manually specify ?url=http://example.com in the URL. Unless I'm missing 
something else? it seems like when I try to do that from the view itself 
the template doesn't find the url in request.args, i.e. 
request.args.get('url', '') seems to be returning ''.

On May 11, 2011, at 3:16 PM, Max Countryman wrote:

> So something like this?
> 
> return redirect(
>                    url_for('do_stuff', '?url={0}'.format(from_url))
>                    )
> 
> Still getting the same results unfortunately...
> 
> On May 11, 2011, at 3:00 PM, Alistair Roche wrote:
> 
>> Hi Max,
>> 
>> It's only a very small mistake you're making.
>> 
>> url_for's **values arguments are for the variable parts of a URL rule,
>> not for GET parameters. See:
>> 
>> http://flask.pocoo.org/docs/api/#flask.Flask.route
>> and
>> http://flask.pocoo.org/docs/api/#flask.url_for
>> 
>> As an example, if your rule is:
>> 
>> @app.route('/do/stuff/<url>/')
>> def do_stuff():
>>   #etc
>> 
>> then url_for('do_stuff', url='something') will return '/do/stuff/something/'.
>> 
>> Instead, you want to suffix url_for('do_stuff') with "?url=%s" % url.
>> 
>> Hope this helps.
>> 
>> Cheers,
>> 
>> Alistair
>> 
>> P.S. In the first return statement you probably meant to wrap that URL
>> with a 'flask.redirect'.
>> 
>> 
>> On 11 May 2011 22:17, Max Countryman <maxc@me.com> wrote:
>>> Hi all,
>>> 
>>> I have what is probably a silly question, so please forgive me if I'm 
missing something obvious. I've only been working with Flask for a few 
weeks and am still learning.
>>> 
>>> My problem is this, I have a view that looks something like this:
>>> 
>>> @app.route('/do/stuff', methods=['GET', 'POST'])
>>> def do_stuff():
>>>   url = request.args.get('url', '')
>>> 
>>>   form = DoStuffForm(request.form)
>>>   if request.method == 'POST' and form.validate():
>>>       if not url:
>>>           url = form.url.data
>>>           return url_for('do_stuff', url=url)
>>>       else:
>>>           # I'm just going to use the user query as an example
>>>           user = User.query.filter_by(
>>>                   username=session.get('username')
>>>                   ).first()
>>>           user = User(
>>>               name = form.name.data,
>>>               )
>>>           db.session.add(user)
>>>           db.session.commit()
>>> 
>>>   return render_template('do_stuff.html', form=form)
>>> 
>>> Now in my template, do_stuff.html, I have a form that will either show
an input box for the URL (if there's no URL given in the form of 
/do/stuff?url=http://example.com/ or if it is provided show the user input
box instead. It looks something like this:
>>> 
>>>   {% if not request.args.get('url', '') %}
>>>     {{ render_field(form.url) }}
>>>   {% else %}
>>>     {{ render_field(form.user) }}
>>>   {% endif %}
>>> 
>>> If I manually specify /do/stuff?url=http://example.com/ it does what I
would expect and renders just the user input box. However I can't seem to 
get the same results using url_for('do_stuff', url=url).
>>> 
>>> Am I going about this the wrong way? Essentially what I want is to 
send a URL to the view using that view and then render a different set of 
forms when the URL is provided. Hopefully that makes sense, I'm a little 
sleepy so my apologies if this makes no sense.
>>> 
>>> Thank you for reading,
>>> 
>>> Regards,
>>> 
>>> 
>>> Max
>>> 
> 

Re: [flask] A question and views and templates

From:
Alistair Roche
Date:
2011-05-11 @ 22:21
Well, I was thinking more url_for('do_stuff') + "?url=" + url, but as
Armin just said, you can get the exact same result by doing
url_for('do_stuff', url=url), which is what you had originally. Have
you passing that to redirect() before returning it?

Also, don't forget #pocoo on irc.freenode.net, if you haven't tried
there already.

On 11 May 2011 23:16, Max Countryman <maxc@me.com> wrote:
> So something like this?
>
> return redirect(
>                    url_for('do_stuff', '?url={0}'.format(from_url))
>                    )
>
> Still getting the same results unfortunately...
>
> On May 11, 2011, at 3:00 PM, Alistair Roche wrote:
>
>> Hi Max,
>>
>> It's only a very small mistake you're making.
>>
>> url_for's **values arguments are for the variable parts of a URL rule,
>> not for GET parameters. See:
>>
>> http://flask.pocoo.org/docs/api/#flask.Flask.route
>> and
>> http://flask.pocoo.org/docs/api/#flask.url_for
>>
>> As an example, if your rule is:
>>
>> @app.route('/do/stuff/<url>/')
>> def do_stuff():
>>    #etc
>>
>> then url_for('do_stuff', url='something') will return '/do/stuff/something/'.
>>
>> Instead, you want to suffix url_for('do_stuff') with "?url=%s" % url.
>>
>> Hope this helps.
>>
>> Cheers,
>>
>> Alistair
>>
>> P.S. In the first return statement you probably meant to wrap that URL
>> with a 'flask.redirect'.
>>
>>
>> On 11 May 2011 22:17, Max Countryman <maxc@me.com> wrote:
>>> Hi all,
>>>
>>> I have what is probably a silly question, so please forgive me if I'm 
missing something obvious. I've only been working with Flask for a few 
weeks and am still learning.
>>>
>>> My problem is this, I have a view that looks something like this:
>>>
>>> @app.route('/do/stuff', methods=['GET', 'POST'])
>>> def do_stuff():
>>>    url = request.args.get('url', '')
>>>
>>>    form = DoStuffForm(request.form)
>>>    if request.method == 'POST' and form.validate():
>>>        if not url:
>>>            url = form.url.data
>>>            return url_for('do_stuff', url=url)
>>>        else:
>>>            # I'm just going to use the user query as an example
>>>            user = User.query.filter_by(
>>>                    username=session.get('username')
>>>                    ).first()
>>>            user = User(
>>>                name = form.name.data,
>>>                )
>>>            db.session.add(user)
>>>            db.session.commit()
>>>
>>>    return render_template('do_stuff.html', form=form)
>>>
>>> Now in my template, do_stuff.html, I have a form that will either show
an input box for the URL (if there's no URL given in the form of 
/do/stuff?url=http://example.com/ or if it is provided show the user input
box instead. It looks something like this:
>>>
>>>    {% if not request.args.get('url', '') %}
>>>      {{ render_field(form.url) }}
>>>    {% else %}
>>>      {{ render_field(form.user) }}
>>>    {% endif %}
>>>
>>> If I manually specify /do/stuff?url=http://example.com/ it does what I
would expect and renders just the user input box. However I can't seem to 
get the same results using url_for('do_stuff', url=url).
>>>
>>> Am I going about this the wrong way? Essentially what I want is to 
send a URL to the view using that view and then render a different set of 
forms when the URL is provided. Hopefully that makes sense, I'm a little 
sleepy so my apologies if this makes no sense.
>>>
>>> Thank you for reading,
>>>
>>> Regards,
>>>
>>>
>>> Max
>>>
>
>



-- 
-- Alistair

Re: [flask] A question and views and templates

From:
Max Countryman
Date:
2011-05-11 @ 22:30
Here's what I'm trying now:

return redirect(url_for('do_stuff', url=url))

Same result. It renders the same form field.

However if I manually request /do/stuff?url=url it works, the template 
updates and shows the other form.

On May 11, 2011, at 3:21 PM, Alistair Roche wrote:

> Well, I was thinking more url_for('do_stuff') + "?url=" + url, but as
> Armin just said, you can get the exact same result by doing
> url_for('do_stuff', url=url), which is what you had originally. Have
> you passing that to redirect() before returning it?
> 
> Also, don't forget #pocoo on irc.freenode.net, if you haven't tried
> there already.
> 
> On 11 May 2011 23:16, Max Countryman <maxc@me.com> wrote:
>> So something like this?
>> 
>> return redirect(
>>                    url_for('do_stuff', '?url={0}'.format(from_url))
>>                    )
>> 
>> Still getting the same results unfortunately...
>> 
>> On May 11, 2011, at 3:00 PM, Alistair Roche wrote:
>> 
>>> Hi Max,
>>> 
>>> It's only a very small mistake you're making.
>>> 
>>> url_for's **values arguments are for the variable parts of a URL rule,
>>> not for GET parameters. See:
>>> 
>>> http://flask.pocoo.org/docs/api/#flask.Flask.route
>>> and
>>> http://flask.pocoo.org/docs/api/#flask.url_for
>>> 
>>> As an example, if your rule is:
>>> 
>>> @app.route('/do/stuff/<url>/')
>>> def do_stuff():
>>>    #etc
>>> 
>>> then url_for('do_stuff', url='something') will return '/do/stuff/something/'.
>>> 
>>> Instead, you want to suffix url_for('do_stuff') with "?url=%s" % url.
>>> 
>>> Hope this helps.
>>> 
>>> Cheers,
>>> 
>>> Alistair
>>> 
>>> P.S. In the first return statement you probably meant to wrap that URL
>>> with a 'flask.redirect'.
>>> 
>>> 
>>> On 11 May 2011 22:17, Max Countryman <maxc@me.com> wrote:
>>>> Hi all,
>>>> 
>>>> I have what is probably a silly question, so please forgive me if I'm
missing something obvious. I've only been working with Flask for a few 
weeks and am still learning.
>>>> 
>>>> My problem is this, I have a view that looks something like this:
>>>> 
>>>> @app.route('/do/stuff', methods=['GET', 'POST'])
>>>> def do_stuff():
>>>>    url = request.args.get('url', '')
>>>> 
>>>>    form = DoStuffForm(request.form)
>>>>    if request.method == 'POST' and form.validate():
>>>>        if not url:
>>>>            url = form.url.data
>>>>            return url_for('do_stuff', url=url)
>>>>        else:
>>>>            # I'm just going to use the user query as an example
>>>>            user = User.query.filter_by(
>>>>                    username=session.get('username')
>>>>                    ).first()
>>>>            user = User(
>>>>                name = form.name.data,
>>>>                )
>>>>            db.session.add(user)
>>>>            db.session.commit()
>>>> 
>>>>    return render_template('do_stuff.html', form=form)
>>>> 
>>>> Now in my template, do_stuff.html, I have a form that will either 
show an input box for the URL (if there's no URL given in the form of 
/do/stuff?url=http://example.com/ or if it is provided show the user input
box instead. It looks something like this:
>>>> 
>>>>    {% if not request.args.get('url', '') %}
>>>>      {{ render_field(form.url) }}
>>>>    {% else %}
>>>>      {{ render_field(form.user) }}
>>>>    {% endif %}
>>>> 
>>>> If I manually specify /do/stuff?url=http://example.com/ it does what 
I would expect and renders just the user input box. However I can't seem 
to get the same results using url_for('do_stuff', url=url).
>>>> 
>>>> Am I going about this the wrong way? Essentially what I want is to 
send a URL to the view using that view and then render a different set of 
forms when the URL is provided. Hopefully that makes sense, I'm a little 
sleepy so my apologies if this makes no sense.
>>>> 
>>>> Thank you for reading,
>>>> 
>>>> Regards,
>>>> 
>>>> 
>>>> Max
>>>> 
>> 
>> 
> 
> 
> 
> -- 
> -- Alistair

Re: [flask] A question and views and templates

From:
Alistair Roche
Date:
2011-05-11 @ 22:48
Is your form validating? Sprinkle print statements liberally over your
code to see which statements are being executed when.

On 11 May 2011 23:30, Max Countryman <maxc@me.com> wrote:
> Here's what I'm trying now:
>
> return redirect(url_for('do_stuff', url=url))
>
> Same result. It renders the same form field.
>
> However if I manually request /do/stuff?url=url it works, the template 
updates and shows the other form.
>
> On May 11, 2011, at 3:21 PM, Alistair Roche wrote:
>
>> Well, I was thinking more url_for('do_stuff') + "?url=" + url, but as
>> Armin just said, you can get the exact same result by doing
>> url_for('do_stuff', url=url), which is what you had originally. Have
>> you passing that to redirect() before returning it?
>>
>> Also, don't forget #pocoo on irc.freenode.net, if you haven't tried
>> there already.
>>
>> On 11 May 2011 23:16, Max Countryman <maxc@me.com> wrote:
>>> So something like this?
>>>
>>> return redirect(
>>>                    url_for('do_stuff', '?url={0}'.format(from_url))
>>>                    )
>>>
>>> Still getting the same results unfortunately...
>>>
>>> On May 11, 2011, at 3:00 PM, Alistair Roche wrote:
>>>
>>>> Hi Max,
>>>>
>>>> It's only a very small mistake you're making.
>>>>
>>>> url_for's **values arguments are for the variable parts of a URL rule,
>>>> not for GET parameters. See:
>>>>
>>>> http://flask.pocoo.org/docs/api/#flask.Flask.route
>>>> and
>>>> http://flask.pocoo.org/docs/api/#flask.url_for
>>>>
>>>> As an example, if your rule is:
>>>>
>>>> @app.route('/do/stuff/<url>/')
>>>> def do_stuff():
>>>>    #etc
>>>>
>>>> then url_for('do_stuff', url='something') will return '/do/stuff/something/'.
>>>>
>>>> Instead, you want to suffix url_for('do_stuff') with "?url=%s" % url.
>>>>
>>>> Hope this helps.
>>>>
>>>> Cheers,
>>>>
>>>> Alistair
>>>>
>>>> P.S. In the first return statement you probably meant to wrap that URL
>>>> with a 'flask.redirect'.
>>>>
>>>>
>>>> On 11 May 2011 22:17, Max Countryman <maxc@me.com> wrote:
>>>>> Hi all,
>>>>>
>>>>> I have what is probably a silly question, so please forgive me if 
I'm missing something obvious. I've only been working with Flask for a few
weeks and am still learning.
>>>>>
>>>>> My problem is this, I have a view that looks something like this:
>>>>>
>>>>> @app.route('/do/stuff', methods=['GET', 'POST'])
>>>>> def do_stuff():
>>>>>    url = request.args.get('url', '')
>>>>>
>>>>>    form = DoStuffForm(request.form)
>>>>>    if request.method == 'POST' and form.validate():
>>>>>        if not url:
>>>>>            url = form.url.data
>>>>>            return url_for('do_stuff', url=url)
>>>>>        else:
>>>>>            # I'm just going to use the user query as an example
>>>>>            user = User.query.filter_by(
>>>>>                    username=session.get('username')
>>>>>                    ).first()
>>>>>            user = User(
>>>>>                name = form.name.data,
>>>>>                )
>>>>>            db.session.add(user)
>>>>>            db.session.commit()
>>>>>
>>>>>    return render_template('do_stuff.html', form=form)
>>>>>
>>>>> Now in my template, do_stuff.html, I have a form that will either 
show an input box for the URL (if there's no URL given in the form of 
/do/stuff?url=http://example.com/ or if it is provided show the user input
box instead. It looks something like this:
>>>>>
>>>>>    {% if not request.args.get('url', '') %}
>>>>>      {{ render_field(form.url) }}
>>>>>    {% else %}
>>>>>      {{ render_field(form.user) }}
>>>>>    {% endif %}
>>>>>
>>>>> If I manually specify /do/stuff?url=http://example.com/ it does what
I would expect and renders just the user input box. However I can't seem 
to get the same results using url_for('do_stuff', url=url).
>>>>>
>>>>> Am I going about this the wrong way? Essentially what I want is to 
send a URL to the view using that view and then render a different set of 
forms when the URL is provided. Hopefully that makes sense, I'm a little 
sleepy so my apologies if this makes no sense.
>>>>>
>>>>> Thank you for reading,
>>>>>
>>>>> Regards,
>>>>>
>>>>>
>>>>> Max
>>>>>
>>>
>>>
>>
>>
>>
>> --
>> -- Alistair
>
>



-- 
-- Alistair

Re: [flask] A question and views and templates

From:
Max Countryman
Date:
2011-05-11 @ 22:55
If it doesn't validate it'll raise an error, so it must be.

I'll see if I can debug with print statements. I'm not really sure where I
can do to see the output of them? Sorry, I'm trying to learn as I go, but 
it comes slowly to me.

On May 11, 2011, at 3:48 PM, Alistair Roche wrote:

> Is your form validating? Sprinkle print statements liberally over your
> code to see which statements are being executed when.
> 
> On 11 May 2011 23:30, Max Countryman <maxc@me.com> wrote:
>> Here's what I'm trying now:
>> 
>> return redirect(url_for('do_stuff', url=url))
>> 
>> Same result. It renders the same form field.
>> 
>> However if I manually request /do/stuff?url=url it works, the template 
updates and shows the other form.
>> 
>> On May 11, 2011, at 3:21 PM, Alistair Roche wrote:
>> 
>>> Well, I was thinking more url_for('do_stuff') + "?url=" + url, but as
>>> Armin just said, you can get the exact same result by doing
>>> url_for('do_stuff', url=url), which is what you had originally. Have
>>> you passing that to redirect() before returning it?
>>> 
>>> Also, don't forget #pocoo on irc.freenode.net, if you haven't tried
>>> there already.
>>> 
>>> On 11 May 2011 23:16, Max Countryman <maxc@me.com> wrote:
>>>> So something like this?
>>>> 
>>>> return redirect(
>>>>                    url_for('do_stuff', '?url={0}'.format(from_url))
>>>>                    )
>>>> 
>>>> Still getting the same results unfortunately...
>>>> 
>>>> On May 11, 2011, at 3:00 PM, Alistair Roche wrote:
>>>> 
>>>>> Hi Max,
>>>>> 
>>>>> It's only a very small mistake you're making.
>>>>> 
>>>>> url_for's **values arguments are for the variable parts of a URL rule,
>>>>> not for GET parameters. See:
>>>>> 
>>>>> http://flask.pocoo.org/docs/api/#flask.Flask.route
>>>>> and
>>>>> http://flask.pocoo.org/docs/api/#flask.url_for
>>>>> 
>>>>> As an example, if your rule is:
>>>>> 
>>>>> @app.route('/do/stuff/<url>/')
>>>>> def do_stuff():
>>>>>    #etc
>>>>> 
>>>>> then url_for('do_stuff', url='something') will return 
'/do/stuff/something/'.
>>>>> 
>>>>> Instead, you want to suffix url_for('do_stuff') with "?url=%s" % url.
>>>>> 
>>>>> Hope this helps.
>>>>> 
>>>>> Cheers,
>>>>> 
>>>>> Alistair
>>>>> 
>>>>> P.S. In the first return statement you probably meant to wrap that URL
>>>>> with a 'flask.redirect'.
>>>>> 
>>>>> 
>>>>> On 11 May 2011 22:17, Max Countryman <maxc@me.com> wrote:
>>>>>> Hi all,
>>>>>> 
>>>>>> I have what is probably a silly question, so please forgive me if 
I'm missing something obvious. I've only been working with Flask for a few
weeks and am still learning.
>>>>>> 
>>>>>> My problem is this, I have a view that looks something like this:
>>>>>> 
>>>>>> @app.route('/do/stuff', methods=['GET', 'POST'])
>>>>>> def do_stuff():
>>>>>>    url = request.args.get('url', '')
>>>>>> 
>>>>>>    form = DoStuffForm(request.form)
>>>>>>    if request.method == 'POST' and form.validate():
>>>>>>        if not url:
>>>>>>            url = form.url.data
>>>>>>            return url_for('do_stuff', url=url)
>>>>>>        else:
>>>>>>            # I'm just going to use the user query as an example
>>>>>>            user = User.query.filter_by(
>>>>>>                    username=session.get('username')
>>>>>>                    ).first()
>>>>>>            user = User(
>>>>>>                name = form.name.data,
>>>>>>                )
>>>>>>            db.session.add(user)
>>>>>>            db.session.commit()
>>>>>> 
>>>>>>    return render_template('do_stuff.html', form=form)
>>>>>> 
>>>>>> Now in my template, do_stuff.html, I have a form that will either 
show an input box for the URL (if there's no URL given in the form of 
/do/stuff?url=http://example.com/ or if it is provided show the user input
box instead. It looks something like this:
>>>>>> 
>>>>>>    {% if not request.args.get('url', '') %}
>>>>>>      {{ render_field(form.url) }}
>>>>>>    {% else %}
>>>>>>      {{ render_field(form.user) }}
>>>>>>    {% endif %}
>>>>>> 
>>>>>> If I manually specify /do/stuff?url=http://example.com/ it does 
what I would expect and renders just the user input box. However I can't 
seem to get the same results using url_for('do_stuff', url=url).
>>>>>> 
>>>>>> Am I going about this the wrong way? Essentially what I want is to 
send a URL to the view using that view and then render a different set of 
forms when the URL is provided. Hopefully that makes sense, I'm a little 
sleepy so my apologies if this makes no sense.
>>>>>> 
>>>>>> Thank you for reading,
>>>>>> 
>>>>>> Regards,
>>>>>> 
>>>>>> 
>>>>>> Max
>>>>>> 
>>>> 
>>>> 
>>> 
>>> 
>>> 
>>> --
>>> -- Alistair
>> 
>> 
> 
> 
> 
> -- 
> -- Alistair

Re: [flask] A question and views and templates

From:
Armin Ronacher
Date:
2011-05-11 @ 22:12
Hi,

On 2011-05-12 12:00 AM, Alistair Roche wrote:

> url_for's **values arguments are for the variable parts of a URL rule,
> not for GET parameters. See:
Values not needed for variable parts are appended to the URL 
automatically as query parameter.


Regards,
Armin

Re: [flask] Easy question - lost query strings on redirect

From:
Rob Mela
Date:
2011-05-06 @ 16:41
This is the code I'm testing with -- on Mac OSX 2.6.6., Python 2.7.1 ( not
that any of that should matter ).... my Python egg is Flask-0.6-py2.7.egg.
Not sure if that's 2.6.1 or what.

 BTW -- is there a way to get Flask version programatically -- e.g. import
flask; print flask.version ?

Anyhow -- with this code



from flask import Flask, request

app=Flask(__name__)
@app.route('/foo')
@app.route('/foo/')
def index():
        return "Query string %s" % request.environ.get('QUERY_STRING')


if __name__ == '__main__':
        app.run(host='127.0.0.1', debug=True)


THe URL http://localhost:5000/foo/?a=b returns "Query string a=b".

The url http://localhost:5000/foo?a=b redirects to 
http://localhost:5000/foo/ and the query string is lost.


Den May 6, 2011 kl. 12:06 PM skrev Drew Vogel:

On Fri, May 6, 2011 at 11:01 AM, Rob Mela 
<rob@thinkingscreen.com<mailto:rob@thinkingscreen.com>> wrote:
Given a route for  '/mypath/foo' and a request to 
"http://myserver.com/mypath/foo?a=b"  then...

Flask redirects to "http://myserver.scom/mypath/foo/" ( path with '/' 
appended) but drops the query string.

This is not the behavior I observe and the documentation disagrees:

http://flask.pocoo.org/docs/quickstart/#routing

A request for /mypath/foo will only redirect to the /mypath/foo/ URI if 
the route includes the trailing slash.


Re: [flask] Easy question - lost query strings on redirect

From:
Wilson Xu
Date:
2011-05-06 @ 18:10
> BTW -- is there a way to get Flask version programatically -- e.g. import
flask; print flask.version ?

I don't see there is any intuitive way to get flask version.

How about reading the source code files? such as setup.py or app.py

-- 
Think and code - imwilsonxu.net

Flask Version String ( was Re: [flask] Easy question - lost query strings on redirect )

From:
Rob Mela
Date:
2011-05-09 @ 14:14
For large applications I like having a "status" or "info" url that 
includes versions of the major 3rd party (non-standard) libraries I use.

In the case of Python, many packages have a __version__ string.

I currently make heavy use of boto.   It's really handy to just print out 
"boto.__version__" to see which release I've got on a particular machine, 
rather than ferret around on disk.

If you have a large number of servers or environments, its nicer to do 
http://myserver.dev.foo.com and see version info in a page than to log 
into servers and look around.

Here's an example of the __version__ string in boto:

>>> import boto
>>> print boto.__version__
2.0b4



Den May 6, 2011 kl. 2:10 PM skrev Wilson Xu:

> BTW -- is there a way to get Flask version programatically -- e.g. 
import flask; print flask.version ?

I don't see there is any intuitive way to get flask version.

How about reading the source code files? such as setup.py or app.py

--
Think and code - imwilsonxu.net<http://imwilsonxu.net/>

Re: Flask Version String ( was Re: [flask] Easy question - lost query strings on redirect )

From:
Ron DuPlain
Date:
2011-05-09 @ 16:10
On Mon, May 9, 2011 at 10:14 AM, Rob Mela <rob@thinkingscreen.com> wrote:
> In the case of Python, many packages have a __version__ string.

+1 for adding flask.__version__.  We would need to make sure this
version string stays in sync with development and that it's clearly
documented at which version it became available.

In the meantime, or for earlier versions should this get added, you
can use setuptools pkg_resources:

    >>> import pkg_resources
    >>> pkg_resources.get_distribution('Flask').version
    '0.7dev-20110509'
    >>>

If you're parsing version strings, one approach:

    >>> version = pkg_resources.get_distribution('Flask').version
    >>> version
    '0.7dev-20110509'
    >>> pkg_resources.parse_version(version)
    ('00000000', '00000007', '*@', '*final-', '20110509', '*final')
    >>>

This tuple is comparable to other version tuples.

-Ron

Re: [flask] Easy question - lost query strings on redirect

From:
Drew Vogel
Date:
2011-05-06 @ 17:33
On Fri, May 6, 2011 at 11:41 AM, Rob Mela <rob@thinkingscreen.com> wrote:

> @app.route('/foo')
> @app.route('/foo/')
>
>
If you want /foo to redirect to /foo/, just specify the second route; the
second route implies the direct from /foo to /foo/. If you don't want the
redirect, just specify the first route. If you want both to be unique
routes, I believe you need to decorate separate functions, but I don't have
time to verify that right now.