what is reverse() in Django

前端 未结 7 1583
Happy的楠姐
Happy的楠姐 2020-11-30 16:40

When I read django code sometimes, I see in some templates reverse(). I am not quite sure what this is but it is used together with HttpResponseRedirect. How an

7条回答
  •  情歌与酒
    2020-11-30 17:08

    This is an old question, but here is something that might help someone.

    From the official docs:

    Django provides tools for performing URL reversing that match the different layers where URLs are needed: In templates: Using the url template tag. In Python code: Using the reverse() function. In higher level code related to handling of URLs of Django model instances: The get_absolute_url() method.

    Eg. in templates (url tag)

    2012 Archive
    

    Eg. in python code (using the reverse function)

    return HttpResponseRedirect(reverse('news-year-archive', args=(year,)))
    

提交回复
热议问题