-1

my urls.py

url(r'^ACB/(?P<slug>.*)/$', views.ACB.as_view(), name='ACB'),

I have requirement where I need to send this url for couple of random slug as part of a response for an API

I do not want the url of request but url of different view

chirag7jain
  • 1,485
  • 3
  • 26
  • 43

2 Answers2

3

Use reverse

from django.core.urlresolvers import reverse

my_url = reverse('ACB')

This would reverse the ACB url without any argument. If slug is present, you can pass it in args:

my_url = reverse('ACB', args=(slug,))

https://docs.djangoproject.com/en/1.9/ref/urlresolvers/#reverse

Railslide
  • 5,344
  • 2
  • 27
  • 34
  • 1
    I would just update this with `my_url = reverse('ACB', args=(slug, ))` if present, as slug is an optional argument. – karthikr Dec 29 '15 at 14:15
  • It's amazing how you are able to get what OP wants. I still have trouble understanding his question. – Shang Wang Dec 29 '15 at 14:59
0

Update 2021 (Django 2.0+)

In Django 2.0, the module django.core.urlresolvers was moved to django.urls.

New solution to question:

from django.urls import reverse

my_url = reverse('ACB')
my_url = reverse('ACB', args=(slug,))

For further information, see:

SoRobby
  • 273
  • 2
  • 8