How to get the url path of a view function in django

后端 未结 7 1313
醉话见心
醉话见心 2021-02-05 06:41

As an example:

view.py

def view1( request ):
    return HttpResponse( \"just a test...\" )

urls.py

urlpatterns = patter         


        
7条回答
  •  猫巷女王i
    2021-02-05 07:07

    Yes, of course you can get the url path of view named 'view1' without hard-coding the url.

    All you need to do is - just import the 'reverse' function from Django urlresolvers.

    Just look at the below example code:

    from django.core.urlresolvers import reverse
    
    from django.http import HttpResponseRedirect
    
    def some_redirect_fun(request):
    
        return HttpResponseRedirect(reverse('view-name'))
    

提交回复
热议问题