Last Updated: February 25, 2016
·
536
· gurpinars

Django Getting Url Avoiding Hard-Coding

Assume that we have an url design like this:

url('^posts/(?P<slug>[-\w]+)/$',view_post,name="posts"),

and we are using getabsoluteurl() function.Instead of coding such as:

return "/posts/%s" %self.slug

we can use reverse() from django.core.urlresolvers:

return(reverse('posts', args=[self.slug, ]))

it will get automatically url we need