Kintarō
Kintarō

Reputation: 3187

How to avoid hardcode URL in Django view

I am working on a simple project on Django. Currently the views that I am implementing always return a hardcode path:

def temp_view(request):
   ...
   return render("app/detail.html")

or

def temp_view_2(request):
   ...
   return redirect("/app/view2")

What I want to do is to get rid of the hardcode URL (for view URL and the template URL). Is there a proper way to do that?

Thanks.

Upvotes: 2

Views: 3457

Answers (1)

Jeffrey Froman
Jeffrey Froman

Reputation: 6623

Django provides a few different methods. In a view, the django.core.urlresolvers.reverse() function is most often used. A full discussion of this problem and the options provided by Django is here:

https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse-resolution-of-urls

Upvotes: 4

Related Questions