Re: urlencode in Django

814 views
Skip to first unread message

Alexandre González

unread,
Aug 30, 2010, 2:10:33 PM8/30/10
to django...@googlegroups.com
example from one of my codes...

import urllib

parameters = ({'langpair': '%s|%s' % (self.detected_language, 'en'),
                      'v': '1.0',
                      'q': self.words_list.encode('utf-8') })

urllib.urlencode(parameters)

but what do you need this? Becasue you can find a more easy reply that uses urlencode.

lista? Are you from Spain? Me too :p

On Mon, Aug 30, 2010 at 18:22, refreegrata <refre...@yahoo.com> wrote:
Hello lista, I'm a newbie in django. In php i have the  urlencode
function for encoding an url with characters "+","ñ","ó", .... Has
Django an urlencode or similar function?

Thanks for read, and sorry my bad english

P.D.: django 1,2.1

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Please, don't send me files with extensions: .doc, .docx, .xls, .xlsx, .ppt and/or .pptx

Shawn Milochik

unread,
Aug 30, 2010, 2:26:29 PM8/30/10
to django...@googlegroups.com
This a Python question, not a Django question.

import urllib
urllib.quote("+ ñ ó")
'%2B%20%C3%B1%20%C3%B3'

Shawn

Felix Dreissig

unread,
Aug 30, 2010, 2:07:32 PM8/30/10
to refreegrata, django...@googlegroups.com
The urlquote() function from django.utils.http and the "urlencode" template filter might be exactly
what you're looking for.

Regards,
Felix


refreegrata schrieb:


> Hello lista, I'm a newbie in django. In php i have the urlencode

> function for encoding an url with characters "+","�","�", .... Has

refreegrata

unread,
Aug 30, 2010, 12:22:51 PM8/30/10
to Django users
Hello lista, I'm a newbie in django. In php i have the urlencode
function for encoding an url with characters "+","ñ","ó", .... Has

refreegrata

unread,
Aug 30, 2010, 4:09:29 PM8/30/10
to Django users
i'm sorry in my last time accidentally send the post before of
finish.
I try to do in the template something like
{{ my_var|urlencode }}
but don't work. Can i do something like an urlencode in the template?

P.D.: I'm from Chile.

Alexandre González

unread,
Aug 30, 2010, 4:44:27 PM8/30/10
to django...@googlegroups.com
Why do you like to do it?

I think that your problem is with codification, and not with urlenconde... try to search about utf8 and html

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

refreegrata

unread,
Aug 30, 2010, 5:08:05 PM8/30/10
to Django users
for the moment i don´t have a problem, the section of my site work's
fine. Maybe is just a PHP habit.

In php when a wont to build an url in the template i do somethin like
------------------------
<?php $aaa = "óñ+" ?>
echo '<a href="http://mysite.php?aaa='.urlencode($aaa).'>link</a>';
--------------------------
because $aaa can have some special characters.

I thought that the urlencode filter do this in django. I must to build
an url in the template with parameters for a filtered pagination.

On 30 ago, 16:44, Alexandre González <agonzale...@gmail.com> wrote:
> Why do you like to do it?
>
> I think that your problem is with codification, and not with urlenconde...
> try to search about utf8 and html
>
>
>
> On Mon, Aug 30, 2010 at 22:09, refreegrata <refreegr...@yahoo.com> wrote:
> > i'm sorry in my last time  accidentally send the post before of
> > finish.
> > I try to do in the template something like
> > {{ my_var|urlencode }}
> > but  don't work. Can i do something like an urlencode in the template?
>
> > P.D.: I'm from Chile.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to django...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > django-users...@googlegroups.com<django-users%2Bunsu...@googlegroups.com>
> > .

Alexandre González

unread,
Aug 30, 2010, 5:13:38 PM8/30/10
to django...@googlegroups.com
With django you can define you url in urls.py as:

url(r'^sample$', sample, name='the_name'),

and then in your template use: {% url the_name %}

If it need a id or similar you can provide it with {% url the_name ID %}

To unsubscribe from this group, send email to django-users...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

refreegrata

unread,
Aug 30, 2010, 5:29:28 PM8/30/10
to Django users
i know how configure my urls.py is ok, with regular expression. but i
must do something like

<a href="{% url mmm %}var/{{ var|urlencode }}/">link</a>

Now works. In the browser url the character "ñ" is not converted to
"%C3%B1", but the character "+" is converted to "%2B". maybe i have
some problems with the theory. Maybe the character "ñ" isn't a
problem, and for this reason is not converted.

Thanks for read.
> > <django-users%2Bunsu...@googlegroups.com<django-users%252Buns...@googlegroups.com>

Alexandre González

unread,
Aug 30, 2010, 5:32:59 PM8/30/10
to django...@googlegroups.com
use a ñ in a url isn't a good way to work... why don't you change it for a n? With a ñ you could have problems between explorers.

Anyway, you can use <a href="{% url mmmm var %}">link</a>

To unsubscribe from this group, send email to django-users...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

refreegrata

unread,
Aug 30, 2010, 5:45:45 PM8/30/10
to Django users
now works for all characters. I don't know why, but now finally
works. ñ is converted to "%C3%B1", ó is converted to "%C3%B3", + to
"%2B", ...

thanks.

P.D.: I hate use special characters in an url, generaly is a bad idea,
but is necessary in my application.
The client want to filter registers according the name, and the name
can have special characters, and i must to paginate the results. The
other option is use a ghost form and javascript.

Thanks.

Tom Evans

unread,
Sep 1, 2010, 12:10:30 PM9/1/10
to django...@googlegroups.com

The url resolver takes care of all of this magic for you. If you find
it isn't, then you aren't using it right. For instance:

<a href="{% url mmm %}var/{{ var|urlencode }}/">link</a>

This is clearly an incorrect use of a url pattern. Instead, the var
argument should be included as part of the urlpattern, and var given
as an argument to the url template tag.

<a href="{% url mmm-with-var var %}">link</a>

Do it like this, and you won't have problems. If you need to generate
a query string, use the pattern as described by Alexandre González in
the first reply.

Cheers

Tom

refreegrata

unread,
Sep 1, 2010, 2:05:44 PM9/1/10
to Django users
just a few moments ago i saw this in the django documentation. Now
works fine. Thanks.

On 1 sep, 12:10, Tom Evans <tevans...@googlemail.com> wrote:
Reply all
Reply to author
Forward
0 new messages