How to extract a number from a url

37 views
Skip to first unread message

Sourajit Mohanty

unread,
Dec 5, 2018, 3:48:20 PM12/5/18
to Django users
I want to extract the number from this url 'music/6/newsongs/' so that I can use that number to redirect it to another page..
How can I do that??

shiva kumar

unread,
Dec 5, 2018, 4:03:30 PM12/5/18
to django...@googlegroups.com
Use 

<int: Id> in place of number

An add extra parameter in views which u will relate this to

On Thu 6 Dec, 2018 2:18 am Sourajit Mohanty <smohan...@gmail.com wrote:
I want to extract the number from this  url 'music/6/newsongs/' so that I can use that number to redirect it to another page..
How can I do that??

--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8512424b-8a0e-40a2-92f0-2e9d4f8995a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sourajit Mohanty

unread,
Dec 5, 2018, 4:13:08 PM12/5/18
to django...@googlegroups.com
I am working with Class based Views..so I dont think this is gonna work

Pankaj Kumar

unread,
Dec 5, 2018, 10:43:38 PM12/5/18
to django...@googlegroups.com
Try using regular expression (?P<id>[0-9]+)
similar to this:

url(r'^music/(?P<id>[0-9]+)/newsongs$', views.your_view_function())


cheers!
>> <https://groups.google.com/d/msgid/django-users/CAMsYeuHeAbwr6SgozHM4wxkqMFiWhLnQc6ODy75FHm0qBs9O0Q%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> 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 https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAMh8ciYOg36bQ%3DP%2BLvuuvoCpDaO7C1bMH2O8nPaP6YFeBeYk%3DA%40mail.gmail.com.

Sourajit Mohanty

unread,
Dec 5, 2018, 11:55:08 PM12/5/18
to django...@googlegroups.com
I am using Model forms..

class CreateSong(CreateView)
       model=Song
       fields=['song_title']

So this class is gonna call the model Song class..how can i pass the album_id??

Jason

unread,
Dec 6, 2018, 8:01:49 AM12/6/18
to Django users
you have to set up the URL with it in your urls.py to map to the view method.  

what that does is tells django you want the regex in the url path you defined to be the parameter you send to the view method

pk_url_kwarg = 'pk'

set that in your view, and ensure the url definition you have maps to `pk`  like

path('music/<int:album_id>/newsongs$', views.CreateSong.as_view())

Sourajit Mohanty

unread,
Dec 6, 2018, 9:25:18 AM12/6/18
to django...@googlegroups.com
But how do I access the pk_url_kwarg(ie. the album_id) in the respective model??
I have a model class Song and I need 2 access the album_id in the get_absolute_url()

Reply all
Reply to author
Forward
0 new messages