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

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

As an example:

view.py

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

urls.py

urlpatterns = patter         


        
7条回答
  •  说谎
    说谎 (楼主)
    2021-02-05 07:25

    You need reverse.

    from django.urls import reverse
    
    reverse('app1.view.view1')
    

    If you want to find out URL and redirect to it, use redirect

    from django.urls import redirect 
    
    redirect('app1.view.view1')
    

    If want to go further and not to hardcode your view names either, you can name your URL patterns and use these names instead.

提交回复
热议问题