| | 554 | |
|---|
| | 555 | Utility methods |
|---|
| | 556 | =============== |
|---|
| | 557 | |
|---|
| | 558 | reverse() |
|---|
| | 559 | --------- |
|---|
| | 560 | |
|---|
| | 561 | If you need to use something similar to the ``{% url %}`` template tag in your |
|---|
| | 562 | code, Django provides the ``django.core.urlresolvers.reverse()``. The |
|---|
| | 563 | ``reverse()`` function has the following signature:: |
|---|
| | 564 | |
|---|
| | 565 | reverse(viewname, urlconf=None, args=None, kwargs=None) |
|---|
| | 566 | |
|---|
| | 567 | The view name is either the function name or the `URL pattern name`_). |
|---|
| | 568 | Normally you will not need to worry about the ``urlconf`` parameter and will |
|---|
| | 569 | only pass in the positional and keyword arguments to use in the url matching. |
|---|
| | 570 | For example:: |
|---|
| | 571 | |
|---|
| | 572 | from django.core.urlresolvers import reverse |
|---|
| | 573 | |
|---|
| | 574 | def myview(request): |
|---|
| | 575 | return HttpResponseRedirect(reverse('arch-summary', args=[1945])) |
|---|
| | 576 | |
|---|
| | 577 | .. _URL pattern name: `Naming URL patterns`_ |
|---|
| | 578 | |
|---|
| | 579 | permalink() |
|---|
| | 580 | ----------- |
|---|
| | 581 | |
|---|
| | 582 | The ``permalink()`` decorator is useful for writing short methods that return |
|---|
| | 583 | a full URL path. For example, a model's ``get_absolute_url()`` method. Refer |
|---|
| | 584 | to the `model API documentation`_ for more information about ``permalink()``. |
|---|
| | 585 | |
|---|
| | 586 | .. _model API documentation: ../model-api/#the-permalink-decorator |
|---|
| | 587 | |
|---|