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
Written by M. Sami GÜRPINAR
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#