librelist archives

« back to archive

URL_FOR() question

URL_FOR() question

From:
Vadim Pestovnikov
Date:
2012-06-19 @ 04:05
Hi all,
I am trying to figure out how to generate links like this: <a
href="news/#1">Read more</a>, so the final result should be:
http://website.com/news/#1

In my view I was trying to specify route like this:

app.route('/news/#<int:1>')

but "#" symbol gets encoded in generated URL every time e.g.
http://website.com/news/%231

-- 
Best Regards,
Vadim Pestovnikov | vadim.pestovnikov@gmail.com

Re: [flask] URL_FOR() question

From:
Smartboy
Date:
2012-06-19 @ 06:02
I don't think this is possible, since the hashtag symbol is used to scroll
to a specific element with the id following the pound sign in the browser.
You'd have to use something client side to override this action, and even
then the hashtag would still have to be encoded. In my opinion, a better
solution would be using something like Sammy.js or similar.

Smartboy

On Mon, Jun 18, 2012 at 9:05 PM, Vadim Pestovnikov <
vadim.pestovnikov@gmail.com> wrote:

> Hi all,
> I am trying to figure out how to generate links like this: <a
> href="news/#1">Read more</a>, so the final result should be:
> http://website.com/news/#1
>
> In my view I was trying to specify route like this:
>
> app.route('/news/#<int:1>')
>
> but "#" symbol gets encoded in generated URL every time e.g.
> http://website.com/news/%231
>
> --
> Best Regards,
> Vadim Pestovnikov | vadim.pestovnikov@gmail.com
>

Re: [flask] URL_FOR() question

From:
pronoyc@gmail.com
Date:
2012-06-19 @ 06:08
>
> I don't think this is possible, since the hashtag symbol is used to scroll
> to a specific element with the id following the pound sign in the browser.
> You'd have to use something client side to override this action, and even
> then the hashtag would still have to be encoded. In my opinion, a better
> solution would be using something like Sammy.js or similar.


Or you could pass a variable in the context and append '#' to the url on
the render time



-- 
Regards,

Pronoy Chopra
http://blog.pronoy.in <http://www.pronoy.in/about>/
http://www.twitter.com/pronoyc

Re: [flask] URL_FOR() question

From:
Vadim Pestovnikov
Date:
2012-06-19 @ 06:19
Well, I am afraid this is not going to work for me because as SmartBoy
already mentioned I need this for scrolling to a specific element with
the id following the pound sign. And this pound sign should be inside
URL:  http://website.com/news/#1

It seems that only one solution for now is not to use url_for() use manual link.

On Tue, Jun 19, 2012 at 2:08 AM, pronoyc@gmail.com <pronoyc@gmail.com> wrote:
>> I don't think this is possible, since the hashtag symbol is used to scroll
>> to a specific element with the id following the pound sign in the browser.
>> You'd have to use something client side to override this action, and even
>> then the hashtag would still have to be encoded. In my opinion, a better
>> solution would be using something like Sammy.js or similar.
>
>
> Or you could pass a variable in the context and append '#' to the url on the
> render time
>
>
>
> --
> Regards,
>
> Pronoy Chopra
> http://blog.pronoy.in/
> http://www.twitter.com/pronoyc
>



-- 
Best Regards,
Vadim Pestovnikov | vadim.pestovnikov@gmail.com

Re: [flask] URL_FOR() question

From:
Steven Kryskalla
Date:
2012-06-19 @ 06:15
On Mon, Jun 18, 2012 at 9:05 PM, Vadim Pestovnikov
<vadim.pestovnikov@gmail.com> wrote:
> Hi all,
> I am trying to figure out how to generate links like this: <a
> href="news/#1">Read more</a>, so the final result should be:
> http://website.com/news/#1
>
> In my view I was trying to specify route like this:
>
> app.route('/news/#<int:1>')
>
> but "#" symbol gets encoded in generated URL every time e.g.
> http://website.com/news/%231
>

Do you want to generate the url with url_for? Or add a route to match
the "#" part of the URL using app.route?

If you just want to generate the URL then use the "_anchor" argument:

http://flask.pocoo.org/docs/api/#flask.url_for

Re: [flask] URL_FOR() question

From:
Vadim Pestovnikov
Date:
2012-06-19 @ 06:34
I guess using URL_FOR() should be enough. It seems that  _anchor is
version 0.9 (New in version 0.9: The _anchor and _method parameters
were added.) and is not released still.

I will try 0.9 version. I am assuming the use of the _anchor would be
something like this:

url_for('news', _anchor='#" + news.id)

Am I correct?

On Tue, Jun 19, 2012 at 2:15 AM, Steven Kryskalla <skryskalla@gmail.com> wrote:
> On Mon, Jun 18, 2012 at 9:05 PM, Vadim Pestovnikov
> <vadim.pestovnikov@gmail.com> wrote:
>> Hi all,
>> I am trying to figure out how to generate links like this: <a
>> href="news/#1">Read more</a>, so the final result should be:
>> http://website.com/news/#1
>>
>> In my view I was trying to specify route like this:
>>
>> app.route('/news/#<int:1>')
>>
>> but "#" symbol gets encoded in generated URL every time e.g.
>> http://website.com/news/%231
>>
>
> Do you want to generate the url with url_for? Or add a route to match
> the "#" part of the URL using app.route?
>
> If you just want to generate the URL then use the "_anchor" argument:
>
> http://flask.pocoo.org/docs/api/#flask.url_for



-- 
Best Regards,
Vadim Pestovnikov | vadim.pestovnikov@gmail.com

Re: [flask] URL_FOR() question

From:
Steven Kryskalla
Date:
2012-06-19 @ 07:19
On Mon, Jun 18, 2012 at 11:34 PM, Vadim Pestovnikov
<vadim.pestovnikov@gmail.com> wrote:
> I will try 0.9 version. I am assuming the use of the _anchor would be
> something like this:
>
> url_for('news', _anchor='#" + news.id)
>
> Am I correct?
>

That's correct, but you don't need to put the "#", it will do that for you.

Re: [flask] URL_FOR() question

From:
Simon Sapin
Date:
2012-06-19 @ 07:39
Le 19/06/2012 06:05, Vadim Pestovnikov a écrit :
> In my view I was trying to specify route like this:
>
> app.route('/news/#<int:1>')

This probably won’t work.

When your browser loads http://example.com/foo#bar it sends something 
like this:

GET /foo HTTP/1.1
Host: example.com

It does *not* send the #bar part to the server. Only when the page is 
loaded, it will try to find something with id="bar" in the HTML and 
scroll to that.

Do you want to pass some information to the server? If so, use something 
else than # for the delimiter.

Do you want the browser to scroll some part of the page into view once 
it is loaded? If so, use # but remember that everything after is not 
sent to the server. As already noted, you can build such an URL with the 
_anchor parameter to url_for()

Regards,
-- 
Simon Sapin