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

后端 未结 7 1383
醉话见心
醉话见心 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:26

    As said by others, reverse function and url templatetags can (should) be used for this.

    I would recommend to add a name to your url pattern

    urlpatterns = patterns('',
        url( r'^view1$', 'app1.view.view1', name='view1'),
    )
    

    and to reverse it thanks to this name

    reverse('view1')
    

    That would make your code easier to refactor

提交回复
热议问题