Many links to one function in url.py

23 views
Skip to first unread message

Jun Tanaka

unread,
May 20, 2014, 10:40:30 PM5/20/14
to django...@googlegroups.com
Hi there.


I hope to know the solution for the following:
say, there are several links to one function but I would like to identify which link that come from.

url.py looks

    (r'^link1/$', 'project.apps.main.get'),
    (r'^link2/$', 'project.apps.main.get'),
    (r'^link3/$', 'project.apps.main.get'),
    (r'^link4/$', 'project.apps.main.get'),

In 'get' function, how can I know which link does that come from? Later, I want to get a parameter , 1, 2, 3, 4 in that function. 

If anyone have a good idea? please teach me.

Lucas Klassmann

unread,
May 20, 2014, 10:49:46 PM5/20/14
to django...@googlegroups.com
Hi Jun,

Try this:

Put only this line in urls.py

url(r'^link/(?P<identifier>\d+)/$', 'project.apps.main.get'),


And in your view, add identifier as argument in function:

def get(request, identifier):
    ...
    return HttpResponse(u'Identifier %d' % identifier)

Note that identifier is a int and you must use link as /link/1

Read more:

Cheers.

--
Lucas Klassmann
Software Developer
Email: lucaskl...@gmail.com
Web site: http://www.lucasklassmann.com

Jun Tanaka

unread,
May 21, 2014, 12:07:50 PM5/21/14
to django...@googlegroups.com
Hi Lucas,

Thank you very much. It seems that I can use this. Hopefully, I can ask you one more question. 

\d+ is for a int. 
is \w+ for a string?  
What does \w* mean? I saw it. 

Jun

2014年5月21日水曜日 11時49分46秒 UTC+9 Lucas Klassmann:

Tom Evans

unread,
May 21, 2014, 12:32:44 PM5/21/14
to django...@googlegroups.com
You can add arguments to send to the view in the url:

https://docs.djangoproject.com/en/1.6/topics/http/urls/#passing-extra-options-to-view-functions

Eg:

urlpatterns = patterns('',
url(r'^link1/$', 'project.apps.main.get', { 'type': 'link1' }),
url(r'^link2/$', 'project.apps.main.get', { 'type': 'link2' }),
url(r'^link3/$', 'project.apps.main.get', { 'type': 'link3' }),
url(r'^link4/$', 'project.apps.main.get', { 'type': 'link4' }),
)

Make sure that you use the url() function rather than a raw tuple.

Cheers

Tom

François Schiettecatte

unread,
May 21, 2014, 12:55:00 PM5/21/14
to django...@googlegroups.com
You can also do something like this:

(r'^link(?P<linkID>d+)/$', 'project.apps.main.get'),

project.apps.main.get will be passed a parameter called linkID containing the number, and if you wanted to limit it to digits 1 through 4, you would use:

(r'^link(?P<linkID>[1-4])/$', 'project.apps.main.get'),

Cheers

François
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1Kjey_jLvi-GsNq0kOFU2PzsXVTx231nrOxrVBHkHsx%2BA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Lucas Klassmann

unread,
May 21, 2014, 1:48:19 PM5/21/14
to django...@googlegroups.com
Hi Jun,

The diference is:

With \w only one character is allowed and not allow null, like this: /link/a/
With \w+ one or more characters is allowed and not allow null, like this: /link/mypage/
With \w* zero or more is allowed and also null argument, like this: /link//   (Note the double slash, django will allow urls like this)

And \d+ is same that \w+, but for only numbers.

More information, you must read Regular Expression for python: https://docs.python.org/2/library/re.html

Cheers


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.



--
Lucas Klassmann
Desenvolvedor de Software
Reply all
Reply to author
Forward
0 new messages